Example usage for java.io InputStream reset

List of usage examples for java.io InputStream reset

Introduction

In this page you can find the example usage for java.io InputStream reset.

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

Repositions this stream to the position at the time the mark method was last called on this input stream.

Usage

From source file:uk.bl.wa.analyser.WARCPayloadAnalysers.java

/**
 * /* ww w  .jav  a2  s. co m*/
 * @param source
 * @param header
 * @param tikainput
 * @param solr
 */
public void analyse(String source, ArchiveRecordHeader header, InputStream tikainput, SolrRecord solr,
        long content_length) {
    final String url = Normalisation.sanitiseWARCHeaderValue(header.getUrl());
    log.debug("Analysing " + url);

    final long start = System.nanoTime();

    // Always run Tika first:
    // (this ensures the SOLR_CONTENT_TYPE is set)
    tika.analyse(source, header, tikainput, solr);

    // Now run the others:
    for (AbstractPayloadAnalyser provider : providers) {
        String mimeType = (String) solr.getField(SolrFields.SOLR_CONTENT_TYPE).getValue();
        if (provider.shouldProcess(mimeType)) {
            try {
                // Reset input stream before running each parser:
                tikainput.reset();
                // Run the parser:
                provider.analyse(source, header, tikainput, solr);
            } catch (Exception i) {
                log.error(i + ": " + i.getMessage() + ";x; " + url + "@" + header.getOffset(), i);
            }
        }
    }

    // Derive normalised/simplified content type:
    processContentType(solr, header, content_length, false);

    // End
    Instrument.timeRel("WARCIndexer.extract#analyzetikainput", "WARCPayloadAnalyzers.analyze#total", start);

}

From source file:com.hortonworks.streamline.streams.service.CustomProcessorUploadHandler.java

@Override
public void created(Path path) {
    File createdFile = path.toFile();
    LOG.info("Created called with " + createdFile);
    boolean succeeded = false;
    try {/*from   w  w w  . j  a va  2  s  .c  o m*/
        if (createdFile.getName().endsWith(".tar")) {
            LOG.info("Processing file at " + path);
            CustomProcessorInfo customProcessorInfo = this.getCustomProcessorInfo(createdFile);
            if (customProcessorInfo == null) {
                LOG.warn("No information found for CustomProcessorRuntime in " + createdFile);
                return;
            }
            InputStream jarFile = this.getJarFile(customProcessorInfo, createdFile);
            if (jarFile == null) {
                LOG.warn("No jar file found for CustomProcessorRuntime in " + createdFile);
                return;
            }
            File tempJarFile = FileUtil.writeInputStreamToTempFile(jarFile, ".jar");
            ProxyUtil<CustomProcessorRuntime> customProcessorProxyUtil = new ProxyUtil<>(
                    CustomProcessorRuntime.class);
            CustomProcessorRuntime customProcessorRuntime = customProcessorProxyUtil.loadClassFromJar(
                    tempJarFile.getAbsolutePath(), customProcessorInfo.getCustomProcessorImpl());
            jarFile.reset();
            this.catalogService.addCustomProcessorInfoAsBundle(customProcessorInfo, jarFile);
            succeeded = true;
        } else {
            LOG.info("Failing unsupported file that was received: " + path);
        }
    } catch (IOException e) {
        LOG.warn("Exception occured while processing tar file: " + createdFile, e);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        LOG.warn("Could not load a class from jar file implementing CustomProcessorRuntime interface from "
                + createdFile, e);
    } catch (ComponentConfigException e) {
        LOG.warn("UI specification for custom processor incorrect for custom processor file: " + createdFile,
                e);
    } finally {
        try {
            if (succeeded) {
                LOG.info("CustomProcessorRuntime uploaded successfully from  " + createdFile
                        + " Moving file to " + uploadSuccessPath);
                moveFileToSuccessDirectory(createdFile);
            } else {
                LOG.warn("CustomProcessorRuntime failed to upload from " + createdFile + " Moving file to "
                        + uploadFailPath);
                moveFileToFailDirectory(createdFile);
            }
        } catch (IOException e1) {
            LOG.warn("Error moving " + createdFile.getAbsolutePath() + " to "
                    + (succeeded ? uploadSuccessPath : uploadFailPath), e1);
        }
    }
}

From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializerTest.java

