Search in sources :

Example 11 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project druid by druid-io.

the class SerializerUtilsTest method testChannelWriteString.

@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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) Test(org.junit.Test)

Example 12 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project LuaViewSDK by alibaba.

the class ChannelTools method fastCopy.

/**
     * fast copy
     *
     * @param inputStream
     * @param outputStream
     * @throws IOException
     */
public static void fastCopy(InputStream inputStream, OutputStream outputStream) throws IOException {
    final ReadableByteChannel input = Channels.newChannel(inputStream);
    final WritableByteChannel output = Channels.newChannel(outputStream);
    fastCopy(input, output);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 13 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project AndroidAsync by koush.

the class NetworkEventReporterWrapper method interpretResponseEmitter.

public DataEmitter interpretResponseEmitter(final String requestId, @Nullable DataEmitter body, final boolean b64Encode) {
    final NetworkPeerManager peerManager = getPeerManagerIfEnabled();
    if (peerManager == null)
        return null;
    final WritableByteChannel channel;
    try {
        if (b64Encode) {
            final Base64OutputStream b64out = new Base64OutputStream(peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false), Base64.DEFAULT);
            channel = Channels.newChannel(b64out);
        } else {
            channel = ((FileOutputStream) peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false)).getChannel();
        }
    } catch (IOException e) {
        return null;
    }
    FilteredDataEmitter ret = new FilteredDataEmitter() {

        ByteBufferList pending = new ByteBufferList();

        @Override
        protected void report(Exception e) {
            super.report(e);
            StreamUtility.closeQuietly(channel);
            if (e == null)
                responseReadFinished(requestId);
            else
                responseReadFailed(requestId, e.toString());
        }

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            int amount = bb.remaining();
            ByteBuffer[] original = bb.getAllArray();
            ByteBuffer[] copy = new ByteBuffer[original.length];
            for (int i = 0; i < original.length; i++) {
                copy[i] = original[i].duplicate();
            }
            try {
                for (ByteBuffer c : copy) {
                    channel.write(c);
                }
            } catch (IOException ignored) {
                StreamUtility.closeQuietly(channel);
            }
            pending.addAll(original);
            dataReceived(requestId, amount, amount);
            super.onDataAvailable(emitter, pending);
        }
    };
    ret.setDataEmitter(body);
    return ret;
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) ByteBufferList(com.koushikdutta.async.ByteBufferList) NetworkPeerManager(com.facebook.stetho.inspector.network.NetworkPeerManager) DataEmitter(com.koushikdutta.async.DataEmitter) FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) Base64OutputStream(android.util.Base64OutputStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Example 14 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project AndroidAsync by koush.

the class StreamUtility method copyStream.

public static void copyStream(InputStream input, OutputStream output) throws IOException {
    final ReadableByteChannel inputChannel = Channels.newChannel(input);
    final WritableByteChannel outputChannel = Channels.newChannel(output);
    // copy the channels
    fastChannelCopy(inputChannel, outputChannel);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 15 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project neo4j by neo4j.

the class PageCacheTest method writingToClosedWritableByteChannelMustThrow.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void writingToClosedWritableByteChannelMustThrow() throws Exception {
    File file = file("a");
    configureStandardPageCache();
    try (PagedFile pf = pageCache.map(file, filePageSize)) {
        WritableByteChannel channel = pf.openWritableByteChannel();
        channel.close();
        expectedException.expect(ClosedChannelException.class);
        channel.write(ByteBuffer.allocate(recordSize));
        fail("That read should have thrown");
    }
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) WritableByteChannel(java.nio.channels.WritableByteChannel) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) File(java.io.File) Test(org.junit.Test)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)111 ByteBuffer (java.nio.ByteBuffer)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ReadableByteChannel (java.nio.channels.ReadableByteChannel)27 FileOutputStream (java.io.FileOutputStream)23 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Test (org.junit.Test)15 IOException (java.io.IOException)14 File (java.io.File)13 OutputStream (java.io.OutputStream)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)10 Vector (java.util.Vector)10 FileInputStream (java.io.FileInputStream)6 Writer (java.io.Writer)6 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)4 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)4 DbusEventsStatisticsCollector (com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 Map (java.util.Map)4