public static void writeCompletely(WritableByteChannel channel, ByteBuffer src) throws IOException { while (src.hasRemaining()) { channel.write(src); } }
/** * Writes the complete contents of this byte buffer output stream to * the specified output stream argument. * * @param out the output stream to which to write the data. * @exception IOException if an I/O error occurs. */ public void writeTo(OutputStream out) throws IOException { WritableByteChannel channel = Channels.newChannel(out); ByteBuffer bb = curBuf.duplicate(); bb.flip(); channel.write(bb); }
/** * An efficient copy between two channels with a fixed-size buffer. * * @param src the source channel * @param dest the destination channel */ public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { // TODO(yupeng): make the buffer size configurable final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { buffer.flip(); dest.write(buffer); buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } }
@Override public void getBox(WritableByteChannel writableByteChannel) throws IOException { writableByteChannel.write(getHeader()); ByteBuffer byteBuffer = ByteBuffer.allocate(8); byteBuffer.position(6); IsoTypeWriter.writeUInt16(byteBuffer, dataReferenceIndex); byteBuffer.rewind(); writableByteChannel.write(byteBuffer); writableByteChannel.write(ByteBuffer.wrap(data)); }
/** * Does some basic writes to {@code channel}. We execute this against both Okio's channels and * also a standard implementation from the JDK to confirm that their behavior is consistent. */ private void testWritableByteChannel(WritableByteChannel channel) throws Exception { assertTrue(channel.isOpen()); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byteBuffer.put("abcdefghijklmnopqrstuvwxyz".getBytes(Charsets.UTF_8)); byteBuffer.flip(); byteBuffer.position(3); byteBuffer.limit(23); int byteCount = channel.write(byteBuffer); assertEquals(20, byteCount); assertEquals(23, byteBuffer.position()); assertEquals(23, byteBuffer.limit()); channel.close(); assertEquals(channel instanceof Buffer, channel.isOpen()); // Buffer.close() does nothing. }
protected void copy(JarFile in, JarEntry je, WritableByteChannel out) throws IOException { InputStream is = in.getInputStream(je); try { ReadableByteChannel inChannel = Channels.newChannel(is); ByteBuffer byteBuffer = ByteBuffer.allocate(Long.valueOf(je.getSize()).intValue()); inChannel.read(byteBuffer); byteBuffer.rewind(); out.write(byteBuffer); } finally { is.close(); } }
public void sendTranscodeOutputToChannel(long offset, long length, java.nio.channels.WritableByteChannel chan) throws java.io.IOException { // We ignore the offset here because that's when this is used through the MediaServer and that only happens // w/ the FFMPEGTranscoder and NOT w/ the remuxer if (nioTmpBuf == null || nioTmpBuf.capacity() < length) nioTmpBuf = java.nio.ByteBuffer.allocate((int)length); nioTmpBuf.clear(); nioTmpBuf.limit((int)length); readFullyTranscodedData(nioTmpBuf); chan.write(nioTmpBuf); }
while (buf.hasRemaining()) { remaining -= dest.write(buf); buf.clear(); while (buf.hasRemaining()) { remaining -= dest.write(buf); buf.clear();
public static long stream(InputStream input, OutputStream output) throws IOException { try ( ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output); ) { ByteBuffer buffer = ByteBuffer.allocateDirect(10240); long size = 0; while (inputChannel.read(buffer) != -1) { buffer.flip(); size += outputChannel.write(buffer); buffer.clear(); } return size; } }
private void writeBlock() throws IOException { buffer.flip(); int i = out.write(buffer); boolean hasRemaining = buffer.hasRemaining(); if (i != blockSize || hasRemaining) { String msg = String .format("Failed to write %,d bytes atomically. Only wrote %,d", blockSize, i); throw new IOException(msg); } buffer.clear(); }
@Override public void getBox(WritableByteChannel writableByteChannel) throws IOException { writableByteChannel.write(getHeader()); ByteBuffer byteBuffer = ByteBuffer.allocate(8); byteBuffer.position(6); IsoTypeWriter.writeUInt16(byteBuffer, dataReferenceIndex); writableByteChannel.write((ByteBuffer) byteBuffer.rewind()); writableByteChannel.write(ByteBuffer.wrap(vc1Content)); }
@Override public void compile() throws IOException { DataOutputStream dataOutput = new DataOutputStream( new BufferedOutputStream(output) ); dataOutput.writeInt(cardinality); dataOutput.writeInt(bufferSize * SHORT_BYTES); ByteBuffer byteBuffer = ByteBuffer.allocate(costs.array().length * SHORT_BYTES); for (short cost : this.costs.array()) { byteBuffer.putShort(cost); } WritableByteChannel channel = Channels.newChannel(dataOutput); byteBuffer.flip(); channel.write(byteBuffer); dataOutput.close(); }
private boolean flush(ByteBuffer bb) throws IOException { wbc.write(bb); return !bb.hasRemaining(); }
protected void copy(JarFile in, JarEntry je, WritableByteChannel out) throws IOException { InputStream is = in.getInputStream(je); try { ReadableByteChannel inChannel = Channels.newChannel(is); ByteBuffer byteBuffer = ByteBuffer.allocate(Long.valueOf(je.getSize()).intValue()); inChannel.read(byteBuffer); byteBuffer.rewind(); out.write(byteBuffer); } finally { is.close(); } }