Example usage for java.io ByteArrayInputStream close

List of usage examples for java.io ByteArrayInputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayInputStream has no effect.

Usage

From source file:cpcc.vvrte.services.VirtualVehicleMigratorTest.java

private void verifyChunk(Object[] params, ArchiveStreamFactory factory, int chunkNumber, byte[] chunk)
        throws ArchiveException, IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(chunk);
    ArchiveInputStream inStream = factory.createArchiveInputStream("tar", bis);

    Object[] chunkParams = (Object[]) params[chunkNumber];

    int entryNumber = 0;
    for (TarArchiveEntry entry = readEntry(inStream); entry != null; entry = readEntry(
            inStream), ++entryNumber) {//from w  w  w .  jav  a 2  s  .c  o m
        byte[] content = IOUtils.toByteArray(inStream); // readContent(inStream);
        // System.out.print("Content: '");
        // System.out.write(content);
        // System.out.print("'");

        assertThat(chunkParams).describedAs("Chunk parameter").isNotNull();
        assertThat(((Object[]) chunkParams).length).describedAs("Chunk parameter length")
                .isGreaterThan(entryNumber);

        Object[] entryParams = (Object[]) chunkParams[entryNumber];

        String name = (String) entryParams[0];
        int length = (Integer) entryParams[1];
        int storageId = (Integer) entryParams[2];
        int chunkId = (Integer) entryParams[3];
        String userName = (String) entryParams[4];
        String groupName = (String) entryParams[5];

        assertThat(entry.getName()).overridingErrorMessage("Expected name %s but was %s in chunk %d", name,
                entry.getName(), chunkNumber).isNotNull().isEqualTo(name);
        assertThat(entry.getSize()).overridingErrorMessage("Expected size %d but was %d in chunk %d", length,
                entry.getSize(), chunkNumber).isNotNull().isEqualTo(length);
        assertThat(entry.getLongUserId()).isNotNull().isEqualTo(storageId);
        assertThat(entry.getLongGroupId()).isNotNull().isEqualTo(chunkId);
        assertThat(entry.getUserName()).isNotNull().isEqualTo(userName);
        assertThat(entry.getGroupName()).isNotNull().isEqualTo(groupName);
        assertThat(content.length).isEqualTo(length);
    }

    inStream.close();
    bis.close();
}

From source file:org.apache.myfaces.shared_ext202patch.util.StateUtils.java

/**
 * Performs deserialization with the serialization provider created from the
 * SerialFactory.//from   ww w .  ja va2  s  .com
 * 
 * @param bytes
 * @param ctx
 * @return
 */

public static final Object getAsObject(byte[] bytes, ExternalContext ctx) {
    ByteArrayInputStream input = null;

    try {
        input = new ByteArrayInputStream(bytes);

        // get the Factory that was instantiated @ startup
        SerialFactory serialFactory = (SerialFactory) ctx.getApplicationMap().get(SERIAL_FACTORY);

        if (serialFactory == null)
            throw new NullPointerException("serialFactory");

        ObjectInputStream s = null;
        Exception pendingException = null;
        try {
            s = serialFactory.getObjectInputStream(input);
            Object object = null;
            if (System.getSecurityManager() != null) {
                final ObjectInputStream ois = s;
                object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    //Put IOException and ClassNotFoundException as "checked" exceptions,
                    //so AccessController wrap them in a PrivilegedActionException
                    public Object run() throws PrivilegedActionException, IOException, ClassNotFoundException {
                        return ois.readObject();
                    }
                });
                // Since s has the same instance as ois,
                // we don't need to close it here, rather
                // close it on the finally block related to s
                // and avoid duplicate close exceptions
                // finally
                // {
                //    ois.close();
                // }
            } else {
                object = s.readObject();
            }
            return object;
        } catch (Exception e) {
            pendingException = e;
            throw new FacesException(e);
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException e) {
                    // If a previous exception is thrown 
                    // ignore this, but if not, wrap it in a
                    // FacesException and throw it. In this way
                    // we preserve the original semantic of this
                    // method, but we handle correctly the case
                    // when we close a stream. Obviously, the 
                    // information about this exception is lost,
                    // but note that the interesting information 
                    // is always on pendingException, since we
                    // only do a readObject() on the outer try block.
                    if (pendingException == null) {
                        throw new FacesException(e);
                    }
                } finally {
                    s = null;
                }
            }
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                //ignore it, because ByteArrayInputStream.close has
                //no effect, but it is better to call close and preserve
                //semantic from previous code.
            } finally {
                input = null;
            }
        }
    }
}

