use of java.nio.channels.ReadableByteChannel 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.ReadableByteChannel in project antlr4 by tunnelvisionlabs.
the class TestCharStreams method fromSMPUTF8SequenceStraddlingBufferBoundary.
@Test
public void fromSMPUTF8SequenceStraddlingBufferBoundary() throws Exception {
File p = folder.newFile();
Utils.writeFile(p, "hello \uD83C\uDF0E".getBytes(Charset.forName("UTF-8")));
ReadableByteChannel c = Channels.newChannel(new FileInputStream(p));
try {
CharStream s = CharStreams.fromChannel(c, // straddles the boundary of two buffers
8, CodingErrorAction.REPLACE, "foo");
assertEquals(7, s.size());
assertEquals(0, s.index());
assertEquals("hello \uD83C\uDF0E", s.toString());
} finally {
c.close();
}
}
use of java.nio.channels.ReadableByteChannel in project antlr4 by tunnelvisionlabs.
the class TestCharStreams method fromSMPUTF8ChannelHasExpectedSize.
@Test
public void fromSMPUTF8ChannelHasExpectedSize() throws Exception {
File p = folder.newFile();
Utils.writeFile(p, "hello \uD83C\uDF0E".getBytes(Charset.forName("UTF-8")));
ReadableByteChannel c = Channels.newChannel(new FileInputStream(p));
try {
CharStream s = CharStreams.fromChannel(c, 4096, CodingErrorAction.REPLACE, "foo");
assertEquals(7, s.size());
assertEquals(0, s.index());
assertEquals("hello \uD83C\uDF0E", s.toString());
assertEquals("foo", s.getSourceName());
} finally {
c.close();
}
}
use of java.nio.channels.ReadableByteChannel in project antlr4 by tunnelvisionlabs.
the class TestCharStreams method fromInvalidUTF8BytesThrowsInReportMode.
@Test
public void fromInvalidUTF8BytesThrowsInReportMode() throws Exception {
File p = folder.newFile();
byte[] toWrite = new byte[] { (byte) 0xCA, (byte) 0xFE };
Utils.writeFile(p, toWrite);
ReadableByteChannel c = Channels.newChannel(new FileInputStream(p));
try {
thrown.expect(CharacterCodingException.class);
CharStreams.fromChannel(c, 4096, CodingErrorAction.REPORT, "foo");
} finally {
c.close();
}
}
use of java.nio.channels.ReadableByteChannel in project serverless by bluenimble.
the class FileApiResource method pipe.
@Override
public void pipe(InputStream is, long position, long length) throws IOException {
if (!file.isFile()) {
throw new IOException("can't pipe a folder resource");
}
if (length == 0) {
return;
}
if (position < 0) {
position = 0;
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
FileChannel outChannel = out.getChannel();
if (length < 0) {
ReadableByteChannel inChannel = Channels.newChannel(is);
long skipped = IOUtils.skip(inChannel, position);
if (skipped < position) {
return;
}
ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);
while (inChannel.read(buffer) != -1) {
buffer.flip();
outChannel.write(buffer);
buffer.compact();
}
buffer.flip();
while (buffer.hasRemaining()) {
outChannel.write(buffer);
}
} else {
ReadableByteChannel inChannel = Channels.newChannel(is);
outChannel.transferFrom(inChannel, position, length);
}
} finally {
if (out != null) {
out.close();
}
}
}
Aggregations