Example usage for org.apache.commons.io.input TeeInputStream TeeInputStream

List of usage examples for org.apache.commons.io.input TeeInputStream TeeInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input TeeInputStream TeeInputStream.

Prototype

public TeeInputStream(InputStream input, OutputStream branch) 

Source Link

Document

Creates a TeeInputStream that proxies the given InputStream and copies all read bytes to the given OutputStream .

Usage

From source file:appengine_commons.logger_filter.RequestWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    return new ServletInputStream() {
        private TeeInputStream tee = new TeeInputStream(RequestWrapper.super.getInputStream(), bos);

        @Override//from   ww w  .java 2  s  . c  om
        public int read() throws IOException {
            return tee.read();
        }
    };
}

From source file:end2endtests.runner.ProcessRunner.java

private static void redirectStream(InputStream input, OutputStream systemOut, OutputStream toWatcher) {
    redirectStream(new TeeInputStream(input, toWatcher), new CloseShieldOutputStream(systemOut));
}

From source file:com.du.order.dist.log.RequestWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    return new ServletInputStream() {
        @Override//  ww w  . j a  v a2 s.com
        public boolean isFinished() {
            return false;
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setReadListener(ReadListener readListener) {

        }

        private TeeInputStream tee = new TeeInputStream(RequestWrapper.super.getInputStream(), bos);

        @Override
        public int read() throws IOException {
            return tee.read();
        }
    };
}

From source file:eu.freme.broker.tools.loggingfilter.RequestWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    return new ServletInputStream() {
        private TeeInputStream tee = new TeeInputStream(RequestWrapper.super.getInputStream(), bos);

        @Override/*from  w w  w.ja  v  a  2 s.c  om*/
        public int read() throws IOException {
            return tee.read();
        }

        @Override
        public void setReadListener(ReadListener listener) {
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public boolean isFinished() {
            return true;
        }
    };
}

From source file:com.xebialabs.overcast.command.CommandProcessor.java

public CommandResponse run(final Command command) {

    logger.debug("Executing command {}", command);

    try {/*from w  w w.  j av a  2s  .co  m*/
        Process p = new ProcessBuilder(command.asList()).directory(new File(execDir)).start();

        // We do this small trick to have stdout and stderr of the process on the console and
        // at the same time capture them to strings.
        ByteArrayOutputStream errors = new ByteArrayOutputStream();
        ByteArrayOutputStream messages = new ByteArrayOutputStream();

        Thread t1 = showProcessOutput(new TeeInputStream(p.getErrorStream(), errors), System.err);
        Thread t2 = showProcessOutput(new TeeInputStream(p.getInputStream(), messages), System.out);

        int code = p.waitFor();

        t1.join();
        t2.join();

        CommandResponse response = new CommandResponse(code, errors.toString(), messages.toString());

        if (!response.isSuccessful()) {
            throw new NonZeroCodeException(command, response);
        }

        return response;

    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Cannot execute " + command.toString(), e);
    } catch (IOException e) {
        throw new RuntimeException("Cannot execute " + command.toString(), e);
    }
}

From source file:io.mapzone.controller.catalog.http.CatalogRequest.java

<T> T parseBody() throws Exception {
    Unmarshaller unmarshaller = CswResponse.jaxbContext.get().createUnmarshaller();
    InputStream in = httpRequest.getInputStream();
    System.out.println("REQUEST: ");
    TeeInputStream tee = new TeeInputStream(in, System.out);
    parsedBody = ((JAXBElement) unmarshaller.unmarshal(new StreamSource(tee))).getValue();
    return (T) parsedBody;
}

From source file:fr.pilato.elasticsearch.crawler.fs.tika.TikaDocParser.java

