Package freenet.crypt

Examples of freenet.crypt.MultiHashInputStream$Digester


            }
          }

          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

     *
     * @return a Digester with rules and variable substitutor
     */
    private Digester createDigesterThatCanDoAnt()
    {
        Digester digester = new Digester();

        MultiVariableExpander expander = new MultiVariableExpander();
        expander.addSource( "$", mutableSource );
        digester.setSubstitutor( new VariableSubstitutor( expander ) );

        int useRootObj = -1;
        Class<?>[] callerArgTypes = new Class[] { String.class, String.class };
        CallMethodRule caller = new CallMethodRule( useRootObj, "addProperty", callerArgTypes.length, callerArgTypes );
        digester.addRule( "root/property", caller );
        digester.addCallParam( "root/property", 0, "name" );
        digester.addCallParam( "root/property", 1, "value" );

        digester.addObjectCreate( "root/bean", SimpleTestBean.class );
        digester.addSetProperties( "root/bean" );
        digester.addSetNext( "root/bean", "addSimpleTestBean" );
        return digester;
    }
View Full Code Here

        throws SAXException, IOException
    {

        String xml = "<root alpha='${attr1}' beta='var{attr2}'/>";
        StringReader input = new StringReader( xml );
        Digester digester = new Digester();

        // Configure the digester as required
        digester.addObjectCreate( "root", SimpleTestBean.class );
        digester.addSetProperties( "root" );

        // Parse our test input.
        SimpleTestBean root = digester.parse( input );

        assertNotNull( "Digester returned no object", root );

        assertEquals( "${attr1}", root.getAlpha() );
        assertEquals( "var{attr2}", root.getBeta() );
View Full Code Here

        throws SAXException, IOException
    {

        String xml = "<root alpha='${attr1}' beta='var{attr2}'/>";
        StringReader input = new StringReader( xml );
        Digester digester = new Digester();

        // Configure the digester as required
        MultiVariableExpander expander = new MultiVariableExpander();
        digester.setSubstitutor( new VariableSubstitutor( expander ) );
        digester.addObjectCreate( "root", SimpleTestBean.class );
        digester.addSetProperties( "root" );

        // Parse our test input.
        SimpleTestBean root = digester.parse( input );

        assertNotNull( "Digester returned no object", root );

        assertEquals( "${attr1}", root.getAlpha() );
        assertEquals( "var{attr2}", root.getBeta() );
View Full Code Here

        String xml =
            "<root>" + "<bean alpha='${attr1}' beta='var{attr1}'/>" + "<bean alpha='${attr2}' beta='var{attr2}'/>"
                + "</root>";

        StringReader input = new StringReader( xml );
        Digester digester = new Digester();

        // Configure the digester as required
        HashMap<String, Object> source1 = new HashMap<String, Object>();
        source1.put( "attr1", "source1.attr1" );
        source1.put( "attr2", "source1.attr2" ); // should not be used

        HashMap<String, Object> source2 = new HashMap<String, Object>();
        source2.put( "attr1", "source2.attr1" ); // should not be used
        source2.put( "attr2", "source2.attr2" );

        MultiVariableExpander expander = new MultiVariableExpander();
        expander.addSource( "$", source1 );
        expander.addSource( "var", source2 );

        digester.setSubstitutor( new VariableSubstitutor( expander ) );
        digester.addObjectCreate( "root/bean", SimpleTestBean.class );
        digester.addSetProperties( "root/bean" );
        digester.addSetNext( "root/bean", "addSimpleTestBean" );

        // Parse our test input.
        this.simpleTestBeans.clear();
        digester.push( this );
        digester.parse( input );

        assertEquals( 2, this.simpleTestBeans.size() );

        {
            SimpleTestBean bean = this.simpleTestBeans.get( 0 );
View Full Code Here

    {

        String xml = "<root>" + "Twas noun{1} and the noun{2}" + " did verb{1} and verb{2} in the noun{3}" + "</root>";

        StringReader input = new StringReader( xml );
        Digester digester = new Digester();

        // Configure the digester as required
        HashMap<String, Object> nouns = new HashMap<String, Object>();
        nouns.put( "1", "brillig" );
        nouns.put( "2", "slithy toves" );
        nouns.put( "3", "wabe" );

        HashMap<String, Object> verbs = new HashMap<String, Object>();
        verbs.put( "1", "gyre" );
        verbs.put( "2", "gimble" );

        MultiVariableExpander expander = new MultiVariableExpander();
        expander.addSource( "noun", nouns );
        expander.addSource( "verb", verbs );
        digester.setSubstitutor( new VariableSubstitutor( expander ) );

        digester.addObjectCreate( "root", SimpleTestBean.class );
        digester.addCallMethod( "root", "setAlpha", 0 );

        // Parse our test input.
        SimpleTestBean root = digester.parse( input );

        assertNotNull( "Digester returned no object", root );

        assertEquals( "Twas brillig and the slithy toves" + " did gyre and gimble in the wabe", root.getAlpha() );
    }
View Full Code Here

TOP

Related Classes of freenet.crypt.MultiHashInputStream$Digester

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.