From source file:org.apache.myfaces.shared.util.StateUtils.java

/**
 * Performs deserialization with the serialization provider created from the
 * SerialFactory.//from   ww w . ja v a2 s .c  o  m
 * 
 * @param bytes
 * @param ctx
 * @return
 */

public static final Object getAsObject(byte[] bytes, ExternalContext ctx) {
    ByteArrayInputStream input = null;

    try {
        input = new ByteArrayInputStream(bytes);

        // get the Factory that was instantiated @ startup
        SerialFactory serialFactory = (SerialFactory) ctx.getApplicationMap().get(SERIAL_FACTORY);

        if (serialFactory == null) {
            throw new NullPointerException("serialFactory");
        }

        ObjectInputStream s = null;
        Exception pendingException = null;
        try {
            s = serialFactory.getObjectInputStream(input);
            Object object = null;
            if (System.getSecurityManager() != null) {
                final ObjectInputStream ois = s;
                object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    //Put IOException and ClassNotFoundException as "checked" exceptions,
                    //so AccessController wrap them in a PrivilegedActionException
                    public Object run() throws PrivilegedActionException, IOException, ClassNotFoundException {
                        return ois.readObject();
                    }
                });
                // Since s has the same instance as ois,
                // we don't need to close it here, rather
                // close it on the finally block related to s
                // and avoid duplicate close exceptions
                // finally
                // {
                //    ois.close();
                // }
            } else {
                object = s.readObject();
            }
            return object;
        } catch (Exception e) {
            pendingException = e;
            throw new FacesException(e);
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException e) {
                    // If a previous exception is thrown 
                    // ignore this, but if not, wrap it in a
                    // FacesException and throw it. In this way
                    // we preserve the original semantic of this
                    // method, but we handle correctly the case
                    // when we close a stream. Obviously, the 
                    // information about this exception is lost,
                    // but note that the interesting information 
                    // is always on pendingException, since we
                    // only do a readObject() on the outer try block.
                    if (pendingException == null) {
                        throw new FacesException(e);
                    }
                } finally {
                    s = null;
                }
            }
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                //ignore it, because ByteArrayInputStream.close has
                //no effect, but it is better to call close and preserve
                //semantic from previous code.
            } finally {
                input = null;
            }
        }
    }
}

From source file:org.globus.gsi.gssapi.GlobusGSSContextImpl.java

private X509Certificate bcConvert(X509Certificate cert) throws GSSException {
    if (!(cert instanceof X509CertificateObject)) {
        ByteArrayInputStream inputStream = null;
        try {/*from w ww.ja va  2s. c  o m*/
            inputStream = new ByteArrayInputStream(cert.getEncoded());
            return CertificateLoadUtil.loadCertificate(inputStream);
        } catch (Exception e) {
            throw new GlobusGSSException(GSSException.FAILURE, e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    logger.warn("Unable to close streamreader.");
                }
            }
        }
    } else {
        return cert;
    }
}

From source file:org.globus.gsi.gssapi.GlobusGSSContextImpl.java

