Examples of RandomAccessBuffer


Examples of freenet.support.api.RandomAccessBuffer

    /** Construct an instance of a given size.
     * @throws IOException */
    protected abstract RandomAccessBuffer construct(long size) throws IOException;
   
    private void innerTestSize(long sz) throws IOException {
        RandomAccessBuffer raf = construct(sz);
        assertEquals(raf.size(), sz);
        raf.close();
        raf.free();
    }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

        byte getByte(long offset);
    }
   
    /** Write using a given formula in random small writes, then check using random small reads. */
    protected void innerTestFormula(long sz, Random r, Formula f) throws IOException {
        RandomAccessBuffer raf = construct(sz);
        assertEquals(raf.size(), sz);
        int x = 0;
        // Write (and check as go)
        while(x < sz) {
            int maxRead = (int)Math.min(BUFFER_SIZE, sz - x);
            int toRead = maxRead == 1 ? 1 : r.nextInt(maxRead-1)+1;
            byte[] buf = new byte[toRead];
            for(int i=0;i<buf.length;i++) buf[i] = f.getByte(i+x);
            raf.pwrite(x, buf, 0, toRead);
            for(int i=0;i<buf.length;i++) buf[i] = (byte)~buf[i];
            raf.pread(x, buf, 0, toRead);
            for(int i=0;i<buf.length;i++) assertEquals(buf[i], f.getByte(i+x));
            x += toRead;
        }
        // Read
        while(x < sz) {
            int maxRead = (int)Math.min(BUFFER_SIZE, sz - x);
            int toRead = r.nextInt(maxRead-1)+1;
            byte[] buf = new byte[toRead];
            raf.pread(x, buf, 0, toRead);
            for(int i=0;i<buf.length;i++) assertEquals(buf[i], f.getByte(i+x));
            x += toRead;
        }
        x = 0;
        raf.close();
        raf.free();
    }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

            innerTestWriteOverLimit(size, size+1);
        }
    }
   
    private void innerTestWriteOverLimit(long sz, int choppedBytes) throws IOException {
        RandomAccessBuffer raf = construct(sz);
        assertEquals(raf.size(), sz);
        long startAt = sz - choppedBytes;
        byte[] buf = new byte[choppedBytes];
        if(sz != 0 && choppedBytes < sz) {
            if(startAt >= 0)
                readWriteMustSucceed(raf, startAt, buf, 0, buf.length); // Read, write up to the end work.
            else
                try {
                    readWriteMustSucceed(raf, startAt, buf, 0, buf.length); // Read, write up to the end work.
                    fail("Should fail to read at negative index");
                } catch (IllegalArgumentException e) {
                    // Ok.
                }
        }
        if(startAt+1 >= 0)
            readWriteMustFail(raf, startAt+1, buf, 0, buf.length); // Read, write over the end fail.
        else
            try {
                readWriteMustSucceed(raf, startAt+1, buf, 0, buf.length); // Read, write up to the end work.
                fail("Should fail to read at negative index");
            } catch (IllegalArgumentException e) {
                // Ok.
            }
        readWriteMustFail(raf, sz, buf, 0, buf.length); // Read, write at the end fail.
        readWriteMustFail(raf, sz+1, buf, 0, buf.length); // One byte into end
        readWriteMustFail(raf, sz+1025, buf, 0, buf.length); // 1KB in
        readWriteMustFail(raf, sz+buf.length, buf, 0, buf.length);
        raf.close();
        raf.free();
    }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

            innerTestClose(size);
    }
   
    /** Test that after closing a RandomAccessBuffer we cannot read from it or write to it */
    protected void innerTestClose(long sz) throws IOException {
        RandomAccessBuffer raf = construct(sz);
        raf.close();
        byte[] buf = new byte[(int)Math.min(1024, sz)];
        readWriteMustFail(raf, 0L, buf, 0, buf.length);
        raf.free();
    }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

     * RandomAccessBuffer, then read randomly and compare. */
    protected void innerTestArray(int len, Random r, boolean readOnly) throws IOException {
        if(len == 0) return;
        byte[] buf = new byte[len];
        r.nextBytes(buf);
        RandomAccessBuffer raf = construct(len);
        raf.pwrite(0L, buf, 0, buf.length);
        for(int i=0;i<100;i++) {
            int end = len == 1 ? 1 : r.nextInt(len)+1;
            int start = r.nextInt(end);
            checkArraySectionEqualsReadData(buf, raf, start, end, readOnly);
        }
        checkArraySectionEqualsReadData(buf, raf, 0, len, readOnly);
        if(len > 1)
            checkArraySectionEqualsReadData(buf, raf, 1, len-1, readOnly);
        raf.close();
        raf.free();
    }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

     * RandomAccessBuffer, then read randomly and compare. */
    protected void innerTestArrayMigration(int len, Random r) throws IOException {
        if(len == 0) return;
        byte[] buf = new byte[len];
        r.nextBytes(buf);
        RandomAccessBuffer raf = construct(len);
        TempRandomAccessBuffer t = (TempRandomAccessBuffer) raf;
        assertFalse(t.hasMigrated());
        assertEquals(factory.getRamUsed(), len);
        t.migrateToDisk();
        assertTrue(t.hasMigrated());
        assertEquals(factory.getRamUsed(), 0);
        raf.pwrite(0L, buf, 0, buf.length);
        checkArrayInner(buf, raf, len, r);
        raf.close();
        raf.free();
    }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

  }

  public int sendFileOffer(File file, String message) throws IOException {
    String fnam = file.getName();
    String mime = DefaultMIMETypes.guessMIMEType(fnam, false);
    RandomAccessBuffer data = new FileRandomAccessBuffer(file, true);
    return sendFileOffer(fnam, mime, message, data);
  }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

  }

  public int sendFileOffer(HTTPUploadedFile file, String message) throws IOException {
    String fnam = file.getFilename();
    String mime = file.getContentType();
    RandomAccessBuffer data = new ByteArrayRandomAccessBuffer(BucketTools.toByteArray(file.getData()));
    return sendFileOffer(fnam, mime, message, data);
  }
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

        }
        return;
      }
      byte[] data = baos.toByteArray();
      long uid = node.fastWeakRandom.nextLong();
      RandomAccessBuffer raf = new ByteArrayRandomAccessBuffer(data);
      PartiallyReceivedBulk prb = new PartiallyReceivedBulk(node.usm, data.length, Node.PACKET_SIZE, raf, true);
      try {
        sendAsync(DMT.createFNPMyFullNoderef(uid, data.length), null, node.nodeStats.foafCounter);
      } catch (NotConnectedException e1) {
        // Ignore
View Full Code Here

Examples of freenet.support.api.RandomAccessBuffer

      if(receivingFullNoderef) return; // DoS????
      receivingFullNoderef = true;
    }
    try {
      final byte[] data = new byte[length];
      RandomAccessBuffer raf = new ByteArrayRandomAccessBuffer(data);
      PartiallyReceivedBulk prb = new PartiallyReceivedBulk(node.usm, length, Node.PACKET_SIZE, raf, false);
      final BulkReceiver br = new BulkReceiver(prb, this, uid, node.nodeStats.foafCounter);
      node.executor.execute(new Runnable() {

        @Override
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.