/** * Compute the length of the cnc file and return it. * * @param totalLengthOfBuffers in bytes * @param alignment for file length to adhere to * @return cnc file length in bytes */ public static int computeCncFileLength(final int totalLengthOfBuffers, final int alignment) { return align(END_OF_METADATA_OFFSET + totalLengthOfBuffers, alignment); }
private static void checkTicksPerWheel(final int ticksPerWheel) { if (!BitUtil.isPowerOfTwo(ticksPerWheel)) { throw new IllegalArgumentException("ticks per wheel must be a power of 2: " + ticksPerWheel); } }
/** * Construct a key set with the given capacity. * * @param initialCapacity for the key set */ public NioSelectedKeySet(final int initialCapacity) { keys = new SelectionKey[BitUtil.findNextPositivePowerOfTwo(initialCapacity)]; }
public LongRingBuffer(int capacity) { /* * For ease of implementation, the actual capacity must be a power of 2; this allows * easy remainder calculations, etc. */ if (BitUtil.isPowerOfTwo(capacity)) { this.bufferCapacity = capacity; } else { this.bufferCapacity = BitUtil.findNextPositivePowerOfTwo(capacity); } elements = new long[bufferCapacity]; Arrays.fill(elements, -1); head = -1; tail = -1; this.capacity = capacity; }
/** * Generate a byte array that is a hex representation of a given byte array. * * @param buffer to convert to a hex representation. * @return new byte array that is hex representation (in Big Endian) of the passed array. */ public static byte[] toHexByteArray(final byte[] buffer) { return toHexByteArray(buffer, 0, buffer.length); }
@Test public void shouldConvertToHexCorrectly() { final byte[] buffer = { 0x01, 0x23, 0x45, 0x69, 0x78, (byte)0xBC, (byte)0xDA, (byte)0xEF, 0x5F }; final byte[] converted = toHexByteArray(buffer); final String hexStr = toHex(buffer); assertThat(converted[0], is((byte)'0')); assertThat(converted[1], is((byte)'1')); assertThat(converted[2], is((byte)'2')); assertThat(converted[3], is((byte)'3')); assertThat(hexStr, is("0123456978bcdaef5f")); }
private static void validateDataAddress(final byte[] addressAsBytes) { if (BitUtil.isEven(addressAsBytes[addressAsBytes.length - 1])) { throw new IllegalArgumentException("Multicast data address must be odd"); } }
/** * Generate a byte array from a string that is the hex representation of the given byte array. * * @param string to convert from a hex representation (in Big Endian). * @return new byte array holding the decimal representation of the passed array. */ public static byte[] fromHex(final String string) { return fromHexByteArray(string.getBytes(UTF_8)); }
public LongRingBuffer(int capacity) { /* * For ease of implementation, the actual capacity must be a power of 2; this allows * easy remainder calculations, etc. */ if (BitUtil.isPowerOfTwo(capacity)) { this.bufferCapacity = capacity; } else { this.bufferCapacity = BitUtil.findNextPositivePowerOfTwo(capacity); } elements = new long[bufferCapacity]; Arrays.fill(elements, -1); head = -1; tail = -1; this.capacity = capacity; }
/** * Generate a string that is the hex representation of a given byte array. * * @param buffer to convert to a hex representation. * @param offset the offset into the buffer. * @param length the number of bytes to convert. * @return new String holding the hex representation (in Big Endian) of the passed array. */ public static String toHex(final byte[] buffer, final int offset, final int length) { return new String(toHexByteArray(buffer, offset, length), UTF_8); }
/** * Generate a byte array from a string that is the hex representation of the given byte array. * * @param string to convert from a hex representation (in Big Endian) * @return new byte array holding the decimal representation of the passed array */ public static byte[] fromHex(final String string) { return fromHexByteArray(string.getBytes(UTF8_CHARSET)); }
/** * Compute the total length of a log file given the term length. * * Assumes {@link #TERM_MAX_LENGTH} is 1GB and that filePageSize is 1GB or less and a power of 2. * * @param termLength on which to base the calculation. * @param filePageSize to use for log. * @return the total length of the log file. */ public static long computeLogLength(final int termLength, final int filePageSize) { if (termLength < (1024 * 1024 * 1024)) { return align((termLength * PARTITION_COUNT) + LOG_META_DATA_LENGTH, filePageSize); } return (PARTITION_COUNT * (long)termLength) + align(LOG_META_DATA_LENGTH, filePageSize); }
private static void checkResolution(final int tickResolution) { if (!BitUtil.isPowerOfTwo(tickResolution)) { throw new IllegalArgumentException(" tick resolution must be a power of 2: " + tickResolution); } } }
@SuppressWarnings("unchecked") public AbstractConcurrentArrayQueue(final int requestedCapacity) { capacity = BitUtil.findNextPositivePowerOfTwo(requestedCapacity); buffer = (E[])new Object[capacity]; }
/** * Generate a string that is the hex representation of a given byte array. * * @param buffer to convert to a hex representation. * @return new String holding the hex representation (in Big Endian) of the passed array. */ public static String toHex(final byte[] buffer) { return new String(toHexByteArray(buffer), UTF_8); }
private static void validateDataAddress(final byte[] addressAsBytes) { if (BitUtil.isEven(addressAsBytes[addressAsBytes.length - 1])) { throw new IllegalArgumentException("Multicast data address must be odd"); } }
/** * Generate a byte array from a string that is the hex representation of the given byte array. * * @param string to convert from a hex representation (in Big Endian). * @return new byte array holding the decimal representation of the passed array. */ public static byte[] fromHex(final String string) { return fromHexByteArray(string.getBytes(UTF_8)); }
private int labelOffset() { return KEY_LENGTH_OFFSET + SIZE_OF_INT + BitUtil.align(buffer.getInt(offset + KEY_LENGTH_OFFSET), SIZE_OF_INT); } }
/** * Check the the buffer capacity is the correct size (a power of 2 + {@link RingBufferDescriptor#TRAILER_LENGTH}). * * @param capacity to be checked. * @throws IllegalStateException if the buffer capacity is incorrect. */ public static void checkCapacity(final int capacity) { if (!BitUtil.isPowerOfTwo(capacity - TRAILER_LENGTH)) { final String msg = "capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity; throw new IllegalStateException(msg); } } }