@Test
public void testUnmarshalEntireState() throws Exception {
    // First, create a state file to unmarshal
    // 1. It must be a zip archive; unmarshalling a non-archive package state file isn't supported
    // 2. We use the liveMarshallerMap; no mocks.
    // 3. We use a live ArchiveStreamFactory
    underTest.setArchive(true);//w w  w  . j  a v a 2 s.c  o  m
    underTest.setMarshallerMap(liveMarshallerMap);
    underTest.setArxStreamFactory(new ZipArchiveStreamFactory());
    File tmp = File.createTempFile(this.getClass().getName() + "_UnmarshalEntireState", ".zip");
    OutputStream out = new FileOutputStream(tmp);
    underTest.serialize(state, out);
    assertTrue(tmp.exists() && tmp.length() > 1);

    // Verify that we wrote out a zip file.
    final byte[] signature = new byte[12];
    InputStream in = new BufferedInputStream(new FileInputStream(tmp));
    in.mark(signature.length);
    int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(in, signature);
    assertTrue(ZipArchiveInputStream.matches(signature, bytesRead));
    in.reset();

    // Create a new instance of PackageState, and deserialize the zip archive created above, which contains the
    // test objects from the {@link #state prepared instance} of PackageState
    PackageState state = new PackageState(); // a new instance of PackageState with no internal state
    underTest.deserialize(state, in);

    Map<StreamId, PropertyDescriptor> pds = SerializationAnnotationUtil
            .getStreamDescriptors(PackageState.class);
    assertTrue(pds.size() > 0);
    pds.keySet().stream().forEach(streamId -> {
        try {
            assertNotNull("Expected non-null value for PackageState field '" + pds.get(streamId).getName()
                    + "', " + "StreamId '" + streamId + "'", pds.get(streamId).getReadMethod().invoke(state));
        } catch (IllegalAccessException | InvocationTargetException e) {
            fail(e.getMessage());
        }
    });

    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tmp);
}

From source file:cn.ctyun.amazonaws.auth.AbstractAWSSigner.java

/**
 * Returns the request's payload contents as binary data, without processing
 * any query string params (i.e. no form encoding for query params).
 *
 * @param request//from  w ww . j a  v a 2 s .c o m
 *            The request
 * @return The request's payload contents as binary data, not including any
 *         form encoding of query string params.
 */
protected byte[] getBinaryRequestPayloadWithoutQueryParams(Request<?> request) {
    InputStream content = getBinaryRequestPayloadStreamWithoutQueryParams(request);

    try {
        content.mark(-1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 5];
        while (true) {
            int bytesRead = content.read(buffer);
            if (bytesRead == -1)
                break;

            byteArrayOutputStream.write(buffer, 0, bytesRead);
        }

        byteArrayOutputStream.close();
        content.reset();

        return byteArrayOutputStream.toByteArray();
    } catch (Exception e) {
        throw new AmazonClientException("Unable to read request payload to sign request: " + e.getMessage(), e);
    }
}

From source file:pt.lunacloud.auth.AbstractAWSSigner.java

/**
 * Returns the request's payload contents as binary data, without processing
 * any query string params (i.e. no form encoding for query params).
 *
 * @param request/*from w w  w  .ja va 2 s.c o m*/
 *            The request
 * @return The request's payload contents as binary data, not including any
 *         form encoding of query string params.
 */
protected byte[] getBinaryRequestPayloadWithoutQueryParams(Request<?> request) {
    InputStream content = getBinaryRequestPayloadStreamWithoutQueryParams(request);

    try {
        content.mark(-1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 5];
        while (true) {
            int bytesRead = content.read(buffer);
            if (bytesRead == -1)
                break;

            byteArrayOutputStream.write(buffer, 0, bytesRead);
        }

        byteArrayOutputStream.close();
        content.reset();

        return byteArrayOutputStream.toByteArray();
    } catch (Exception e) {
        throw new LunacloudClientException("Unable to read request payload to sign request: " + e.getMessage(),
                e);
    }
}

From source file:com.sina.auth.AbstractAWSSigner.java

/**
 * Returns the request's payload contents as binary data, without processing
 * any query string params (i.e. no form encoding for query params).
 *
 * @param request//w ww . j a v a2 s.  co  m
 *            The request
 * @return The request's payload contents as binary data, not including any
 *         form encoding of query string params.
 */
protected byte[] getBinaryRequestPayloadWithoutQueryParams(Request<?> request) {
    InputStream content = getBinaryRequestPayloadStreamWithoutQueryParams(request);

    try {
        content.mark(-1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 5];
        while (true) {
            int bytesRead = content.read(buffer);
            if (bytesRead == -1)
                break;

            byteArrayOutputStream.write(buffer, 0, bytesRead);
        }

        byteArrayOutputStream.close();
        content.reset();

        return byteArrayOutputStream.toByteArray();
    } catch (Exception e) {
        throw new SCSClientException("Unable to read request payload to sign request: " + e.getMessage(), e);
    }
}

