Search in sources :

Example 16 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project spring-framework by spring-projects.

the class ResourceRegionEncoder method readResourceRegion.

private Flux<DataBuffer> readResourceRegion(ResourceRegion region, DataBufferFactory bufferFactory) {
    Resource resource = region.getResource();
    try {
        if (resource.isFile()) {
            File file = region.getResource().getFile();
            AsynchronousFileChannel channel = AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ);
            return DataBufferUtils.read(channel, region.getPosition(), bufferFactory, this.bufferSize);
        }
    } catch (IOException ignore) {
    // fallback to resource.readableChannel(), below
    }
    try {
        ReadableByteChannel channel = resource.readableChannel();
        Flux<DataBuffer> in = DataBufferUtils.read(channel, bufferFactory, this.bufferSize);
        return DataBufferUtils.skipUntilByteCount(in, region.getPosition());
    } catch (IOException ex) {
        return Flux.error(ex);
    }
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) ReadableByteChannel(java.nio.channels.ReadableByteChannel) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) File(java.io.File) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 17 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project spring-framework by spring-projects.

the class DataBufferUtils method read.

/**
	 * Read the given {@code InputStream} into a {@code Flux} of
	 * {@code DataBuffer}s. Closes the input stream when the flux is terminated.
	 * @param inputStream the input stream to read from
	 * @param dataBufferFactory the factory to create data buffers with
	 * @param bufferSize the maximum size of the data buffers
	 * @return a flux of data buffers read from the given channel
	 */
public static Flux<DataBuffer> read(InputStream inputStream, DataBufferFactory dataBufferFactory, int bufferSize) {
    Assert.notNull(inputStream, "InputStream must not be null");
    Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null");
    ReadableByteChannel channel = Channels.newChannel(inputStream);
    return read(channel, dataBufferFactory, bufferSize);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel)

Example 18 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project robovm by robovm.

the class ChannelsTest method testNewChannelInputStream.

/*
     * Test method for 'java.nio.channels.Channels.NewChannel'
     */
public void testNewChannelInputStream() throws IOException {
    int bufSize = 10;
    int readres = 0;
    byte[] byteArray = new byte[bufSize];
    ByteBuffer byteBuf = ByteBuffer.allocate(bufSize);
    this.fins = new FileInputStream(tmpFile);
    readres = this.fins.read(byteArray);
    assertEquals(bufSize, readres);
    assertFalse(0 == this.fins.available());
    ReadableByteChannel rbChannel = Channels.newChannel(this.fins);
    // fins still reads.
    assertFalse(0 == this.fins.available());
    readres = this.fins.read(byteArray);
    assertEquals(bufSize, readres);
    // rbChannel also reads.
    assertNotNull(rbChannel);
    readres = rbChannel.read(byteBuf);
    assertEquals(bufSize, readres);
    InputStream ins = Channels.newInputStream(rbChannel);
    assertNotNull(ins);
    assertEquals(0, ins.available());
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 19 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project robovm by robovm.

the class ChannelsTest method testNewReaderReadableByteChannelString.

/*
     * Test method for
     * 'java.nio.channels.Channels.newReader(ReadableByteChannel, String)'
     */
public void testNewReaderReadableByteChannelString() throws IOException {
    int bufSize = this.testNum;
    int readres = 0;
    CharBuffer charBuf = CharBuffer.allocate(bufSize);
    this.fins = new FileInputStream(tmpFile);
    ReadableByteChannel rbChannel = Channels.newChannel(this.fins);
    Reader testReader = Channels.newReader(rbChannel, Charset.forName(CODE_SET).newDecoder(), -1);
    //$NON-NLS-1$
    Reader testReader_s = Channels.newReader(rbChannel, CODE_SET);
    assertEquals(this.fileSize, this.fins.available());
    // not ready...
    assertFalse(testReader.ready());
    assertFalse(testReader_s.ready());
    // still reads
    readres = testReader.read(charBuf);
    assertEquals(bufSize, readres);
    assertEquals(0, this.fins.available());
    try {
        readres = testReader.read((CharBuffer) null);
        fail();
    } catch (NullPointerException e) {
    // correct
    }
    readres = testReader_s.read(charBuf);
    assertEquals(0, readres);
    assertTrue(testReader.ready());
    assertFalse(testReader_s.ready());
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) CharBuffer(java.nio.CharBuffer) Reader(java.io.Reader) FileInputStream(java.io.FileInputStream)

Example 20 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project robovm by robovm.

the class ChannelsTest method testNewChannelInputStream_BufferNull.

// test if buffer to read is null
public void testNewChannelInputStream_BufferNull() throws IOException {
    ByteBuffer byteBuf = ByteBuffer.allocate(this.testNum);
    int readres = this.testNum;
    this.fins = new FileInputStream(tmpFile);
    ReadableByteChannel rbChannel = Channels.newChannel(this.fins);
    assertNotNull(rbChannel);
    try {
        readres = rbChannel.read(null);
        fail();
    } catch (NullPointerException e) {
    // correct
    }
    assertEquals(this.testNum, readres);
    readres = 0;
    try {
        readres = rbChannel.read(byteBuf);
    } catch (NullPointerException e) {
        fail();
    }
    assertEquals(this.testNum, readres);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

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