/** * Flips the buffer output buffer so we can start reading bytes from it. If we are starting to * drain because there was overflow, and there aren't actually any characters to drain, then the * overflow must be due to a small output buffer. */ private void startDraining(boolean overflow) { byteBuffer.flip(); if (overflow && byteBuffer.remaining() == 0) { byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2); } else { draining = true; } }
/** * Reads and returns {@link ByteBuffer} from the underlying {@link SocketChannel} represented by * the <code>key</code>. Due to the fact that there is a dedicated channel for each client * connection we don't need to store the sender. */ @Override public ByteBuffer read(SelectionKey key) throws IOException { SocketChannel socketChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int read = socketChannel.read(buffer); buffer.flip(); if (read == -1) { throw new IOException("Socket closed"); } return buffer; }
private void munch() { buffer.flip(); while (buffer.remaining() >= chunkSize) { // we could limit the buffer to ensure process() does not read more than // chunkSize number of bytes, but we trust the implementations process(buffer); } buffer.compact(); // preserve any remaining data that do not make a full chunk } }
/** * Check if the given ByteBuffer capacity * @param existingBuffer ByteBuffer capacity to check * @param newLength new length for the ByteBuffer. * returns ByteBuffer */ public static ByteBuffer ensureCapacity(ByteBuffer existingBuffer, int newLength) { if (newLength > existingBuffer.capacity()) { ByteBuffer newBuffer = ByteBuffer.allocate(newLength); existingBuffer.flip(); newBuffer.put(existingBuffer); return newBuffer; } return existingBuffer; }
@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 void queueInput(ByteBuffer buffer) { int remaining = buffer.remaining(); if (remaining == 0) { return; } audioBufferSink.handleBuffer(buffer.asReadOnlyBuffer()); if (this.buffer.capacity() < remaining) { this.buffer = ByteBuffer.allocateDirect(remaining).order(ByteOrder.nativeOrder()); } else { this.buffer.clear(); } this.buffer.put(buffer); this.buffer.flip(); outputBuffer = this.buffer; }
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); } }
/** * This is invoked for the last bytes of the input, which are not enough to fill a whole chunk. * The passed {@code ByteBuffer} is guaranteed to be non-empty. * * <p>This implementation simply pads with zeros and delegates to {@link #process(ByteBuffer)}. */ protected void processRemaining(ByteBuffer bb) { bb.position(bb.limit()); // move at the end bb.limit(chunkSize + 7); // get ready to pad with longs while (bb.position() < chunkSize) { bb.putLong(0); } bb.limit(chunkSize); bb.flip(); process(bb); }
@Test public void testParseUnknownVersion() { ByteBuffer buffer = ByteBuffer.allocate(32); buffer.putShort((short) 5); buffer.putShort(ControlRecordType.ABORT.type); buffer.putInt(23432); // some field added in version 5 buffer.flip(); ControlRecordType type = ControlRecordType.parse(buffer); assertEquals(ControlRecordType.ABORT, type); }
@Override public void run() throws Exception { ByteBuffer buf = ByteBuffer.allocate(1024 * 1024); long totalBytes = 0; for (int i = 0; i < testTimes; i++) { buf.clear(); newCounter.writeRegisters(buf); totalBytes += buf.position(); buf.flip(); newCounter.readRegisters(buf); } System.out.println("new serialize bytes : " + totalBytes / testTimes + "B"); } });
private void doTestWriteToByteBuffer(ByteBuffer source, ByteBuffer dest) throws IOException { int numBytes = source.remaining(); int position = source.position(); DataOutputStream out = new DataOutputStream(new ByteBufferOutputStream(dest)); Utils.writeTo(out, source, source.remaining()); dest.flip(); assertEquals(numBytes, dest.remaining()); assertEquals(position, source.position()); assertEquals(source, dest); }
@Test public void testDirectBuffer() throws IOException { byte[] compressed = compressedBytes(); ByteBuffer buffer; buffer = ByteBuffer.allocateDirect(compressed.length); buffer.put(compressed).flip(); testDecompression(buffer); int offset = 42; buffer = ByteBuffer.allocateDirect(compressed.length + offset + 123); buffer.position(offset); buffer.put(compressed).flip(); buffer.position(offset); testDecompression(buffer); }
private void testWriteByteBuffer(ByteBuffer input) throws IOException { long value = 234239230L; input.putLong(value); input.flip(); ByteBufferOutputStream output = new ByteBufferOutputStream(ByteBuffer.allocate(32)); output.write(input); assertEquals(8, input.position()); assertEquals(8, output.position()); assertEquals(value, output.buffer().getLong(0)); output.close(); }