Git Product home page Git Product logo

ksuid's Issues

Expose KsuidGenerator#newKsuid(Instant)

It would be nice to be able to create Ksuids directly from Instants. Currently, the only option is to do:

new KsuidGenerator(new SecureRandom()).newKsuid(instant)

Ksuid class does not implement Serializable

Hello. First, thank you for this Java port of the Ksuid library. I have a question: is there any reason why the Ksuid class does not implement Serializable? This severely limits its usage, and other Java implementations defines it as serializable so it looks like it should be ok.

Support of unsigned 32 bit UTC timestamp.

It seems that the base62 encoder does not handle uint_32 timestamp properly. This Java version of ksuid only supports year 2014~2082 while the Golang version actually supportes year 2014~2150.

static String base62Encode(final byte[] bytes, final int length) {
    final int size = (int) ceil((bytes.length * BYTE_BITS) / DIGIT_BITS);
    final StringBuilder sb = new StringBuilder(size);

    // Allocate an extra zero value byte in the first position, so that the unsigned 32bit UTC 
    // timestamp value is not treated as negative value and thus being encoded as
    // "000000000000000000000000000"
    byte[] b = new byte[bytes.length+1];
    System.arraycopy(bytes, 0, b, 1, bytes.length);

    BigInteger value = new BigInteger(b);
    while (value.compareTo(ZERO) > 0) {
        final BigInteger[] quotientAndRemainder = value.divideAndRemainder(BASE);
        sb.append(BASE_62_CHARACTERS[abs(quotientAndRemainder[1].intValue())]);
        value = quotientAndRemainder[0];
    }

    while (length > 0 && sb.length() < length) {
        sb.append('0');
    }

    return sb.reverse().toString();
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.