/** * Creates a new byte sequence of the given size where all bits are 0. * * @param size number of bytes * @return a new immutable byte sequence */ public static ImmutableByteSequence ofZeros(int size) { // array is initialized to all 0's by default return new ImmutableByteSequence(ByteBuffer.wrap(new byte[size])); }
/** * Creates a new byte sequence of the given size where all bits are 1. * * @param size number of bytes * @return a new immutable byte sequence */ public static ImmutableByteSequence ofOnes(int size) { byte[] bytes = new byte[size]; Arrays.fill(bytes, (byte) 0xFF); return new ImmutableByteSequence(ByteBuffer.wrap(bytes)); }
/** * Creates a new byte sequence of 1 byte containing the given value. * * @param original a byte value * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(byte original) { return new ImmutableByteSequence( ByteBuffer.allocate(Byte.BYTES).put(original)); }
/** * Creates a new byte sequence of 8 bytes containing the given long value. * * @param original a long value * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(long original) { return new ImmutableByteSequence( ByteBuffer.allocate(Long.BYTES).putLong(original)); }
/** * Creates a new byte sequence of 2 bytes containing the given short value. * * @param original a short value * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(short original) { return new ImmutableByteSequence( ByteBuffer.allocate(Short.BYTES).putShort(original)); }
/** * Creates a new byte sequence of 4 bytes containing the given int value. * * @param original an int value * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(int original) { return new ImmutableByteSequence( ByteBuffer.allocate(Integer.BYTES).putInt(original)); }
/** * Creates a new immutable byte sequence with the same content and order of * the passed byte array. * * @param original a byte array value * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(byte[] original) { checkArgument(original != null && original.length > 0, "Cannot copy from an empty or null array"); return new ImmutableByteSequence( ByteBuffer.allocate(original.length).put(original)); }
/** * Creates a new immutable byte sequence with the same content and order of * the passed byte array, from/to the given indexes (inclusive). * * @param original a byte array value * @param fromIdx starting index * @param toIdx ending index * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(byte[] original, int fromIdx, int toIdx) { checkArgument(original != null && original.length > 0, "Cannot copy from an empty or null array"); checkArgument(toIdx >= fromIdx && toIdx < original.length, "invalid indexes"); ByteBuffer buffer = ByteBuffer.allocate((toIdx - fromIdx) + 1); for (int i = fromIdx; i <= toIdx; i++) { buffer.put(original[i]); } return new ImmutableByteSequence(buffer); }
/** * Creates a new byte sequence that is prefixed with specified number of * zeros if val = 0 or ones if val = 0xff. * * @param size number of total bytes * @param prefixBits number of bits in prefix * @param val 0 for prefix of zeros; 0xff for prefix of ones * @return new immutable byte sequence */ static ImmutableByteSequence prefix(int size, long prefixBits, byte val) { checkArgument(val == 0 || val == (byte) 0xff, "Val must be 0 or 0xff"); byte[] bytes = new byte[size]; int prefixBytes = (int) (prefixBits / Byte.SIZE); Arrays.fill(bytes, 0, prefixBytes, val); Arrays.fill(bytes, prefixBytes, bytes.length, (byte) ~val); int partialBits = (int) (prefixBits % Byte.SIZE); if (partialBits != 0) { bytes[prefixBytes] = val == 0 ? (byte) (0xff >> partialBits) : (byte) (0xff << Byte.SIZE - partialBits); } return new ImmutableByteSequence(ByteBuffer.wrap(bytes)); }
/** * Creates a new immutable byte sequence copying bytes from the given * ByteBuffer {@link ByteBuffer}. If the byte buffer order is not big-endian * bytes will be copied in reverse order. * * @param original a byte buffer * @return a new byte buffer object */ public static ImmutableByteSequence copyFrom(ByteBuffer original) { checkArgument(original != null && original.capacity() > 0, "Cannot copy from an empty or null byte buffer"); byte[] bytes = new byte[original.capacity()]; // copy bytes from original buffer original.rewind(); original.get(bytes); if (original.order() == ByteOrder.LITTLE_ENDIAN) { // FIXME: this can be improved, e.g. read bytes in reverse order from original reverse(bytes); } return new ImmutableByteSequence(ByteBuffer.wrap(bytes)); }
return new ImmutableByteSequence(newBuffer);