@Override public void close() throws IOException { channel.close(); } }
@Override public void close() throws IOException { channel.close(); } }
@Override public synchronized void close() throws IOException { channel.close(); } }
public void close() throws IOException { dataChannel.close(); if(nioEnabled) { outChannel.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 { mChannel.close(); } };
@Override public void close() throws IOException { if (closed.compareAndSet(false, true)) { try { flushBlock(); } finally { out.close(); } } }
@Override public void close() throws IOException { buffer.clear(); if ( channel != null ) { channel.close(); } }
@Override public final void close() throws IOException { if (closed) { return; } try { flush(); out.close(); } finally { closed = true; releaseDirectByteBuffer(directInputBuffer); releaseDirectByteBuffer(outputBuffer); } }
/** * 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. }
@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)); }
@Test public void testChannelWriteString() throws IOException { final int index = 0; WritableByteChannel channelOutput = Channels.newChannel(outStream); serializerUtils.writeString(channelOutput, strings[index]); ByteArrayInputStream inputstream = new ByteArrayInputStream(outStream.toByteArray()); channelOutput.close(); inputstream.close(); String expected = serializerUtils.readString(inputstream); String actuals = strings[index]; Assert.assertEquals(expected, actuals); }
private GenericIndexed<String> serializeAndDeserialize(GenericIndexed<String> indexed) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); final WritableByteChannel channel = Channels.newChannel(baos); indexed.writeTo(channel, null); channel.close(); final ByteBuffer byteBuffer = ByteBuffer.wrap(baos.toByteArray()); Assert.assertEquals(indexed.getSerializedSize(), byteBuffer.remaining()); GenericIndexed<String> deserialized = GenericIndexed.read(byteBuffer, GenericIndexed.STRING_STRATEGY); Assert.assertEquals(0, byteBuffer.remaining()); return deserialized; } }
@Test public void testChannelWritelong() throws IOException { final int index = 0; WritableByteChannel channelOutput = Channels.newChannel(outStream); serializerUtils.writeLong(channelOutput, longs[index]); ByteArrayInputStream inputstream = new ByteArrayInputStream(outStream.toByteArray()); channelOutput.close(); inputstream.close(); long expected = serializerUtils.readLong(inputstream); long actuals = longs[index]; Assert.assertEquals(expected, actuals); }
@Test public void testChannelWritefloat() throws IOException { final int index = 0; WritableByteChannel channelOutput = Channels.newChannel(outStream); serializerUtils.writeFloat(channelOutput, floats[index]); ByteArrayInputStream inputstream = new ByteArrayInputStream(outStream.toByteArray()); if (channelOutput != null) { channelOutput.close(); } float expected = serializerUtils.readFloat(inputstream); float actuals = floats[index]; Assert.assertEquals(expected, actuals, delta); }
@Test public void writeWritableByteChannelErrorInWrite() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar); WritableByteChannel channel = mock(WritableByteChannel.class); when(channel.write(any())) .thenAnswer(invocation -> { ByteBuffer buffer = invocation.getArgument(0); int written = buffer.remaining(); buffer.position(buffer.limit()); return written; }) .thenThrow(new IOException()); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectError(IOException.class) .verify(Duration.ofSeconds(3)); channel.close(); }
@Test public void writeWritableByteChannelCancel() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult, 1) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(Duration.ofSeconds(5)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foo", result); channel.close(); flux.subscribe(DataBufferUtils::release); }
@Test public void writeWritableByteChannelErrorInFlux() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException())); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectError() .verify(Duration.ofSeconds(5)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobar", result); channel.close(); }
@Test public void writeWritableByteChannel() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); DataBuffer baz = stringBuffer("baz"); DataBuffer qux = stringBuffer("qux"); Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); verifyWrittenData(writeResult); channel.close(); }