Java Code Examples for java.io.RandomAccessFile#writeShort()

The following examples show how to use java.io.RandomAccessFile#writeShort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TreeLookupOverflowNode.java    From BPlusTree with Apache License 2.0 6 votes vote down vote up
/**
 * Write a lookup page overflow to the page index; the node should
 * have the following structure:
 * <p>
 * -- node type -- (2 bytes)
 * -- next pointer -- (8 bytes)
 * -- current capacity -- (4 bytes)
 * <p>
 * -- page indexes (in place of keys) (8 bytes)
 *
 * @param r     an *already* open pointer which points to our B+ Tree file
 * @param conf  B+ Tree configuration
 * @param bPerf instance of performance counter class
 * @throws IOException is thrown when an I/O operation fails
 */
@Override
public void writeNode(RandomAccessFile r,
                      BPlusConfiguration conf,
                      BPlusTreePerformanceCounter bPerf)
        throws IOException {

    // account for the header page as well
    r.seek(getPageIndex());

    // write the node type
    r.writeShort(getPageType());

    // write the next pointer
    r.writeLong(next);

    // write current capacity
    r.writeInt(getCurrentCapacity());

    // now write the index values
    for (int i = 0; i < getCurrentCapacity(); i++) {
        r.writeLong(getKeyAt(i));
    }

}
 
Example 2
Source File: Paged.java    From btree4j with Apache License 2.0 5 votes vote down vote up
protected void write(RandomAccessFile raf) throws IOException {
    raf.writeShort(_fhSize);
    raf.writeInt(_pageSize);
    raf.writeLong(_totalPageCount);
    raf.writeLong(_firstFreePage);
    raf.writeLong(_lastFreePage);
    raf.writeByte(_pageHeaderSize);
}
 
Example 3
Source File: DirectoryChunk.java    From KorgPackage with GNU General Public License v3.0 5 votes vote down vote up
public void save(RandomAccessFile writer) throws IOException {
    writer.writeInt(Integer.reverseBytes(id));
    long offset = writer.getFilePointer();
    writer.write(new byte[4]);
    writer.writeShort(Short.reverseBytes(owner));
    writer.writeShort(Short.reverseBytes(group));
    writer.writeShort(Short.reverseBytes(attributes));
    writer.writeShort(Short.reverseBytes(condition));
    writeString(writer, path);
    int size = (int) (writer.getFilePointer() - offset - 4);
    writer.seek(offset);
    writer.writeInt(Integer.reverseBytes(size));
}
 
Example 4
Source File: TreeLeaf.java    From BPlusTree with Apache License 2.0 5 votes vote down vote up
/**
 *
 * Leaf node write structure is as follows:
 *
 *  -- node type -- (2 bytes)
 *  -- next pointer -- (8 bytes)
 *  -- prev pointer -- (8 bytes)
 *  -- key/value pairs -- (max size * (key size + satellite size))
 *
 * @param r pointer to *opened* B+ tree file
 * @param conf configuration parameter
 * @throws IOException is thrown when an I/O operation fails
 */
@Override
public void writeNode(RandomAccessFile r, BPlusConfiguration conf,
                      BPlusTreePerformanceCounter bPerf)
        throws IOException {

    // update root index in the file
    if(this.isRoot()) {
        r.seek(conf.getHeaderSize()-16L);
        r.writeLong(getPageIndex());
    }

    // account for the header page as well.
    r.seek(getPageIndex());

    // now write the node type
    r.writeShort(getPageType());

    // write the next pointer
    r.writeLong(nextPagePointer);

    // write the prev pointer
    r.writeLong(prevPagePointer);

    // then write the current capacity
    r.writeInt(getCurrentCapacity());

    // now write the Key/Value pairs
    for(int i = 0; i < getCurrentCapacity(); i++) {
        r.writeLong(getKeyAt(i));
        r.writeLong(getOverflowPointerAt(i));
        r.write(valueList.get(i).getBytes(StandardCharsets.UTF_8));
    }

    // annoying correction
    if(r.length() < getPageIndex()+conf.getPageSize())
        {r.setLength(getPageIndex()+conf.getPageSize());}

    bPerf.incrementTotalLeafNodeWrites();
}
 
