Package freenet.support.api

Examples of freenet.support.api.Bucket


  public URIFeedMessage(String header, String shortText, String text, short priorityClass, long updatedTime,
      String sourceNodeName, long composed, long sent, long received,
      FreenetURI URI, String description) {
    super(header, shortText, text, priorityClass, updatedTime, sourceNodeName, composed, sent, received);
    this.URI = URI;
    final Bucket descriptionBucket;
    try {
      if(description != null)
        descriptionBucket = new ArrayBucket(description.getBytes("UTF-8"));
      else
        descriptionBucket = new NullBucket();
View Full Code Here


  }
 
  @Override
  protected void writeData(OutputStream os) throws IOException {
      for(Map.Entry<String, Bucket> entry : buckets.entrySet()) {
        Bucket bucket = entry.getValue();
        BucketTools.copyTo(bucket, os, bucket.size());
        if(freeOnSent) bucket.free(); // Always transient so no removeFrom() needed.
      }
  }
View Full Code Here

  public SimpleFieldSet getFieldSet() {
    int dataLength = 0;
    SimpleFieldSet fs = new SimpleFieldSet(true);
    for(Map.Entry<String, Bucket> entry : buckets.entrySet()) {
      String field = entry.getKey();
      Bucket bucket = entry.getValue();
      fs.put(field + "Length", bucket.size());
      dataLength += bucket.size();
    }
    fs.put("DataLength", dataLength);
    return fs;
  }
View Full Code Here

  protected abstract Bucket makeBucket(long size) throws IOException;

  protected abstract void freeBucket(Bucket bucket) throws IOException;

  public void testReadEmpty() throws IOException {
    Bucket bucket = makeBucket(3);
    try {
      assertEquals("Size-0", 0, bucket.size());
      OutputStream os = bucket.getOutputStream();
      os.close();
     
      // Read byte[]
      InputStream is = bucket.getInputStream();
      byte[] data = new byte[10];
      int read = is.read(data, 0, 10);
      is.close();

      assertEquals("Read-Empty", -1, read);
View Full Code Here

      freeBucket(bucket);
    }
  }

  public void testReadExcess() throws IOException {
    Bucket bucket = makeBucket(Math.max(DATA1.length, DATA2.length));
    try {
      assertEquals("Size-0", 0, bucket.size());

      // Write
      OutputStream os = bucket.getOutputStream();
      os.write(new byte[] { 5 });
      os.close();

      assertEquals("Read-Excess-Size", 1, bucket.size());

      // Read byte[]
      InputStream is = bucket.getInputStream();
      byte[] data = new byte[10];
      int read = is.read(data, 0, 10);
      assertEquals("Read-Excess", 1, read);
      assertEquals("Read-Excess-5", 5, data[0]);
View Full Code Here

      freeBucket(bucket);
    }
  }

  public void testReadWrite() throws IOException {
    Bucket bucket = makeBucket(Math.max(DATA1.length, DATA2.length));
    try {
      assertEquals("Size-0", 0, bucket.size());

      // Write
      OutputStream os = bucket.getOutputStream();
      os.write(DATA1);
      os.close();

      assertEquals("Size-1", DATA1.length, bucket.size());

      // Read byte[]
      InputStream is = bucket.getInputStream();
      byte[] data = new byte[DATA1.length];
      int read = is.read(data, 0, DATA1.length);
      is.close();

      assertEquals("SimpleRead-1-SIZE", DATA1.length, read);
      assertEquals("SimpleRead-1", new ByteArrayWrapper(DATA1), new ByteArrayWrapper(data));

      // Read byte
      is = bucket.getInputStream();
      for (byte b : DATA1)
        assertEquals("SimpleRead-2", b, (byte) is.read());

      // EOF
      assertEquals("SimpleRead-EOF0", -1, is.read(new byte[4]));
View Full Code Here

  // Write twice -- should overwrite, not append
  public void testReuse() throws IOException {
    if (!canOverwrite)
      return;
   
    Bucket bucket = makeBucket(Math.max(DATA1.length, DATA2.length));
    try {
      // Write
      OutputStream os = bucket.getOutputStream();
      os.write(DATA1);
      os.close();

      // Read byte[]
      InputStream is = bucket.getInputStream();
      byte[] data = new byte[DATA1.length];
      int read = is.read(data, 0, DATA1.length);
      is.close();

      assertEquals("Read-1-SIZE", DATA1.length, read);
      assertEquals("Read-1", new ByteArrayWrapper(DATA1), new ByteArrayWrapper(data));

      // Write again
      os = bucket.getOutputStream();
      os.write(DATA2);
      os.close();

      // Read byte[]
      is = bucket.getInputStream();
      data = new byte[DATA2.length];
      read = is.read(data, 0, DATA2.length);
      is.close();

      assertEquals("Read-2-SIZE", DATA2.length, read);
View Full Code Here

      freeBucket(bucket);
    }
  }

  public void testNegative() throws IOException {
    Bucket bucket = makeBucket(Math.max(DATA1.length, DATA2.length));
    try {
      // Write
      OutputStream os = bucket.getOutputStream();
      os.write(0);
      os.write(-1);
      os.write(-2);
      os.write(123);
      os.close();

      // Read byte[]
      InputStream is = bucket.getInputStream();
      assertEquals("Write-0", 0xff & (byte) 0, is.read());
      assertEquals("Write-1", 0xff & (byte) -1, is.read());
      assertEquals("Write-2", 0xff & (byte) -2, is.read());
      assertEquals("Write-123", 0xff & (byte) 123, is.read());
      assertEquals("EOF", -1, is.read());
View Full Code Here

      DATA_LONG[i] = (byte) i;
  }

  public void testLargeData() throws IOException {

    Bucket bucket = makeBucket(DATA_LONG.length * 16);
    try {
      // Write
      OutputStream os = bucket.getOutputStream();
      for (int i = 0; i < 16; i++)
        os.write(DATA_LONG);
      os.close();

      // Read byte[]
      DataInputStream is = new DataInputStream(bucket.getInputStream());
      for (int i = 0; i < 16; i++) {
        byte[] buf = new byte[DATA_LONG.length];
        is.readFully(buf);
        assertEquals("Read-Long", new ByteArrayWrapper(DATA_LONG), new ByteArrayWrapper(buf));
      }
View Full Code Here

    File blobFile = null;
    synchronized(this) {
      if(fetchedVersion <= this.fetchedVersion) {
        tempBlobFile.delete();
        if(result != null) {
          Bucket toFree = result.asBucket();
          if(toFree != null)
            toFree.free();
        }
        return;
      }
      if(result == null || result.asBucket() == null || result.asBucket().size() == 0) {
        tempBlobFile.delete();
View Full Code Here

TOP

Related Classes of freenet.support.api.Bucket

Copyright © 2018 www.massapicom. 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.