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