Package freenet.support.io

Examples of freenet.support.io.CountedOutputStream


        f.inc(2, 2);
        assertEquals(fixedLength, getStoredLength(f));
    }

    private int getStoredLength(FailureCodeTracker f) throws IOException {
        CountedOutputStream os = new CountedOutputStream(new NullOutputStream());
        DataOutputStream dos = new DataOutputStream(os);
        f.writeFixedLengthTo(dos);
        dos.close();
        return (int) os.written();
    }
View Full Code Here


    }
    return baos.toByteArray();
  }
 
    public long writtenLength() throws MetadataUnresolvedException {
        CountedOutputStream cos = new CountedOutputStream(new NullOutputStream());
        try {
            writeTo(new DataOutputStream(cos));
        } catch (IOException e) {
            throw new Error("Could not write to CountedOutputStream: "+e, e);
        }
        return cos.written();
    }
View Full Code Here

  }
 
  @Override
  public long compress(InputStream is, OutputStream os, long maxReadLength, long maxWriteLength) throws IOException, CompressionOutputSizeException {
    CountedInputStream cis = null;
    CountedOutputStream cos = null;
    cis = new CountedInputStream(is);
    cos = new CountedOutputStream(os);
    Encoder encoder = new Encoder();
        encoder.SetEndMarkerMode( true );
        // Dictionary size 1MB, this is equivalent to lzma -4, it uses 16MB to compress and 2MB to decompress.
        // Next one up is 2MB = -5 = 26M compress, 3M decompress.
        encoder.SetDictionarySize( 1 << 20 );
        // enc.WriteCoderProperties( out );
        // 5d 00 00 10 00
        encoder.Code( cis, cos, -1, -1, null );
    if(logMINOR)
      Logger.minor(this, "Read "+cis.count()+" written "+cos.written());
    if(cos.written() > maxWriteLength)
      throw new CompressionOutputSizeException();
    cos.flush();
    return cos.written();
  }
View Full Code Here

        props[4] = 0x00;
    }

  @Override
  public long decompress(InputStream is, OutputStream os, long maxLength, long maxCheckSizeBytes) throws IOException, CompressionOutputSizeException {
    CountedOutputStream cos = new CountedOutputStream(os);
    Decoder decoder = new Decoder();
    decoder.SetDecoderProperties(props);
    decoder.Code(is, cos, maxLength);
    return cos.written();
  }
View Full Code Here

  }
 
  @Override
  public long compress(InputStream is, OutputStream os, long maxReadLength, long maxWriteLength) throws IOException, CompressionOutputSizeException {
    CountedInputStream cis = null;
    CountedOutputStream cos = null;
    cis = new CountedInputStream(is);
    cos = new CountedOutputStream(os);
    Encoder encoder = new Encoder();
        encoder.SetEndMarkerMode( true );
        int dictionarySize = 1;
        if(maxReadLength == Long.MAX_VALUE || maxReadLength < 0) {
          dictionarySize = MAX_DICTIONARY_SIZE;
          Logger.error(this, "No indication of size, having to use maximum dictionary size", new Exception("debug"));
        } else {
          while(dictionarySize < maxReadLength && dictionarySize < MAX_DICTIONARY_SIZE)
            dictionarySize <<= 1;
        }
        encoder.SetDictionarySize( dictionarySize );
        encoder.WriteCoderProperties(os);
        encoder.Code( cis, cos, maxReadLength, maxWriteLength, null );
    if(cos.written() > maxWriteLength)
      throw new CompressionOutputSizeException(cos.written());
        cos.flush();
    if(logMINOR)
      Logger.minor(this, "Read "+cis.count()+" written "+cos.written());
    return cos.written();
  }
View Full Code Here

  @Override
  public long decompress(InputStream is, OutputStream os, long maxLength, long maxCheckSizeBytes) throws IOException, CompressionOutputSizeException {
    byte[] props = new byte[5];
    DataInputStream dis = new DataInputStream(is);
    dis.readFully(props);
    CountedOutputStream cos = new CountedOutputStream(os);
   
    int dictionarySize = 0;
    for (int i = 0; i < 4; i++)
      dictionarySize += ((props[1 + i]) & 0xFF) << (i * 8);
   
    if(dictionarySize < 0) throw new InvalidCompressedDataException("Invalid dictionary size");
    if(dictionarySize > MAX_DICTIONARY_SIZE) throw new TooBigDictionaryException();
    Decoder decoder = new Decoder();
    if(!decoder.SetDecoderProperties(props)) throw new InvalidCompressedDataException("Invalid properties");
    decoder.Code(is, cos, maxLength);
    //cos.flush();
    return cos.written();
  }
