@Override public void close() throws IOException { channel.close(); } }
@Override public void close() throws Exception { in.close(); }
@Override public void close() throws IOException { channel.close(); } }
@Override public void close() throws IOException { channel.close(); } }
@Override public void close() throws Exception { in.close(); }
/** * @see ReadableByteChannel#close() */ public void close() throws IOException { readableByteChannel.close(); } }
@Override public void accept(ReadableByteChannel channel) { try { if (channel != null) { channel.close(); } } catch (IOException ioe) { throw new IllegalStateException(ioe); } } }
@Override public synchronized void close() throws IOException { channel.close(); } }
@Override public void close() throws IOException { originalChannel.close(); }
public void close() throws IOException { this.readableByteChannel.close(); }
/** * 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 void close() throws IOException { if (mClosed) { return; } if (mInputStream != null) { mInputStream.close(); mChannel.close(); } mClosed = true; }
@Override public synchronized void close() throws IOException { /* close the channel since Socket.getInputStream().close() * closes the socket. */ reader.channel.close(); reader.close(); }
if (input != null) { try { input.close();
@Override public void close() throws IOException { try { rbc.close(); } finally { if (!closed) { closed = true; } if (input != null) { releaseDirectByteBuffer(input); } if (uncompressedDirect != null) { releaseDirectByteBuffer(uncompressedDirect); } } }
@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 testReadableChannel() throws IOException { Resource resource = new FileSystemResource(getClass().getResource("Resource.class").getFile()); ReadableByteChannel channel = null; try { channel = resource.readableChannel(); ByteBuffer buffer = ByteBuffer.allocate((int) resource.contentLength()); channel.read(buffer); buffer.rewind(); assertTrue(buffer.limit() > 0); } finally { if (channel != null) { channel.close(); } } }
public void testCopyFileChannel() throws IOException { final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); File testFile = createTempFile(); FileOutputStream fos = new FileOutputStream(testFile); byte[] dummyData = newPreFilledByteArray(chunkSize); try { for (int i = 0; i < 500; i++) { fos.write(dummyData); } } finally { fos.close(); } ReadableByteChannel inChannel = new RandomAccessFile(testFile, "r").getChannel(); try { ByteStreams.copy(inChannel, outChannel); } finally { inChannel.close(); } byte[] actual = out.toByteArray(); for (int i = 0; i < 500 * chunkSize; i += chunkSize) { assertEquals(dummyData, Arrays.copyOfRange(actual, i, i + chunkSize)); } }
/** * Does some basic reads from {@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 testReadableByteChannel(ReadableByteChannel channel) throws Exception { assertTrue(channel.isOpen()); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byteBuffer.position(3); byteBuffer.limit(23); int byteCount = channel.read(byteBuffer); assertEquals(20, byteCount); assertEquals(23, byteBuffer.position()); assertEquals(23, byteBuffer.limit()); channel.close(); assertEquals(channel instanceof Buffer, channel.isOpen()); // Buffer.close() does nothing. byteBuffer.flip(); byteBuffer.position(3); byte[] data = new byte[byteBuffer.remaining()]; byteBuffer.get(data); assertEquals("abcdefghijklmnopqrst", new String(data, Charsets.UTF_8)); } }
@Test public void getReadableByteChannel() throws IOException { PathResource resource = new PathResource(TEST_FILE); ReadableByteChannel channel = null; try { channel = resource.readableChannel(); ByteBuffer buffer = ByteBuffer.allocate((int) resource.contentLength()); channel.read(buffer); buffer.rewind(); assertThat(buffer.limit(), greaterThan(0)); } finally { if (channel != null) { channel.close(); } } }