public static ByteBuffer newByteBuffer (int numBytes) { ByteBuffer buffer = ByteBuffer.allocateDirect(numBytes); buffer.order(ByteOrder.nativeOrder()); return buffer; }
public static int byteArrayToLeInt(byte[] b) { final ByteBuffer bb = ByteBuffer.wrap(b); bb.order(ByteOrder.LITTLE_ENDIAN); return bb.getInt(); } public static byte[] leIntToByteArray(int i) { final ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); bb.order(ByteOrder.LITTLE_ENDIAN); bb.putInt(i); return bb.array(); }
private void ensureBufferSize(int bytes) { if (this.data.position() + bytes > this.data.limit()) { if (this.isResizeAllowed) { byte[] array = this.data.array(); byte[] newArray = new byte[array.length + bytes + (array.length >> 1)]; System.arraycopy(array, 0, newArray, 0, this.data.position()); int lastPos = this.data.position(); this.data = ByteBuffer.wrap(newArray); this.data.order(ByteOrder.LITTLE_ENDIAN); this.data.position(lastPos); this.data.limit(this.data.capacity()); } } }
void setup (byte[] pcm, int channels, int sampleRate) { int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2)); int samples = bytes / (2 * channels); duration = samples / (float)sampleRate; ByteBuffer buffer = ByteBuffer.allocateDirect(bytes); buffer.order(ByteOrder.nativeOrder()); buffer.put(pcm, 0, bytes); buffer.flip(); if (bufferID == -1) { bufferID = alGenBuffers(); alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate); } }
@Override public void queueInput(ByteBuffer inputBuffer) { Assertions.checkState(outputChannels != null); int position = inputBuffer.position(); int limit = inputBuffer.limit(); int frameCount = (limit - position) / (2 * channelCount); int outputSize = frameCount * outputChannels.length * 2; if (buffer.capacity() < outputSize) { buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder()); } else { buffer.clear(); } while (position < limit) { for (int channelIndex : outputChannels) { buffer.putShort(inputBuffer.getShort(position + 2 * channelIndex)); } position += channelCount * 2; } inputBuffer.position(limit); buffer.flip(); outputBuffer = buffer; }
@Override public byte[] serialize(String value) { final byte[] serialized = value.getBytes(StandardCharsets.UTF_8); final byte[] targetBytes = new byte[Integer.BYTES + serialized.length]; final ByteBuffer bb = ByteBuffer.wrap(targetBytes).order(ByteOrder.LITTLE_ENDIAN); bb.putInt(serialized.length); bb.put(serialized); return targetBytes; }
/** Read from a fixed byte[]; should not be closed. */ AutoBuffer( byte[] buf, int off ) { assert buf != null : "null fed to ByteBuffer.wrap"; _bb = ByteBuffer.wrap(buf).order(ByteOrder.nativeOrder()); _bb.position(off); _chan = null; _h2o = null; _read = true; _firstPage = true; _persist = 0; // No persistance }
/** * Initializes the buffer. * * @param timeUs The presentation timestamp for the buffer, in microseconds. * @param size An upper bound on the size of the data that will be written to the buffer. * @return The {@link #data} buffer, for convenience. */ public ByteBuffer init(long timeUs, int size) { this.timeUs = timeUs; if (data == null || data.capacity() < size) { data = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()); } data.position(0); data.limit(size); return data; }
/** * Gets a byte array representation of this instance. * * <p><b>Note:</b> No guarantees are made regarding stability of the representation between * versions. */ public byte[] toByteArray() { ByteBuffer buff = ByteBuffer.allocate(BYTES).order(ByteOrder.LITTLE_ENDIAN); writeTo(buff); return buff.array(); }
/** * Gets a byte array representation of this instance. * * <p><b>Note:</b> No guarantees are made regarding stability of the representation between * versions. */ public byte[] toByteArray() { ByteBuffer buffer = ByteBuffer.allocate(BYTES).order(ByteOrder.LITTLE_ENDIAN); xStats.writeTo(buffer); yStats.writeTo(buffer); buffer.putDouble(sumOfProductsOfDeltas); return buffer.array(); }