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);
}
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);
}
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;
}
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);
}
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");
}
}
Aggregations