Example 5
Source File: TreeOverflow.java    From BPlusTree with Apache License 2.0 5 votes vote down vote up
/**
 *
 * Overflow node write structure is as follows:
 *
 *  -- node type -- (2 bytes)
 *  -- next pointer -- (8 bytes)
 *  -- prev pointer -- (8 bytes)
 *  -- values -- (max size * satellite size)
 *
 * @param r pointer to *opened* B+ tree file
 * @throws IOException is thrown when an I/O operation fails
 */
@Override
public void writeNode(RandomAccessFile r, BPlusConfiguration conf,
                      BPlusTreePerformanceCounter bPerf)
        throws IOException {
    // account for the header page as well.
    r.seek(getPageIndex());

    // now write the node type
    r.writeShort(getPageType());

    // write the next pointer
    r.writeLong(nextPagePointer);

    // write the prev pointer
    r.writeLong(prevPagePointer);

    // then write the current capacity
    r.writeInt(getCurrentCapacity());

    // now write the values
    for(int i = 0; i < getCurrentCapacity(); i++)
        {r.write(valueList.get(i).getBytes(StandardCharsets.UTF_8));}

    // annoying correction
    if(r.length() < getPageIndex()+conf.getPageSize())
        {r.setLength(getPageIndex()+conf.getPageSize());}

    bPerf.incrementTotalOverflowNodeWrites();
}
 
Example 6
Source File: TreeInternalNode.java    From BPlusTree with Apache License 2.0 5 votes vote down vote up
/**
 *
 *  Internal node structure is as follows:
 *
 *  -- node type -- (2 bytes)
 *  -- current capacity -- (4 bytes)
 *
 *  -- Key -- (8 bytes max size)
 *
 *  -- Pointers (8 bytes max size + 1)
 *
 *  we go like: k1 -- p0 -- k2 -- p1 ... kn -- pn+1
 *
 * @param r pointer to *opened* B+ tree file
 * @throws IOException is thrown when an I/O exception is captured.
 */
@Override
public void writeNode(RandomAccessFile r, BPlusConfiguration conf,
                      BPlusTreePerformanceCounter bPerf)
        throws IOException {

    // update root index in the file
    if(this.isRoot()) {
        r.seek(conf.getHeaderSize()-8);
        r.writeLong(getPageIndex());
    }

    // account for the header page as well.
    r.seek(getPageIndex());

    // write the node type
    r.writeShort(getPageType());

    // write current capacity
    r.writeInt(getCurrentCapacity());

    // now write Key/Pointer pairs
    for(int i = 0; i < getCurrentCapacity(); i++) {
        r.writeLong(getKeyAt(i));       // Key
        r.writeLong(getPointerAt(i));   // Pointer
    }
    // final pointer.
    r.writeLong(getPointerAt(getCurrentCapacity()));

    // annoying correction
    if(r.length() < getPageIndex()+conf.getPageSize())
        {r.setLength(getPageIndex()+conf.getPageSize());}

    bPerf.incrementTotalInternalNodeWrites();
}
 
Example 7
Source File: FrameBodyASPI.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void write(final RandomAccessFile file) throws IOException {
    writeHeader(file, getSize());
    file.writeInt(dataStart);
    file.writeInt(dataLength);
    file.writeShort(indexPoints);
    file.writeByte(16);
    for (int i = 0; i < indexPoints; i++) {
        file.writeShort((int) fraction[i]);
    }
}
 
Example 8
Source File: RandomAccessFileTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.io.RandomAccessFile#readShort()
 */
public void test_readShort() throws IOException {
    // Test for method short java.io.RandomAccessFile.readShort()
    RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
    raf.writeShort(Short.MIN_VALUE);
    raf.seek(0);
    assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
            .readShort());
    raf.close();
}
 
Example 9
Source File: RandomAccessFileTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.io.RandomAccessFile#readUnsignedShort()
 */
public void test_readUnsignedShort() throws IOException {
    // Test for method int java.io.RandomAccessFile.readUnsignedShort()
    RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
    raf.writeShort(-1);
    raf.seek(0);
    assertEquals("Incorrect byte read/written", 65535, raf
            .readUnsignedShort());
    raf.close();
}
 
Example 10
Source File: RandomAccessFileTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.io.RandomAccessFile#writeShort(int)
 */
public void test_writeShortI() throws IOException {
    // Test for method void java.io.RandomAccessFile.writeShort(int)
    RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
    raf.writeShort(Short.MIN_VALUE);
    raf.seek(0);
    assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
            .readShort());
    raf.close();
}