public static void generate(FsSettings fsSettings, InputStream inputStream, String filename, Doc doc,
        MessageDigest messageDigest, long filesize) throws IOException {
    logger.trace("Generating document [{}]", filename);
    // Extracting content with Tika
    // See #38: https://github.com/dadoonet/fscrawler/issues/38
    int indexedChars = 100000;
    if (fsSettings.getFs().getIndexedChars() != null) {
        if (fsSettings.getFs().getIndexedChars().percentage()) {
            indexedChars = (int) Math.round(filesize * fsSettings.getFs().getIndexedChars().asDouble());
            logger.trace("using percentage [{}] to define indexed chars: [{}]",
                    fsSettings.getFs().getIndexedChars(), indexedChars);
        } else {//from w ww  . j ava 2s.  co m
            indexedChars = (int) fsSettings.getFs().getIndexedChars().value();
            logger.trace("indexed chars [{}]",
                    indexedChars == -1 ? "has been disabled. All text will be extracted" : indexedChars);
        }
    }
    Metadata metadata = new Metadata();

    String parsedContent = null;

    if (messageDigest != null) {
        logger.trace("Generating hash with [{}]", messageDigest.getAlgorithm());
        inputStream = new DigestInputStream(inputStream, messageDigest);
    }

    ByteArrayOutputStream bos = null;
    if (fsSettings.getFs().isStoreSource()) {
        logger.debug("Using a TeeInputStream as we need to store the source");
        bos = new ByteArrayOutputStream();
        inputStream = new TeeInputStream(inputStream, bos);
    }

    try {
        // Set the maximum length of strings returned by the parseToString method, -1 sets no limit
        logger.trace("Beginning Tika extraction");
        parsedContent = tika().parseToString(inputStream, metadata, indexedChars);
        logger.trace("End of Tika extraction");
    } catch (Throwable e) {
        logger.debug("Failed to extract [" + indexedChars + "] characters of text for [" + filename + "]", e);
    }

    // Adding what we found to the document we want to index

    // File
    doc.getFile().setContentType(metadata.get(Metadata.CONTENT_TYPE));
    doc.getFile().setExtension(FilenameUtils.getExtension(filename));

    // We only add `indexed_chars` if we have other value than default or -1
    if (fsSettings.getFs().getIndexedChars() != null && fsSettings.getFs().getIndexedChars().value() != -1) {
        doc.getFile().setIndexedChars(indexedChars);
    }

    if (fsSettings.getFs().isAddFilesize()) {
        if (metadata.get(Metadata.CONTENT_LENGTH) != null) {
            // We try to get CONTENT_LENGTH from Tika first
            doc.getFile().setFilesize(Long.parseLong(metadata.get(Metadata.CONTENT_LENGTH)));
        }
    }
    if (messageDigest != null) {
        byte[] digest = messageDigest.digest();
        String result = "";
        // Convert to Hexa
        for (int i = 0; i < digest.length; i++) {
            result += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
        }
        doc.getFile().setChecksum(result);
    }
    // File

    // Meta
    doc.getMeta().setAuthor(metadata.get(TikaCoreProperties.CREATOR));
    doc.getMeta().setTitle(metadata.get(TikaCoreProperties.TITLE));
    String sDate = metadata.get(TikaCoreProperties.MODIFIED);
    if (sDate != null) {
        try {
            LocalDateTime date = LocalDateTime.parse(sDate, DateTimeFormatter.ISO_DATE_TIME);
            doc.getMeta().setDate(date);
        } catch (DateTimeParseException e) {
            logger.warn("Can not parse date [{}] for [{}]. Skipping date field...", sDate, filename);
        }
    }
    doc.getMeta().setKeywords(commaDelimitedListToStringArray(metadata.get(TikaCoreProperties.KEYWORDS)));

    if (fsSettings.getFs().isRawMetadata()) {
        logger.trace("Listing all available metadata:");
        for (String metadataName : metadata.names()) {
            String value = metadata.get(metadataName);
            // This is a logger trick which helps to generate our unit tests
            // You need to change test/resources/log4j2.xml fr.pilato.elasticsearch.crawler.fs.tika level to trace
            logger.trace("  assertThat(raw, hasEntry(\"{}\", \"{}\"));", metadataName, value);
            doc.getMeta().addRaw(metadataName, value);
        }
    }
    // Meta

    // Doc content
    doc.setContent(parsedContent);

    // Doc as binary attachment
    if (fsSettings.getFs().isStoreSource()) {
        doc.setAttachment(Base64.getEncoder().encodeToString(bos.toByteArray()));
    }
    logger.trace("End document generation");
    // End of our document
}