View Full Code Here

        this.splitfileCryptoKey = splitfileCryptoKey;
        crossDataBlocksAllocated = new boolean[dataBlocks + crossCheckBlocks];
        blockChooser = new SplitFileInserterSegmentBlockChooser(this, totalBlockCount, random,
                maxRetries, keysFetching, consecutiveRNFsCountAsSuccess);
        try {
            CountedOutputStream cos = new CountedOutputStream(new NullOutputStream());
            DataOutputStream dos = new DataOutputStream(cos);
            innerStoreStatus(dos);
            dos.close();
            statusLength = (int) cos.written() + parent.checker.checksumLength();
        } catch (IOException e) {
            throw new Error(e); // Impossible
        }
    }
View Full Code Here

        this.splitfileCryptoKey = splitfileCryptoKey;
        crossDataBlocksAllocated = new boolean[dataBlockCount + crossCheckBlockCount];
        blockChooser = new SplitFileInserterSegmentBlockChooser(this, totalBlockCount, random,
                maxRetries, keysFetching, consecutiveRNFsCountAsSuccess);
        try {
            CountedOutputStream cos = new CountedOutputStream(new NullOutputStream());
            DataOutputStream dos = new DataOutputStream(cos);
            innerStoreStatus(dos);
            dos.close();
            int minStatusLength = (int) cos.written() + parent.checker.checksumLength();
            if(minStatusLength > statusLength)
                throw new StorageFormatException("Bad status length (too short)");
        } catch (IOException e) {
            throw new Error(e); // Impossible
        }
View Full Code Here

  public long compress(InputStream is, OutputStream os, long maxReadLength, long maxWriteLength) throws IOException, CompressionOutputSizeException {
    if(maxReadLength <= 0)
      throw new IllegalArgumentException();
    BZip2CompressorOutputStream bz2os = null;
    try {
      CountedOutputStream cos = new CountedOutputStream(os);
      bz2os = new BZip2CompressorOutputStream(HeaderStreams.dimOutput(BZ_HEADER, cos));
      long read = 0;
      // Bigger input buffer, so can compress all at once.
      // Won't hurt on I/O either, although most OSs will only return a page at a time.
      byte[] buffer = new byte[32768];
      while(true) {
        int l = (int) Math.min(buffer.length, maxReadLength - read);
        int x = l == 0 ? -1 : is.read(buffer, 0, buffer.length);
        if(x <= -1) break;
        if(x == 0) throw new IOException("Returned zero from read()");
        bz2os.write(buffer, 0, x);
        read += x;
        if(cos.written() > maxWriteLength)
          throw new CompressionOutputSizeException();
      }
      bz2os.flush();
      cos.flush();
      bz2os.close();
      bz2os = null;
      if(cos.written() > maxWriteLength)
        throw new CompressionOutputSizeException();
      return cos.written();
    } finally {
      if(bz2os != null) {
        bz2os.flush();
        bz2os.close();
      }
View Full Code Here

  @Override
  public long compress(InputStream is, OutputStream os, long maxReadLength, long maxWriteLength) throws IOException, CompressionOutputSizeException {
    if(maxReadLength < 0)
      throw new IllegalArgumentException();
    GZIPOutputStream gos = null;
    CountedOutputStream cos = new CountedOutputStream(os);
    try {
      gos = new GZIPOutputStream(cos);
      long read = 0;
      // Bigger input buffer, so can compress all at once.
      // Won't hurt on I/O either, although most OSs will only return a page at a time.
      byte[] buffer = new byte[32768];
      while(true) {
        int l = (int) Math.min(buffer.length, maxReadLength - read);
        int x = l == 0 ? -1 : is.read(buffer, 0, l);
        if(x <= -1) break;
        if(x == 0) throw new IOException("Returned zero from read()");
        gos.write(buffer, 0, x);
        read += x;
        if(cos.written() > maxWriteLength)
          throw new CompressionOutputSizeException();
      }
      gos.flush();
      gos.finish();
      cos.flush();
      gos = null;
      if(cos.written() > maxWriteLength)
        throw new CompressionOutputSizeException();
      return cos.written();
    } finally {
      if(gos != null) {
        gos.flush();
        gos.finish();
      }
View Full Code Here

TOP

Related Classes of freenet.support.io.CountedOutputStream

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.