Search in sources :

Example 76 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 77 with ReadableByteChannel

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();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) File(java.io.File) FileInputStream(java.io.FileInputStream) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Example 78 with ReadableByteChannel

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();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) File(java.io.File) FileInputStream(java.io.FileInputStream) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Example 79 with ReadableByteChannel

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();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 80 with ReadableByteChannel

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();
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)307 ByteBuffer (java.nio.ByteBuffer)111 IOException (java.io.IOException)84 FileOutputStream (java.io.FileOutputStream)62 WritableByteChannel (java.nio.channels.WritableByteChannel)62 Test (org.junit.Test)52 File (java.io.File)50 FileChannel (java.nio.channels.FileChannel)49 FileInputStream (java.io.FileInputStream)43 ByteArrayInputStream (java.io.ByteArrayInputStream)38 InputStream (java.io.InputStream)36 URL (java.net.URL)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Path (java.nio.file.Path)18 Test (org.testng.annotations.Test)14 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)11 MalformedURLException (java.net.MalformedURLException)11 Vector (java.util.Vector)11