use of java.nio.channels.ReadableByteChannel in project jetty.project by eclipse.
the class HttpOutputTest method testSendChannelBigChunked.
@Test
public void testSendChannelBigChunked() throws Exception {
Resource big = Resource.newClassPathResource("simple/big.txt");
final ReadableByteChannel channel = big.getReadableByteChannel();
_handler._contentChannel = new ReadableByteChannel() {
@Override
public boolean isOpen() {
return channel.isOpen();
}
@Override
public void close() throws IOException {
channel.close();
}
@Override
public int read(ByteBuffer dst) throws IOException {
int filled = 0;
if (dst.position() == 0 && dst.limit() > 2000) {
int limit = dst.limit();
dst.limit(2000);
filled = channel.read(dst);
dst.limit(limit);
} else
filled = channel.read(dst);
return filled;
}
};
LocalEndPoint endp = _connector.executeRequest("GET / HTTP/1.1\nHost: localhost:80\n\n" + "GET / HTTP/1.1\nHost: localhost:80\nConnection: close\n\n");
String response = endp.getResponse();
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Transfer-Encoding: chunked"));
assertThat(response, containsString("1\tThis is a big file"));
assertThat(response, containsString("400\tThis is a big file"));
assertThat(response, containsString("\r\n0\r\n"));
response = endp.getResponse();
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertThat(response, containsString("Connection: close"));
}
use of java.nio.channels.ReadableByteChannel in project deeplearning4j by deeplearning4j.
the class DoubleArrayTrie method read.
/**
* Load Stored data
*
* @param input input stream to read the double array trie from
* @return double array trie, not null
* @throws IOException if an IO error occured during reading the double array trie
*/
public static DoubleArrayTrie read(InputStream input) throws IOException {
DoubleArrayTrie trie = new DoubleArrayTrie();
DataInputStream dis = new DataInputStream(new BufferedInputStream(input));
trie.compact = dis.readBoolean();
// Read size of baseArr and checkArr
int baseCheckSize = dis.readInt();
// Read size of tailArr
int tailSize = dis.readInt();
ReadableByteChannel channel = Channels.newChannel(dis);
ByteBuffer tmpBaseBuffer = ByteBuffer.allocate(baseCheckSize * 4);
channel.read(tmpBaseBuffer);
tmpBaseBuffer.rewind();
trie.baseBuffer = tmpBaseBuffer.asIntBuffer();
ByteBuffer tmpCheckBuffer = ByteBuffer.allocate(baseCheckSize * 4);
channel.read(tmpCheckBuffer);
tmpCheckBuffer.rewind();
trie.checkBuffer = tmpCheckBuffer.asIntBuffer();
ByteBuffer tmpTailBuffer = ByteBuffer.allocate(tailSize * 2);
channel.read(tmpTailBuffer);
tmpTailBuffer.rewind();
trie.tailBuffer = tmpTailBuffer.asCharBuffer();
input.close();
return trie;
}
use of java.nio.channels.ReadableByteChannel in project deeplearning4j by deeplearning4j.
the class ByteBufferIO method read.
public static ByteBuffer read(InputStream input) throws IOException {
DataInputStream dataInput = new DataInputStream(new BufferedInputStream(input));
int size = dataInput.readInt();
ByteBuffer buffer = ByteBuffer.allocate(size);
ReadableByteChannel channel = Channels.newChannel(dataInput);
channel.read(buffer);
buffer.rewind();
return buffer;
}
use of java.nio.channels.ReadableByteChannel in project neo4j by neo4j.
the class PageCacheTest method readableByteChannelMustReadAllBytesInFile.
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void readableByteChannelMustReadAllBytesInFile() throws Exception {
File file = file("a");
generateFileWithRecords(file, recordCount, recordSize);
configureStandardPageCache();
try (PagedFile pf = pageCache.map(file, filePageSize);
ReadableByteChannel channel = pf.openReadableByteChannel()) {
verifyRecordsInFile(channel, recordCount);
}
}
use of java.nio.channels.ReadableByteChannel in project neo4j by neo4j.
the class PageCacheTest method readableByteChannelMustBeOpenUntilClosed.
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void readableByteChannelMustBeOpenUntilClosed() throws Exception {
configureStandardPageCache();
try (PagedFile pf = pageCache.map(file("a"), filePageSize)) {
ReadableByteChannel channel;
try (ReadableByteChannel ch = pf.openReadableByteChannel()) {
assertTrue(ch.isOpen());
channel = ch;
}
assertFalse(channel.isOpen());
}
}
Aggregations