@Override public ChannelBuffer copy(int index, int length) { ByteBuffer src; try { src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(); } ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); dst.put(src); dst.clear(); return new ByteBufferBackedChannelBuffer(dst); }
private static byte[] readBytesFromFileInputStream(FileInputStream fis) throws IOException { FileChannel channel = fis.getChannel(); int fileSize = (int) channel.size(); ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize); channel.read(byteBuffer); byteBuffer.flip(); byte[] bytes = byteBuffer.array(); byteBuffer.clear(); channel.close(); fis.close(); return bytes; }
@Override public AppendMessageResult<MessageSequence> doAppend(long baseOffset, ByteBuffer targetBuffer, int freeSpace, PullLogMessage message) { workingBuffer.clear(); final long wroteOffset = baseOffset + targetBuffer.position(); workingBuffer.flip(); workingBuffer.limit(PULL_LOG_UNIT_BYTES); workingBuffer.putLong(message.getMessageSequence()); targetBuffer.put(workingBuffer.array(), 0, PULL_LOG_UNIT_BYTES); final long messageIndex = wroteOffset / PULL_LOG_UNIT_BYTES; return new AppendMessageResult<>(AppendMessageStatus.SUCCESS, wroteOffset, PULL_LOG_UNIT_BYTES, new MessageSequence(messageIndex, wroteOffset)); } }
@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; }
@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; }
private void read(ByteBuffer buffer) throws IOException { buffer.clear(); while (buffer.hasRemaining()) if (channel.read(buffer) == -1) throw new EOFException(); buffer.flip(); }
@Override protected void doCleanup(Context context) throws IOException, InterruptedException { Iterator<Integer> it = hllcMap.keySet().iterator(); ByteBuffer buf = ByteBuffer.allocate(BufferedMeasureCodec.DEFAULT_BUFFER_SIZE); while (it.hasNext()) { int key = it.next(); HLLCounter hllc = hllcMap.get(key); buf.clear(); hllc.writeRegisters(buf); buf.flip(); context.write(new IntWritable(key), new BytesWritable(buf.array(), buf.limit())); } }
private long flushFreeIds0( ByteBuffer writeBuffer ) throws IOException { channel.position( channel.size() ); writeBuffer.clear(); while ( !freeIds.isEmpty() ) { long id = freeIds.dequeue(); if ( id == NO_RESULT ) { continue; } writeBuffer.putLong( id ); if ( writeBuffer.position() == writeBuffer.capacity() ) { writeBuffer.flip(); channel.writeAll( writeBuffer ); writeBuffer.clear(); } } writeBuffer.flip(); if ( writeBuffer.hasRemaining() ) { channel.writeAll( writeBuffer ); } return channel.position(); }
public AbstractLegacyRecordBatch nextBatch() throws IOException { offsetAndSizeBuffer.clear(); Utils.readFully(stream, offsetAndSizeBuffer); if (offsetAndSizeBuffer.hasRemaining()) return null; long offset = offsetAndSizeBuffer.getLong(Records.OFFSET_OFFSET); int size = offsetAndSizeBuffer.getInt(Records.SIZE_OFFSET); if (size < LegacyRecord.RECORD_OVERHEAD_V0) throw new CorruptRecordException(String.format("Record size is less than the minimum record overhead (%d)", LegacyRecord.RECORD_OVERHEAD_V0)); if (size > maxMessageSize) throw new CorruptRecordException(String.format("Record size exceeds the largest allowable message size (%d).", maxMessageSize)); ByteBuffer batchBuffer = ByteBuffer.allocate(size); Utils.readFully(stream, batchBuffer); if (batchBuffer.hasRemaining()) return null; batchBuffer.flip(); return new BasicLegacyRecordBatch(offset, new LegacyRecord(batchBuffer)); } }
public static boolean readAndFlip( ReadableByteChannel channel, ByteBuffer buffer, int bytes ) throws IOException { buffer.clear(); buffer.limit( bytes ); while ( buffer.hasRemaining() ) { int read = channel.read( buffer ); if ( read == -1 ) { return false; } } buffer.flip(); return true; }