Example usage for org.apache.cassandra.io.util RandomAccessReader skipBytes

List of usage examples for org.apache.cassandra.io.util RandomAccessReader skipBytes

Introduction

In this page you can find the example usage for org.apache.cassandra.io.util RandomAccessReader skipBytes.

Prototype

@Override
    public int skipBytes(int n) throws IOException 

Source Link

Usage

From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java

License:Apache License

@Test
public void testSkipBytes() throws IOException {
    SequentialWriter w = createTempFile("brafSkipBytes");
    w.write(generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE * 2));
    w.close();//from  ww w .ja v a  2  s.  c o  m

    RandomAccessReader file = RandomAccessReader.open(w, fs);

    file.seek(0); // back to the beginning of the file
    assertEquals(file.skipBytes(10), 10);
    assertEquals(file.bytesRemaining(), file.length() - 10);

    int initialPosition = (int) file.getFilePointer();
    // can't skip more than file size
    assertEquals(file.skipBytes((int) file.length() + 10), file.length() - initialPosition);
    assertEquals(file.getFilePointer(), file.length());
    assert file.bytesRemaining() == 0 && file.isEOF();

    file.seek(0);

    // skipping negative amount should return 0
    assertEquals(file.skipBytes(-1000), 0);
    assertEquals(file.getFilePointer(), 0);
    assertEquals(file.bytesRemaining(), file.length());

    file.close();
}

From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java

License:Apache License

@Test
public void testGetFilePointer() throws IOException {
    final SequentialWriter w = createTempFile("brafGetFilePointer");

    assertEquals(w.getFilePointer(), 0); // initial position should be 0

    w.write(generateByteArray(20));/* www.j  av  a2 s  .  c  om*/
    assertEquals(w.getFilePointer(), 20); // position 20 after writing 20 bytes

    w.sync();

    RandomAccessReader r = RandomAccessReader.open(w, fs);

    // position should change after skip bytes
    r.seek(0);
    r.skipBytes(15);
    assertEquals(r.getFilePointer(), 15);

    r.read();
    assertEquals(r.getFilePointer(), 16);
    r.read(new byte[4]);
    assertEquals(r.getFilePointer(), 20);

    w.close();
    r.close();
}

From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java

License:Apache License

@Test
public void testBytesRemaining() throws IOException {
    SequentialWriter w = createTempFile("brafBytesRemaining");

    int toWrite = RandomAccessReader.DEFAULT_BUFFER_SIZE + 10;

    w.write(generateByteArray(toWrite));

    w.sync();//w  w  w  . j  ava 2 s . co  m

    RandomAccessReader r = RandomAccessReader.open(w, fs);

    assertEquals(r.bytesRemaining(), toWrite);

    for (int i = 1; i <= r.length(); i++) {
        r.read();
        assertEquals(r.bytesRemaining(), r.length() - i);
    }

    r.seek(0);
    r.skipBytes(10);
    assertEquals(r.bytesRemaining(), r.length() - 10);

    w.close();
    r.close();
}

From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java

License:Apache License

@Test
public void testReadOnly() throws IOException {
    SequentialWriter file = createTempFile("brafReadOnlyTest");

    byte[] data = new byte[20];
    for (int i = 0; i < data.length; i++)
        data[i] = 'c';

    file.write(data);//from w ww . ja v a 2s. com
    file.sync(); // flushing file to the disk

    // read-only copy of the file, with fixed file length
    final RandomAccessReader copy = RandomAccessReader.open(new Path(file.getPath()), fs);

    copy.seek(copy.length());
    assertTrue(copy.bytesRemaining() == 0 && copy.isEOF());

    // can't seek past the end of the file for read-only files
    expectException(new Callable<Object>() {
        public Object call() {
            copy.seek(copy.length() + 1);
            return null;
        }
    }, IllegalArgumentException.class);

    // Any write() call should fail
    expectException(new Callable<Object>() {
        public Object call() throws IOException {
            copy.write(1);
            return null;
        }
    }, UnsupportedOperationException.class);

    expectException(new Callable<Object>() {
        public Object call() throws IOException {
            copy.write(new byte[1]);
            return null;
        }
    }, UnsupportedOperationException.class);

    expectException(new Callable<Object>() {
        public Object call() throws IOException {
            copy.write(new byte[3], 0, 2);
            return null;
        }
    }, UnsupportedOperationException.class);

    copy.seek(0);
    copy.skipBytes(5);

    assertEquals(copy.bytesRemaining(), 15);
    assertEquals(copy.getFilePointer(), 5);
    assertTrue(!copy.isEOF());

    copy.seek(0);
    ByteBuffer contents = copy.readBytes((int) copy.length());

    assertEquals(contents.limit(), copy.length());
    assertTrue(ByteBufferUtil.compare(contents, data) == 0);

    copy.seek(0);

    int count = 0;
    while (!copy.isEOF()) {
        assertEquals((byte) copy.read(), 'c');
        count++;
    }

    assertEquals(count, copy.length());

    copy.seek(0);
    byte[] content = new byte[10];
    copy.read(content);

    assertEquals(new String(content), "cccccccccc");

    file.close();
    copy.close();
}