Package freenet.crypt

Examples of freenet.crypt.MultiHashInputStream


            }
          }

          InputStream is = null;
          OutputStream os = null;
          MultiHashInputStream hasher = null;
          try {
            is = origData.getInputStream();
            result = bucketFactory.makeBucket(-1);
            os = result.getOutputStream();
            long maxOutputSize = bestCompressedDataSize;
            if(first && generateHashes != 0) {
              if(logMINOR) Logger.minor(this, "Generating hashes: "+generateHashes);
              is = hasher = new MultiHashInputStream(is, generateHashes);
            }
            try {
              comp.compress(is, os, origSize, maxOutputSize);
            } catch (RuntimeException e) {
              // ArithmeticException has been seen in bzip2 codec.
              Logger.error(this, "Compression failed with codec "+comp+" : "+e, e);
              // Try the next one
              // RuntimeException is iffy, so lets not try the hasher.
              continue;
            } catch (CompressionOutputSizeException e) {
              if(hasher != null) {
                is.skip(Long.MAX_VALUE);
                hashes = hasher.getResults();
                first = false;
              }
              continue; // try next compressor type
            }
            if(hasher != null) {
              hashes = hasher.getResults();
              first = false;
            }
          } finally {
            Closer.close(is);
            Closer.close(os);
View Full Code Here


  public void run() {
    if(logMINOR) Logger.minor(this, "Starting worker thread for "+uri+" mime type "+mimeType+" filter data = "+filterData+" charset "+charset);
    try {
      //Validate the hash of the now decompressed data
      input = new BufferedInputStream(input);
      MultiHashInputStream hashStream = null;
      if(hashes != null) {
        hashStream = new MultiHashInputStream(input, HashResult.makeBitmask(hashes));
        input = hashStream;
      }
      //Filter the data, if we are supposed to
      if(filterData){
        if(logMINOR) Logger.minor(this, "Running content filter... Prefetch hook: "+prefetchHook+" tagReplacer: "+tagReplacer);
        if(mimeType == null || uri == null || input == null || output == null) throw new IOException("Insufficient arguements to worker thread");
        // Send XHTML as HTML because we can't use web-pushing on XHTML.
        FilterStatus filterStatus = ContentFilter.filter(input, output, mimeType, uri, prefetchHook, tagReplacer, charset, linkFilterExceptionProvider);

        String detectedMIMEType = filterStatus.mimeType.concat(filterStatus.charset == null ? "" : "; charset="+filterStatus.charset);
        synchronized(this) {
          clientMetadata = new ClientMetadata(detectedMIMEType);
        }
      }
      else {
        if(logMINOR) Logger.minor(this, "Ignoring content filter. The final result has not been written. Writing now.");
        FileUtil.copy(input, output, -1);
      }
      // Dump the rest.
      try {
        while(true) {
            // FileInputStream.skip() doesn't do what we want. Use read().
            // Note this is only necessary because we might have an AEADInputStream?
            // FIXME get rid - they should check the end anyway?
            byte[] buf = new byte[4096];
            int r = input.read(buf);
            if(r < 0) break;
        }
      } catch (EOFException e) {
        // Okay.
      }
      input.close();
      output.close();
      if(hashes != null) {
        HashResult[] results = hashStream.getResults();
        if(!HashResult.strictEquals(results, hashes)) {
          Logger.error(this, "Hashes failed verification (length read is "+hashStream.getReadBytes()+") "+" for "+uri);
          throw new FetchException(FetchExceptionMode.CONTENT_HASH_FAILED);
        }
      }

      onFinish();
View Full Code Here

      try {
        if(hashes != null) {
          InputStream is = null;
          try {
            is = data.getInputStream();
            MultiHashInputStream hasher = new MultiHashInputStream(is, HashResult.makeBitmask(hashes));
            byte[] buf = new byte[32768];
            while(hasher.read(buf) > 0);
            hasher.close();
            is = null;
            HashResult[] results = hasher.getResults();
            if(!HashResult.strictEquals(results, hashes)) {
              onFailure(new FetchException(FetchExceptionMode.CONTENT_HASH_FAILED), SingleFileFetcher.this, context);
              return;
            }
          } catch (InsufficientDiskSpaceException e) {
View Full Code Here

        }
    }

    private HashResult[] getHashes(LockableRandomAccessBuffer data) throws IOException {
        InputStream is = new RAFInputStream(data, 0, data.size());
        MultiHashInputStream hashStream = new MultiHashInputStream(is, HashType.SHA256.bitmask);
        FileUtil.copy(is, new NullOutputStream(), data.size());
        is.close();
        return hashStream.getResults();
    }
View Full Code Here

    }
   
    private HashResult[] getHashes(LockableRandomAccessBuffer data) throws IOException {
        InputStream is = new RAFInputStream(data, 0, data.size());
        MultiHashInputStream hashStream = new MultiHashInputStream(is, HashType.SHA256.bitmask);
        FileUtil.copy(is, new NullOutputStream(), data.size());
        is.close();
        return hashStream.getResults();
    }
View Full Code Here

TOP

Related Classes of freenet.crypt.MultiHashInputStream

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.