From source file:org.pentaho.platform.util.XmlHelperTest.java

public void doEncodingTest(String rootElementName, String rootElementText, String encoding) throws Exception {
    // Create the test document.
    Element rootElement = new DefaultElement(rootElementName);
    Document document = DocumentHelper.createDocument(rootElement);
    rootElement.setText(rootElementText);

    // Write out the document to a byte array.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XmlDom4JHelper.saveDom(document, outputStream, encoding);

    // Read in the document from the byte array, and make sure it's decoded properly.
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    // Read in the XML string using a java reader and make sure the decoded string
    // contains the same strings as the encoded dom4j document.
    BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, encoding));
    StringBuffer stringBuffer = new StringBuffer();
    int intValue = in.read();
    while (intValue != -1) {
        stringBuffer.append((char) intValue);
        intValue = in.read();//w w w  .  ja v a2 s .c om
    }
    assertTrue(stringBuffer.toString().indexOf(rootElementName) != -1);
    assertTrue(stringBuffer.toString().indexOf(rootElementText) != -1);

    inputStream.reset();

    // Read in the XML string using the dom4j api and make sure the decoded xml
    // contains the same strings as the original document.
    document = XmlDom4JHelper.getDocFromStream(inputStream);
    assertEquals(rootElementName, document.getRootElement().getName());
    assertEquals(rootElementText, document.getRootElement().getText());
}

From source file:com.graphhopper.reader.osm.OSMInputFile.java

@SuppressWarnings("unchecked")
private InputStream decode(File file) throws IOException {
    final String name = file.getName();

    InputStream ips = null;
    try {/*from   ww  w.ja  v a 2  s .  c o  m*/
        ips = new BufferedInputStream(new FileInputStream(file), 50000);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    ips.mark(10);

    // check file header
    byte header[] = new byte[6];
    if (ips.read(header) < 0)
        throw new IllegalArgumentException("Input file is not of valid type " + file.getPath());

    /*     can parse bz2 directly with additional lib
     if (header[0] == 'B' && header[1] == 'Z')
     {
     return new CBZip2InputStream(ips);
     }
     */
    if (header[0] == 31 && header[1] == -117) {
        ips.reset();
        return new GZIPInputStream(ips, 50000);
    } else if (header[0] == 0 && header[1] == 0 && header[2] == 0 && header[4] == 10 && header[5] == 9
            && (header[3] == 13 || header[3] == 14)) {
        ips.reset();
        binary = true;
        return ips;
    } else if (header[0] == 'P' && header[1] == 'K') {
        ips.reset();
        ZipInputStream zip = new ZipInputStream(ips);
        zip.getNextEntry();

        return zip;
    } else if (name.endsWith(".osm") || name.endsWith(".xml")) {
        ips.reset();
        return ips;
    } else if (name.endsWith(".bz2") || name.endsWith(".bzip2")) {
        String clName = "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream";
        try {
            Class clazz = Class.forName(clName);
            ips.reset();
            Constructor<InputStream> ctor = clazz.getConstructor(InputStream.class, boolean.class);
            return ctor.newInstance(ips, true);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot instantiate " + clName, e);
        }
    } else {
        throw new IllegalArgumentException("Input file is not of valid type " + file.getPath());
    }
}

From source file:org.obm.push.mail.MailBackendImpl.java

private void tryToStoreInSent(UserDataRequest udr, SendEmail email, InputStream emailStream) {
    try {//from  w ww .ja v a 2  s .  c o  m
        emailStream.reset();
        mailboxService.storeInSent(udr,
                multipleTimesReadable(emailStream, email.getMimeMessage().getCharset()));
    } catch (CollectionNotFoundException e) {
        logger.warn("Cannot store an email in the Sent folder, the collection was not found: {}",
                e.getMessage());
    } catch (Throwable t) {
        logger.error("Cannot store an email in the Sent folder", t);
    }
}

From source file:edu.harvard.hmdc.dvnplugin.DVNOAIUrlCacher.java

/**
 * Try to reset the provided input stream, if we can't then return
 * new input stream for the given url//from   ww w .  j av a2s.  c  om
 */
private InputStream resetInputStream(InputStream is, String url, String lastModified) throws IOException {
    try {
        is.reset();
    } catch (IOException e) {
        logger.debug("Couldn't reset input stream, so getting new one", e);
        is.close();
        releaseConnection();
        is = new BufferedInputStream(getUncachedInputStreamOnly(lastModified));
    }
    return is;
}