/**
 * This function drives the accepting side of the context establishment
 * process. It is expected to be called in tandem with the
 * {@link #initSecContext(byte[], int, int) initSecContext} function.
 * <BR>/*from  w  w w  .j a  v  a2  s .  co  m*/
 * The behavior of context establishment process can be modified by
 * {@link GSSConstants#GSS_MODE GSSConstants.GSS_MODE}
 * and {@link GSSConstants#REJECT_LIMITED_PROXY
 * GSSConstants.REJECT_LIMITED_PROXY} context options. If the
 * {@link GSSConstants#GSS_MODE GSSConstants.GSS_MODE}
 * option is set to
 * {@link GSIConstants#MODE_SSL GSIConstants.MODE_SSL}
 * the context establishment process will be compatible with regular SSL
 * (no credential delegation support). If the option is set to
 * {@link GSIConstants#MODE_GSI GSIConstants.MODE_GSI}
 * credential delegation during context establishment process will be accepted.
 * If the {@link GSSConstants#REJECT_LIMITED_PROXY
 * GSSConstants.REJECT_LIMITED_PROXY} option is enabled, a peer
 * presenting limited proxy credential will be automatically
 * rejected and the context establishment process will be aborted.
 *
 * @return a byte[] containing the token to be sent to the peer.
 *         null indicates that no token is generated (needs more data)
 */
public byte[] acceptSecContext(byte[] inBuff, int off, int len) throws GSSException {
    logger.debug("enter acceptSecContext");

    if (!this.conn) {
        this.role = ACCEPT;

        logger.debug("enter initializing in acceptSecContext");

        if (this.ctxCred.getName().isAnonymous()) {
            throw new GlobusGSSException(GSSException.DEFECTIVE_CREDENTIAL, GlobusGSSException.UNKNOWN,
                    "acceptCtx00");
        }

        if (this.ctxCred.getUsage() != GSSCredential.ACCEPT_ONLY
                && this.ctxCred.getUsage() != GSSCredential.INITIATE_AND_ACCEPT) {
            throw new GlobusGSSException(GSSException.DEFECTIVE_CREDENTIAL, GlobusGSSException.UNKNOWN,
                    "badCredUsage");
        }

        setCredential();

        try {
            init(this.role);
        } catch (SSLException e) {
            throw new GlobusGSSException(GSSException.FAILURE, e);
        }

        this.conn = true;
        logger.debug("done initializing in acceptSecContext");
    }

    /*DEL
            this.out.reset();
            this.in.putToken(inBuff, off, len);
    */
    this.outByteBuff.clear();
    ByteBuffer inByteBuff;
    if (savedInBytes != null) {
        if (len > 0) {
            byte[] allInBytes = new byte[savedInBytes.length + len];
            logger.debug("ALLOCATED for allInBytes " + savedInBytes.length + " + " + len + " bytes\n");
            System.arraycopy(savedInBytes, 0, allInBytes, 0, savedInBytes.length);
            System.arraycopy(inBuff, off, allInBytes, savedInBytes.length, len);
            inByteBuff = ByteBuffer.wrap(allInBytes, 0, allInBytes.length);
        } else {
            inByteBuff = ByteBuffer.wrap(savedInBytes, 0, savedInBytes.length);
        }
        savedInBytes = null;
    } else {
        inByteBuff = ByteBuffer.wrap(inBuff, off, len);
    }

    switch (state) {

    case HANDSHAKE:

        try {
            logger.debug("STATUS BEFORE: " + this.sslEngine.getHandshakeStatus().toString());
            SSLEngineResult.HandshakeStatus handshake_status = sslEngine.getHandshakeStatus();

            if (handshake_status == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
                // return null;
                throw new Exception("GSSAPI in HANDSHAKE state but " + "SSLEngine in NOT_HANDSHAKING state!");
            } else {
                outByteBuff = this.sslProcessHandshake(inByteBuff, outByteBuff);
            }

            logger.debug("STATUS AFTER: " + this.sslEngine.getHandshakeStatus().toString());

            outByteBuff.flip();

            /*DEL
                            if (this.conn.getHandshake().finishedP()) {
            */
            if (this.sslEngine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
                // the wrap/unwrap above has resulted in handshaking
                // being complete on our end.

                logger.debug("acceptSecContext handshake finished");
                handshakeFinished();

                // acceptor
                for (X509Certificate cert : this.ctxCred.getCertificateChain()) {
                    setGoodUntil(cert.getNotAfter());
                }
                this.targetName = this.ctxCred.getName();

                // initiator - peer
                /*DEL
                                    Vector chain = this.conn.getCertificateChain();
                */
                Certificate[] chain;
                try {
                    chain = this.sslEngine.getSession().getPeerCertificates();
                } catch (SSLPeerUnverifiedException e) {
                    chain = null;
                }
                if (chain == null || chain.length == 0) {
                    this.sourceName = new GlobusGSSName();
                    this.anonymity = true;
                } else {
                    /*DEL
                                            X509Cert crt = (X509Cert)chain.elementAt(chain.size()-1);
                                            setGoodUntil(crt.getValidityNotAfter());
                            
                                            String identity = verifyChain(chain);
                    */
                    for (X509Certificate cert : (X509Certificate[]) chain) {
                        setGoodUntil(cert.getNotAfter());
                    }

                    String identity = BouncyCastleUtil.getIdentity(
                            bcConvert(BouncyCastleUtil.getIdentityCertificate((X509Certificate[]) chain)));
                    this.sourceName = new GlobusGSSName(CertificateUtil.toGlobusID(identity, false));
                    this.peerLimited = Boolean.valueOf(ProxyCertificateUtil
                            .isLimitedProxy(BouncyCastleUtil.getCertificateType((X509Certificate) chain[0])));

                    logger.debug("Peer Identity is: " + identity + " Target name is: " + this.targetName
                            + " Limited Proxy: " + this.peerLimited.toString());

                    this.anonymity = false;
                }

                if (this.gssMode == GSIConstants.MODE_GSI) {
                    this.state = SERVER_START_DEL;
                } else {
                    setDone();
                }
            }
        } catch (IOException e) {
            throw new GlobusGSSException(GSSException.FAILURE, e);
        } catch (Exception e) {
            throw new GlobusGSSException(GSSException.FAILURE, e);
        }

        break;

    case SERVER_START_DEL:

        try {
            if (inByteBuff.remaining() <= 0) {
                return null;
            }

            /*DEL
                            int delChar = this.conn.getInStream().read();
            */
            outByteBuff = sslDataUnwrap(inByteBuff, outByteBuff);
            outByteBuff.flip();
            byte[] delChar = new byte[outByteBuff.remaining()];
            outByteBuff.get(delChar, 0, delChar.length);
            /*DEL
                            if (delChar != GSIConstants.DELEGATION_CHAR) {
            */
            if (!Arrays.equals(delChar, DELEGATION_TOKEN)) {
                setDone();
                break;
            }

            /*DEL
                            Vector chain = this.conn.getCertificateChain();
            */
            Certificate[] chain;
            try {
                chain = this.sslEngine.getSession().getPeerCertificates();
            } catch (SSLPeerUnverifiedException e) {
                chain = null;
            }
            if (chain == null || chain.length == 0) {
                throw new GlobusGSSException(GSSException.FAILURE, GlobusGSSException.DELEGATION_ERROR,
                        "noClientCert");
            }

            X509Certificate tmpCert = (X509Certificate) chain[0];
            /*DEL
            PureTLSUtil.convertCert((X509Cert)chain.lastElement());
            */
            byte[] req = generateCertRequest(tmpCert);
            /*DEL
                            this.conn.getOutStream().write(req, 0, req.length);
            */
            inByteBuff = ByteBuffer.wrap(req, 0, req.length);
            outByteBuff.clear();
            outByteBuff = sslDataWrap(inByteBuff, outByteBuff);
            outByteBuff.flip();

        } catch (GeneralSecurityException e) {
            throw new GlobusGSSException(GSSException.FAILURE, e);
        }

        this.state = SERVER_END_DEL;
        break;

    case SERVER_END_DEL:

        try {
            if (inByteBuff.remaining() <= 0) {
                return null;
            }

            /*DEL
                            X509Certificate certificate = CertUtil.loadCertificate(this.conn.getInStream());
            */
            outByteBuff = sslDataUnwrap(inByteBuff, outByteBuff);
            outByteBuff.flip();
            if (!outByteBuff.hasRemaining())
                break;
            byte[] buf = new byte[outByteBuff.remaining()];
            outByteBuff.get(buf, 0, buf.length);
            ByteArrayInputStream inStream = new ByteArrayInputStream(buf, 0, buf.length);
            CertificateFactory cf = null;
            X509Certificate certificate = null;
            try {
                cf = CertificateFactory.getInstance("X.509");
                certificate = (X509Certificate) cf.generateCertificate(inStream);
            } finally {
                inStream.close();
            }

            if (logger.isTraceEnabled()) {
                logger.trace("Received delegated cert: " + certificate.toString());
            }

            verifyDelegatedCert(certificate);

            /*DEL
                            Vector chain = this.conn.getCertificateChain();
            */
            Certificate[] chain = this.sslEngine.getSession().getPeerCertificates();
            int chainLen = chain.length;
            X509Certificate[] newChain = new X509Certificate[chainLen + 1];
            newChain[0] = bcConvert((X509Certificate) certificate);
            for (int i = 0; i < chainLen; i++) {
                /*DEL
                                    newChain[i+1] = PureTLSUtil.convertCert((X509Cert)chain.elementAt(chainLen - 1 - i));
                */
                newChain[i + 1] = bcConvert((X509Certificate) chain[i]);
            }

            X509Credential proxy = new X509Credential(this.keyPair.getPrivate(), newChain);

            this.delegCred = new GlobusGSSCredentialImpl(proxy, GSSCredential.INITIATE_AND_ACCEPT);

        } catch (GeneralSecurityException e) {
            throw new GlobusGSSException(GSSException.FAILURE, e);
        } catch (IOException e) {
            throw new GlobusGSSException(GSSException.FAILURE, e);
        }
        setDone();
        break;

    default:
        throw new GSSException(GSSException.FAILURE);
    }

    if (inByteBuff.hasRemaining()) {
        // Likely BUFFER_UNDERFLOW; save the
        // inByteBuff bytes here like in the unwrap() case
        logger.debug("Not all data processed; Original: " + len + " Remaining: " + inByteBuff.remaining()
                + " Handshaking status: " + sslEngine.getHandshakeStatus());
        logger.debug("SAVING unprocessed " + inByteBuff.remaining() + "BYTES\n");
        savedInBytes = new byte[inByteBuff.remaining()];
        inByteBuff.get(savedInBytes, 0, savedInBytes.length);
    }

    logger.debug("exit acceptSecContext");
    /*DEL
            return (this.out.size() > 0) ? this.out.toByteArray() : null;
    */
    if (this.outByteBuff.hasRemaining()) {
        // TODO can we avoid this copy if the ByteBuffer is array based
        // and we return that array, each time allocating a new array
        // for outByteBuff?
        byte[] out = new byte[this.outByteBuff.remaining()];
        this.outByteBuff.get(out, 0, out.length);
        return out;
    } else
        return null;
}

From source file:org.sakaiproject.kernel.authz.simple.SimpleJcrUserEnvironmentResolverService.java

/**
 * {@inheritDoc}/*  ww w.ja  v  a  2  s.  c  om*/
 *
 * @see org.sakaiproject.kernel.api.userenv.UserEnvironmentResolverService#create(org.sakaiproject.kernel.api.user.User,
 *      java.lang.String)
 */
public UserEnvironment create(User u, String externalId, String password, String userType) {
    String userEnvironmentPath = userFactoryService.getUserEnvPath(u.getUuid());

    ByteArrayInputStream bais = null;
    InputStream templateInputStream = null;
    try {

        String userEnvironmentTemplate = userFactoryService.getUserEnvTemplate(userType);

        // load the template
        templateInputStream = jcrNodeFactoryService.getInputStream(userEnvironmentTemplate);
        String template = IOUtils.readFully(templateInputStream, "UTF-8");
        System.err.println("Loading UE from " + userEnvironmentTemplate + " as " + template);
        UserEnvironmentBean userEnvironmentBean = beanConverter.convertToObject(template,
                UserEnvironmentBean.class);

        // make the template this user
        userEnvironmentBean.setEid(externalId);
        userEnvironmentBean.setUuid(u.getUuid());
        Map<String, String> p = new HashMap<String, String>();
        p.put("userType", userType);
        userEnvironmentBean.setProperties(p);

        // save the template
        String userEnv = beanConverter.convertToString(userEnvironmentBean);
        System.err.println("Saving UE to " + userEnvironmentPath + " as " + userEnv);
        bais = new ByteArrayInputStream(userEnv.getBytes("UTF-8"));
        Node userEnvNode = jcrNodeFactoryService.setInputStream(userEnvironmentPath, bais,
                RestProvider.CONTENT_TYPE);

        // set the password
        userEnvNode.setProperty(JcrAuthenticationResolverProvider.JCRPASSWORDHASH,
                org.sakaiproject.kernel.util.StringUtils.sha1Hash(password));

        // make the private and shares spaces for the user owned by this used.
        jcrNodeFactoryService.setOwner(userFactoryService.getUserPrivatePath(u.getUuid()), u.getUuid());
        jcrNodeFactoryService.setOwner(userFactoryService.getUserSharedPrivatePath(u.getUuid()), u.getUuid());

        // allow other provisioning agents to perform
        Registry<String, UserProvisionAgent> registry = registryService
                .getRegistry(UserProvisionAgent.REGISTRY);
        for (UserProvisionAgent agent : registry.getList()) {
            agent.provision(userEnvironmentBean);
        }

        userEnvironmentBean.seal();
        return userEnvironmentBean;

    } catch (RepositoryException e) {
        LOG.error(e.getMessage(), e);
    } catch (JCRNodeFactoryServiceException e) {
        LOG.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        LOG.error(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        try {
            bais.close();
        } catch (Exception ex) {
            // not interested
        }
        try {
            templateInputStream.close();
        } catch (Exception ex) {
            // not interested
        }
    }
    return null;

}

From source file:com.flexoodb.common.FlexUtils.java

static public void saveBytesToFile(String filepath, byte[] data) throws Exception {
    ByteArrayInputStream is = new ByteArrayInputStream(data);
    java.io.FileOutputStream fos = new java.io.FileOutputStream(new File(filepath));
    while (is.available() > 0) {
        fos.write(is.read());/*  www. ja va 2  s . co m*/
    }
    fos.close();
    is.close();
}

From source file:com.flexoodb.common.FlexUtils.java

static public void saveStringToFile(String filepath, String content) throws Exception {
    ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
    java.io.FileOutputStream fos = new java.io.FileOutputStream(new File(filepath));
    while (is.available() > 0) {
        fos.write(is.read());/*from w  w  w . j a v  a  2 s  .c  o m*/
    }
    fos.close();
    is.close();
}

From source file:com.digitalpebble.stormcrawler.tika.ParserBolt.java

@Override
public void execute(Tuple tuple) {
    eventCounter.scope("tuple_in").incrBy(1);

    byte[] content = tuple.getBinaryByField("content");

    String url = tuple.getStringByField("url");
    Metadata metadata = (Metadata) tuple.getValueByField("metadata");

    long start = System.currentTimeMillis();

    ByteArrayInputStream bais = new ByteArrayInputStream(content);
    org.apache.tika.metadata.Metadata md = new org.apache.tika.metadata.Metadata();

    // provide the mime-type as a clue for guessing
    String httpCT = metadata.getFirstValue(HttpHeaders.CONTENT_TYPE);
    if (StringUtils.isNotBlank(httpCT)) {
        // pass content type from server as a clue
        md.set(org.apache.tika.metadata.Metadata.CONTENT_TYPE, httpCT);
    }/*from   w ww  . j  av  a 2 s.com*/

    // as well as the filename
    try {
        URL _url = new URL(url);
        md.set(org.apache.tika.metadata.Metadata.RESOURCE_NAME_KEY, _url.getFile());
    } catch (MalformedURLException e1) {
        throw new IllegalStateException("Malformed URL", e1);
    }

    LinkContentHandler linkHandler = new LinkContentHandler();
    ContentHandler textHandler = new BodyContentHandler(-1);
    TeeContentHandler teeHandler = new TeeContentHandler(linkHandler, textHandler);
    ParseContext parseContext = new ParseContext();

    try {
        parseContext.set(HtmlMapper.class, (HtmlMapper) HTMLMapperClass.newInstance());
    } catch (Exception e) {
        LOG.error("Exception while specifying HTMLMapper {}", url, e);
    }

    // build a DOM if required by the parseFilters
    DocumentFragment root = null;
    if (parseFilters.needsDOM()) {
        HTMLDocumentImpl doc = new HTMLDocumentImpl();
        doc.setErrorChecking(false);
        root = doc.createDocumentFragment();
        DOMBuilder domhandler = new DOMBuilder(doc, root);
        domhandler.setUpperCaseElementNames(upperCaseElementNames);
        domhandler.setDefaultNamespaceURI(XHTMLContentHandler.XHTML);
        teeHandler = new TeeContentHandler(linkHandler, textHandler, domhandler);
    }

    // parse
    String text;
    try {
        tika.getParser().parse(bais, teeHandler, md, parseContext);
        text = textHandler.toString();
    } catch (Throwable e) {
        String errorMessage = "Exception while parsing " + url + ": " + e;
        LOG.error(errorMessage);
        // send to status stream in case another component wants to update
        // its status
        metadata.setValue(Constants.STATUS_ERROR_SOURCE, "content parsing");
        metadata.setValue(Constants.STATUS_ERROR_MESSAGE, errorMessage);
        collector.emit(StatusStreamName, tuple, new Values(url, metadata, Status.ERROR));
        collector.ack(tuple);
        // Increment metric that is context specific
        eventCounter.scope("error_content_parsing_" + e.getClass().getSimpleName()).incrBy(1);
        // Increment general metric
        eventCounter.scope("parse exception").incrBy(1);
        return;
    } finally {
        try {
            bais.close();
        } catch (IOException e) {
            LOG.error("Exception while closing stream", e);
        }
    }

    // add parse md to metadata
    for (String k : md.names()) {
        String[] values = md.getValues(k);
        metadata.setValues("parse." + k, values);
    }

    long duration = System.currentTimeMillis() - start;

    LOG.info("Parsed {} in {} msec", url, duration);

    // filter and convert the outlinks
    List<Outlink> outlinks = toOutlinks(url, linkHandler.getLinks(), metadata);

    ParseResult parse = new ParseResult();
    parse.setOutlinks(outlinks);

    // parse data of the parent URL
    ParseData parseData = parse.get(url);
    parseData.setMetadata(metadata);
    parseData.setText(text);
    parseData.setContent(content);

    // apply the parse filters if any
    try {
        parseFilters.filter(url, content, root, parse);
    } catch (RuntimeException e) {
        String errorMessage = "Exception while running parse filters on " + url + ": " + e;
        LOG.error(errorMessage);
        metadata.setValue(Constants.STATUS_ERROR_SOURCE, "content filtering");
        metadata.setValue(Constants.STATUS_ERROR_MESSAGE, errorMessage);
        collector.emit(StatusStreamName, tuple, new Values(url, metadata, Status.ERROR));
        collector.ack(tuple);
        // Increment metric that is context specific
        eventCounter.scope("error_content_filtering_" + e.getClass().getSimpleName()).incrBy(1);
        // Increment general metric
        eventCounter.scope("parse exception").incrBy(1);
        return;
    }

    if (emitOutlinks) {
        for (Outlink outlink : parse.getOutlinks()) {
            collector.emit(StatusStreamName, tuple,
                    new Values(outlink.getTargetURL(), outlink.getMetadata(), Status.DISCOVERED));
        }
    }

    // emit each document/subdocument in the ParseResult object
    // there should be at least one ParseData item for the "parent" URL

    for (Map.Entry<String, ParseData> doc : parse) {
        ParseData parseDoc = doc.getValue();

        collector.emit(tuple,
                new Values(doc.getKey(), parseDoc.getContent(), parseDoc.getMetadata(), parseDoc.getText()));
    }

    collector.ack(tuple);
    eventCounter.scope("tuple_success").incrBy(1);
}

From source file:big.BigZip.java

/**
 * Copies one file into the big archive/*from  w  w w. jav  a2 s.co  m*/
 * @param stream
 * @param SHA1
 * @param filePathToWriteInTextLine
 * @return 
 * @throws java.io.IOException 
 */
public boolean quickWriteGenericStream(final InputStream stream, final String SHA1,
        final String filePathToWriteInTextLine) throws IOException {
    // declare
    ByteArrayOutputStream outputZipStream = new ByteArrayOutputStream();
    ByteArrayInputStream byteInput = null;
    try {
        // save this operation on the log of commits
        addTagStarted(filePathToWriteInTextLine);
        // Create Archive Output Stream that attaches File Output Stream / and specifies type of compression
        ArchiveOutputStream logical_zip = new ArchiveStreamFactory()
                .createArchiveOutputStream(ArchiveStreamFactory.ZIP, outputZipStream);
        // Create Archive entry - write header information
        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(filePathToWriteInTextLine);
        logical_zip.putArchiveEntry(zipArchiveEntry);
        // Copy input file

        IOUtils.copy(stream, logical_zip);

        logical_zip.closeArchiveEntry();
        logical_zip.finish();
        logical_zip.flush();
        logical_zip.close();

        // get the bytes
        byteInput = new ByteArrayInputStream(outputZipStream.toByteArray());

        byte[] buffer = new byte[8192];
        int length, counter = 0;
        // add the magic number to this file block
        outputStream.write(magicSignature.getBytes());
        // now copy the whole file into the BIG archive
        while ((length = byteInput.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
            counter += length;
        }

        final String line = "\n" + utils.files.getPrettyFileSize(currentPosition) + " " + SHA1 + " "
                + filePathToWriteInTextLine;

        // write a new line in our index file
        writerFileIndex.write(line);
        //writer.flush();
        // increase the position counter
        currentPosition += counter + magicSignature.length();

        // close the log with success
        addTagEnded();
    } catch (Exception e) {
        System.err.println("BIG600 - Error copying file: " + filePathToWriteInTextLine);
        return false;
    } finally {
        if (byteInput != null) {
            byteInput.close();
        }
        stream.close();
        outputZipStream.close();
        outputStream.close();
    }
    return true;
}