From source file:at.bitfire.davdroid.webdav.WebDavCollection.java

public boolean propfind(HttpPropfind.Mode mode) throws IOException, IncapableResourceException, HttpException {
    HttpPropfind propfind = new HttpPropfind(location, mode);
    HttpResponse response = client.execute(propfind);
    checkResponse(response);/*w w w .j  a  v a  2s.c  o  m*/

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_MULTI_STATUS) {
        DavMultistatus multistatus;
        try {
            Serializer serializer = new Persister();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream is = new TeeInputStream(response.getEntity().getContent(), baos);
            multistatus = serializer.read(DavMultistatus.class, is);

            Log.d(TAG, "Received multistatus response: " + baos.toString("UTF-8"));
        } catch (Exception e) {
            Log.w(TAG, e);
            throw new IncapableResourceException();
        }
        processMultiStatus(multistatus);
        return true;

    } else
        return false;
}

From source file:com.cedarsoft.couchdb.io.ActionFailedExceptionSerializer.java

@Nonnull
public ActionFailedException deserialize(int status, @Nonnull InputStream in)
        throws VersionException, IOException {
    try (MaxLengthByteArrayOutputStream teedOut = new MaxLengthByteArrayOutputStream();
            TeeInputStream teeInputStream = new TeeInputStream(in, teedOut)) {

        JsonFactory jsonFactory = JacksonSupport.getJsonFactory();

        JsonParser parser = jsonFactory.createJsonParser(teeInputStream);
        JacksonParserWrapper parserWrapper = new JacksonParserWrapper(parser);

        parserWrapper.nextToken(JsonToken.START_OBJECT);

        String error = null;/*w w  w . ja v a 2s.  c o  m*/
        String reason = null;

        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            String currentName = parser.getCurrentName();

            if (currentName.equals(PROPERTY_ERROR)) {
                parserWrapper.nextToken(JsonToken.VALUE_STRING);
                error = parser.getText();
                continue;
            }

            if (currentName.equals(PROPERTY_REASON)) {
                parserWrapper.nextToken(JsonToken.VALUE_STRING);
                reason = parser.getText();
                continue;
            }

            throw new IllegalStateException("Unexpected field reached <" + currentName + ">");
        }

        parserWrapper.verifyDeserialized(error, PROPERTY_ERROR);
        parserWrapper.verifyDeserialized(reason, PROPERTY_REASON);
        assert reason != null;
        assert error != null;

        parserWrapper.ensureObjectClosed();

        return new ActionFailedException(status, error, reason, teedOut.toByteArray());
    }
}

From source file:com.zimbra.cs.octosync.PatchInputStream.java

/**
 * Factory method for convenient creation of PatchInputStream using
 * BinaryPatchReader and BlobAccessViaMailbox.
 *
 * @param is Input stream with the patch data
 * @param mbox Mailbox used to access blobs
 * @param opContext The operation context
 * @param defaultFileId The file id to be the default file id
 * @param defaultVersion The default version
 * @param patchOs Output stream for the patch. If provided a byte copy
 *      of the patch will be made and written to the output stream as the patch
 *      is being processed. Can be null// ww  w.  j  av  a  2 s . c  om
 *
 * @param manifest Patch manifest to populate with references being made by
 *      the patch. Optional, can be null
 *
 * @return New instance of PatchInputStream
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ServiceException the service exception
 */
public static PatchInputStream create(InputStream is, Mailbox mbox, OperationContext opContext,
        int defaultFileId, int defaultVersion, OutputStream patchOs, PatchManifest manifest)
        throws IOException, ServiceException {
    InputStream input = null;

    if (patchOs != null) {
        input = new TeeInputStream(is, patchOs);
    } else {
        input = is;
    }

    return new PatchInputStream(new BinaryPatchReader(input),
            new BlobAccessViaMailbox(mbox, opContext, defaultFileId, defaultVersion), manifest);
}