Search in sources :

Example 56 with ByteBuffer

use of java.nio.ByteBuffer in project jvm-tools by aragozin.

the class ByteBufferPageManager method accurePage.

@Override
public ByteBuffer accurePage() throws NoMorePagesException {
    if (freePages.isEmpty()) {
        refill();
    }
    if (freePages.isEmpty()) {
        throw new NoMorePagesException();
    }
    Iterator<BW> it = freePages.iterator();
    ByteBuffer bb = it.next().pageBuffer;
    it.remove();
    return bb;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 57 with ByteBuffer

use of java.nio.ByteBuffer in project jvm-tools by aragozin.

the class PagedVirtualMemory method ensureBuffer.

private ByteBuffer ensureBuffer(long index) {
    if (index > SANITY_LIMIT) {
        throw new IllegalArgumentException("Offset beyond sanity: " + index);
    }
    int n = (int) (index >> pageBits);
    if (n >= bufferMap.length) {
        int nsize = Math.max(n + 1, (3 * bufferMap.length) / 4);
        bufferMap = Arrays.copyOf(bufferMap, nsize);
        hitCounts = Arrays.copyOf(hitCounts, nsize);
        pageMap = Arrays.copyOf(pageMap, nsize);
    }
    ByteBuffer b = bufferMap[n];
    if (b == null) {
        pageFault(n);
        b = bufferMap[n];
        if (b == null) {
            throw new IllegalArgumentException("Failed to load page #" + n);
        }
    }
    return b;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 58 with ByteBuffer

use of java.nio.ByteBuffer in project disunity by ata4.

the class SerializedFileReader method readObjects.

private void readObjects(DataReader in) throws IOException {
    long ofsMin = Long.MAX_VALUE;
    long ofsMax = Long.MIN_VALUE;
    SerializedFileHeader header = serialized.header();
    SerializedFileMetadata metadata = serialized.metadata();
    Map<Long, ObjectInfo> objectInfoMap = metadata.objectInfoTable().infoMap();
    Map<Integer, TypeRoot<Type>> typeTreeMap = metadata.typeTree().typeMap();
    List<SerializedObjectData> objectData = serialized.objectData();
    for (Map.Entry<Long, ObjectInfo> infoEntry : objectInfoMap.entrySet()) {
        ObjectInfo info = infoEntry.getValue();
        long id = infoEntry.getKey();
        long ofs = header.dataOffset() + info.offset();
        ofsMin = Math.min(ofsMin, ofs);
        ofsMax = Math.max(ofsMax, ofs + info.length());
        SerializedObjectData object = new SerializedObjectData(id);
        object.info(info);
        // create and read object data buffer
        ByteBuffer buf = ByteBufferUtils.allocate((int) info.length());
        in.position(ofs);
        in.readBuffer(buf);
        object.buffer(buf);
        // get type tree if possible
        TypeRoot typeRoot = typeTreeMap.get(info.typeID());
        if (typeRoot != null) {
            object.typeTree(typeRoot.nodes());
        }
        objectData.add(object);
    }
    DataBlock objectDataBlock = serialized.objectDataBlock();
    objectDataBlock.offset(ofsMin);
    objectDataBlock.endOffset(ofsMax);
    L.log(Level.FINER, "objectDataBlock: {0}", objectDataBlock);
}
Also used : ByteBuffer(java.nio.ByteBuffer) ObjectInfo(info.ata4.junity.serialize.objectinfo.ObjectInfo) DataBlock(info.ata4.util.io.DataBlock) TypeRoot(info.ata4.junity.serialize.typetree.TypeRoot) Map(java.util.Map)

Example 59 with ByteBuffer

use of java.nio.ByteBuffer in project disunity by ata4.

the class SerializedFileWriter method writeObjects.

private void writeObjects(DataWriter out) throws IOException {
    long ofsMin = Long.MAX_VALUE;
    long ofsMax = Long.MIN_VALUE;
    for (SerializedObjectData data : serialized.objectData()) {
        ByteBuffer bb = data.buffer();
        bb.rewind();
        out.align(8);
        ofsMin = Math.min(ofsMin, out.position());
        ofsMax = Math.max(ofsMax, out.position() + bb.remaining());
        ObjectInfo info = data.info();
        info.offset(out.position() - serialized.header().dataOffset());
        info.length(bb.remaining());
        out.writeBuffer(bb);
    }
    DataBlock objectDataBlock = serialized.objectDataBlock();
    objectDataBlock.offset(ofsMin);
    objectDataBlock.endOffset(ofsMax);
    L.log(Level.FINER, "objectDataBlock: {0}", objectDataBlock);
}
Also used : ByteBuffer(java.nio.ByteBuffer) ObjectInfo(info.ata4.junity.serialize.objectinfo.ObjectInfo) DataBlock(info.ata4.util.io.DataBlock)

Example 60 with ByteBuffer

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

the class CentralDirectoryTest method testParse.

/**
   * Test of parse method, of class CentralDirectory.
   */
@Test
public void testParse() {
    // First fill it with some entries
    ByteBuffer inputBuffer = ByteBuffer.allocate(10000).order(ByteOrder.LITTLE_ENDIAN);
    String comment = null;
    byte[] extra = null;
    String filename = "pkg/0.txt";
    DirectoryEntry entry = DirectoryEntry.view(inputBuffer, filename, extra, comment);
    int expSize = entry.getSize();
    comment = "";
    extra = new byte[] {};
    for (int i = 1; i < 20; i++) {
        filename = "pkg/" + i + ".txt";
        entry = DirectoryEntry.view(inputBuffer, filename, extra, comment);
        expSize += entry.getSize();
        extra = new byte[extra.length + 1];
        comment = comment + "," + i;
    }
    // Parse the entries.
    CentralDirectory cdir = CentralDirectory.viewOf(inputBuffer).at(0).parse();
    assertEquals("Count", 20, cdir.getCount());
    assertEquals("Position after parse", expSize, cdir.buffer.position());
    assertEquals("Limit after parse", 10000, cdir.buffer.limit());
    cdir.buffer.flip();
    assertEquals("Position after finish", 0, cdir.buffer.position());
    assertEquals("Limit after finish", expSize, cdir.buffer.limit());
}
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