Example usage for org.apache.cassandra.io.util SequentialWriter getPath

List of usage examples for org.apache.cassandra.io.util SequentialWriter getPath

Introduction

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

Prototype

public String getPath() 

Source Link

Usage

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

License:Apache License

@Test
public void testGetPath() throws IOException {
    SequentialWriter file = createTempFile("brafGetPath");
    assert file.getPath().contains("brafGetPath");
    file.close();//from w  w w.  j  a  va 2  s .  co m
}

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

License:Apache License

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

    byte[] data = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE + 20);

    w.write(data);/*ww w. j  av  a  2 s . c  o  m*/
    w.close(); // will flush

    final RandomAccessReader r = RandomAccessReader.open(new Path(new File(w.getPath()).getPath()), fs);

    r.close(); // closing to test read after close

    expectException(new Callable<Object>() {
        public Object call() {
            return r.read();
        }
    }, AssertionError.class);

    expectException(new Callable<Object>() {
        public Object call() throws IOException {
            w.write(generateByteArray(1));
            return null;
        }
    }, ClosedChannelException.class);

    RandomAccessReader copy = RandomAccessReader.open(new Path(new File(r.getPath()).getPath()), fs);
    ByteBuffer contents = copy.readBytes((int) copy.length());

    assertEquals(contents.limit(), data.length);
    assertEquals(ByteBufferUtil.compare(contents, data), 0);
}

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  w  w.  j  ava 2  s. c  o  m*/
    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();
}

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

License:Apache License

@VisibleForTesting
static RandomAccessReader open(SequentialWriter writer, FileSystem fs) {
    return open(new Path(writer.getPath()), DEFAULT_BUFFER_SIZE, false, null, fs);
}