Refine search
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; } }
@NoWarning("OBL_UNSATISFIED_OBLIGATION") public static void transfer(InputStream src, File dest) throws IOException { ReadableByteChannel in = Channels.newChannel(src); try { WritableByteChannel out = new FileOutputStream(dest).getChannel(); try { nioCopy(in, out); } finally { out.close(); } } finally { in.close(); } }
@Override public void write(Buffer source, long byteCount) throws IOException { if (!channel.isOpen()) throw new IllegalStateException("closed"); if (byteCount == 0) return; long remaining = byteCount; while (remaining > 0) { timeout.throwIfReached(); try (Buffer.UnsafeCursor ignored = source.readUnsafe(cursor)) { cursor.seek(0); int length = (int) Math.min(cursor.end - cursor.start, remaining); int written = channel.write(ByteBuffer.wrap(cursor.data, cursor.start, length)); remaining -= written; source.skip(written); } } }
public void writeBuffer(ByteBuffer buffer, OutputStream stream) { WritableByteChannel channel = Channels.newChannel(stream); channel.write(buffer); }
/** * 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); } }
/** * Writes the implementation specific header or "marker bytes" to * <i>out</i>. * * @param out The underlying {@link OutputStream}. * @throws IOException */ private void writeHeader(WritableByteChannel out) throws IOException { out.write(ByteBuffer.wrap(HEADER_BYTES)); }
int bufferSize = 4096; ByteBuffer buffer = ByteBuffer.allocate(bufferSize); WritableByteChannel wch = Channels.newChannel(outputStream); ReadableByteChannel rch; for (Object byteChunk : byteChunks) { continue; rch = Channels.newChannel( new ByteArrayInputStream((byte[]) byteChunk)); do { buffer.clear(); readSize = rch.read(buffer); buffer.flip(); while ((writeSize += wch.write(buffer)) < readSize) { rch.close();
public static void downloadFromMaster(Map conf, String file, String localFile) throws IOException, TException { NimbusClient client = NimbusClient.getConfiguredClient(conf); String id = client.getClient().beginFileDownload(file); WritableByteChannel out = Channels.newChannel(new FileOutputStream( localFile)); while (true) { ByteBuffer chunk = client.getClient().downloadChunk(id); int written = out.write(chunk); if (written == 0) break; } out.close(); }
@Override public void send(final FileChannel channel, final long position, final long count) throws Exception { try (FileChannel src = channel) { WritableByteChannel dest = Channels.newChannel(rsp.getOutputStream()); src.transferTo(position, count, dest); dest.close(); committed = true; } }
@Override protected long transferContentTo(WritableByteChannel target) throws IOException { ReadableByteChannel channel = getChannel(); ByteBuffer buffer = getBuffer(); int transferred = 0; int read = channel.read(buffer); if (read > 0) { buffer.flip(); while (buffer.hasRemaining()) { transferred += target.write(buffer); } buffer.compact(); position += transferred; } if (position == getContentLength() || read < 0) { state = MultipartState.POST_CONTENT; if (channel.isOpen()) { channel.close(); } } return transferred; }
@Test public void getWritableChannel() throws IOException { PathResource resource = new PathResource(temporaryFolder.newFile("test").toPath()); ByteBuffer buffer = ByteBuffer.wrap("test".getBytes(StandardCharsets.UTF_8)); WritableByteChannel channel = null; try { channel = resource.writableChannel(); channel.write(buffer); } finally { if (channel != null) { channel.close(); } } assertThat(resource.contentLength(), equalTo(4L)); }
/** * 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. }
/** * Closes the channels that will end up closing the input and output streams of the connection. * The channels implement the InterruptibleChannel interface so any other thread that was * blocked in an I/O operation will be interrupted and will get an exception. * * @throws IOException if an I/O error occurs. */ public void close() throws IOException { wbc.close(); rbc.close(); }
@Override public synchronized void write(byte[] source, int offset, int length) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(source, offset, length); checkBlocking(channel); int total = 0; while (total < length) { total += channel.write(buffer); } }
@Override public void close() throws IOException { channel.close(); } }
@Override public boolean isOpen() { return channel.isOpen(); }
/** * 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); }