Search in sources :

Example 61 with ByteBuffer

use of java.nio.ByteBuffer in project bazel by bazelbuild.

the class CentralDirectoryTest method testViewOf.

/**
   * Test of viewOf method, of class CentralDirectory.
   */
@Test
public void testViewOf() {
    ByteBuffer buffer = ByteBuffer.allocate(100).order(ByteOrder.LITTLE_ENDIAN);
    for (int i = 0; i < 100; i++) {
        buffer.put((byte) i);
    }
    buffer.position(20);
    buffer.limit(90);
    CentralDirectory view = CentralDirectory.viewOf(buffer);
    int expPos = 0;
    int expLimit = 90;
    // expect the buffer to have been reset to 0 (CentralDirectory does NOT slice).
    assertEquals("View not at position 0", expPos, view.buffer.position());
    assertEquals("Buffer not at position 0", expPos, buffer.position());
    assertEquals("Buffer limit changed", expLimit, view.buffer.limit());
    assertEquals("Buffer limit changed", expLimit, buffer.limit());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 62 with ByteBuffer

use of java.nio.ByteBuffer in project bazel by bazelbuild.

the class DirectoryEntryTest method testCopy.

/**
   * Test of copy method, of class DirectoryEntry.
   */
@Test
public void testCopy() {
    String filename = "pkg/foo.class";
    byte[] extraData = {};
    String comment = "always comment!";
    ByteBuffer buffer = ByteBuffer.allocate(100).order(ByteOrder.LITTLE_ENDIAN);
    DirectoryEntry view = DirectoryEntry.allocate(filename, extraData, comment);
    view.copy(buffer);
    int expSize = view.getSize();
    assertEquals("buffer not advanced as expected", expSize, buffer.position());
    buffer.position(0);
    DirectoryEntry clone = DirectoryEntry.viewOf(buffer);
    assertEquals("Fail to copy mark", view.get(CENSIG), clone.get(CENSIG));
    assertEquals("Fail to copy comment", view.getFilename(), clone.getFilename());
    Assert.assertArrayEquals("Fail to copy comment", view.getExtraData(), clone.getExtraData());
    assertEquals("Fail to copy comment", view.getComment(), clone.getComment());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 63 with ByteBuffer

use of java.nio.ByteBuffer in project bazel by bazelbuild.

the class DirectoryEntryTest method testView_4Args.

/**
   * Test of view method, of class DirectoryEntry.
   */
@Test
public void testView_4Args() {
    ByteBuffer buffer = ByteBuffer.allocate(100).order(ByteOrder.LITTLE_ENDIAN);
    for (int i = 0; i < 100; i++) {
        buffer.put((byte) i);
    }
    int offset = 20;
    buffer.position(offset);
    String filename = "pkg/foo.class";
    byte[] extraData = { 1, 2, 3, 4, 5 };
    String comment = "c";
    int expMark = (int) ZipInputStream.CENSIG;
    int expSize = 46 + filename.getBytes(UTF_8).length + extraData.length + comment.getBytes(UTF_8).length;
    int expPos = 0;
    DirectoryEntry view = DirectoryEntry.view(buffer, filename, extraData, comment);
    assertEquals("not based at current position", expMark, view.get(CENSIG));
    assertEquals("Not slice with position 0", expPos, view.buffer.position());
    assertEquals("Not sized with filename", expSize, view.getSize());
    assertEquals("Not limited to size", expSize, view.buffer.limit());
    assertEquals("Incorrect filename", filename, view.getFilename());
    Assert.assertArrayEquals("Incorrect extra data", extraData, view.getExtraData());
    assertEquals("Incorrect comment", comment, view.getComment());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 64 with ByteBuffer

use of java.nio.ByteBuffer in project bazel by bazelbuild.

the class LocalFileHeaderTest method testCopy.

@Test
public void testCopy() {
    ByteBuffer buffer = ByteBuffer.allocate(100).order(ByteOrder.LITTLE_ENDIAN);
    LocalFileHeader view = LocalFileHeader.allocate("pkg/foo.class", null);
    view.copy(buffer);
    int expSize = view.getSize();
    assertEquals("buffer not advanced as expected", expSize, buffer.position());
    buffer.position(0);
    LocalFileHeader clone = LocalFileHeader.viewOf(buffer);
    assertEquals("Fail to copy mark", view.get(LOCSIG), view.get(LOCSIG));
    assertEquals("Fail to copy comment", view.getFilename(), clone.getFilename());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 65 with ByteBuffer

use of java.nio.ByteBuffer in project bazel by bazelbuild.

the class ViewTest method testFileOffset.

@Test
public void testFileOffset() {
    ByteBuffer buffer = ByteBuffer.allocate(100);
    TestView instance = new TestView(buffer);
    long expResult = -1L;
    long result = instance.fileOffset();
    assertEquals("default file offset should be -1", expResult, result);
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8919 Test (org.junit.Test)1965 IOException (java.io.IOException)1022 ArrayList (java.util.ArrayList)450 File (java.io.File)224 FileChannel (java.nio.channels.FileChannel)214 MappedByteBuffer (java.nio.MappedByteBuffer)196 HashMap (java.util.HashMap)177 CharBuffer (java.nio.CharBuffer)153 ByteArrayOutputStream (java.io.ByteArrayOutputStream)146 InetSocketAddress (java.net.InetSocketAddress)143 Random (java.util.Random)127 InputStream (java.io.InputStream)125 Map (java.util.Map)119 FileInputStream (java.io.FileInputStream)116 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)99 Test (org.testng.annotations.Test)98 IntBuffer (java.nio.IntBuffer)95 SocketChannel (java.nio.channels.SocketChannel)94 FileOutputStream (java.io.FileOutputStream)93