Search in sources :

Example 1 with ReadableByteChannel

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"));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) Resource(org.eclipse.jetty.util.resource.Resource) IOException(java.io.IOException) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) LocalEndPoint(org.eclipse.jetty.server.LocalConnector.LocalEndPoint) Test(org.junit.Test)

Example 2 with ReadableByteChannel

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

Example 3 with ReadableByteChannel

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

Example 4 with ReadableByteChannel

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

Example 5 with ReadableByteChannel

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());
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) Test(org.junit.Test)

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