Example usage for java.io InputStream markSupported

List of usage examples for java.io InputStream markSupported

Introduction

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

Prototype

public boolean markSupported() 

Source Link

Document

Tests if this input stream supports the mark and reset methods.

Usage

From source file:org.mule.transport.http.multipart.MultiPartInputStream.java

private byte[] readLine(InputStream in) throws IOException {
    byte[] buf = new byte[256];

    int i = 0;//from  w  ww.  j a v a2 s . co m
    int loops = 0;
    int ch = 0;

    while (true) {
        ch = in.read();
        if (ch < 0)
            break;
        loops++;

        // skip a leading LF's
        if (loops == 1 && ch == LF)
            continue;

        if (ch == CR || ch == LF)
            break;

        if (i >= buf.length) {
            byte[] old_buf = buf;
            buf = new byte[old_buf.length + 256];
            System.arraycopy(old_buf, 0, buf, 0, old_buf.length);
        }
        buf[i++] = (byte) ch;
    }

    if (ch == -1 && i == 0)
        return null;

    // skip a trailing LF if it exists
    if (ch == CR && in.available() >= 1 && in.markSupported()) {
        in.mark(1);
        ch = in.read();
        if (ch != LF)
            in.reset();
    }

    byte[] old_buf = buf;
    buf = new byte[i];
    System.arraycopy(old_buf, 0, buf, 0, i);

    return buf;
}

From source file:org.springframework.extensions.webscripts.connector.AuthenticatingConnector.java

public Response call(String uri, ConnectorContext context, InputStream in, OutputStream out) {
    Response response = null;/*  ww w .  j  a va 2 s .c  o  m*/
    boolean handshake = false;
    boolean firstcall = true;

    if (isAuthenticated()) {
        // try to call into the connector to see if we can successfully do this
        context.setCommitResponseOnAuthenticationError(false);
        response = this.connector.call(uri, context, in, out);
        firstcall = false;

        if (logger.isDebugEnabled())
            logger.debug("Received " + response.getStatus().getCode() + " on first call to: " + uri);

        // if there was an authentication challenge, handle here
        if (response.getStatus().getCode() == ResponseStatus.STATUS_UNAUTHORIZED) {
            handshake = true;
        }
    } else {
        handshake = true;
    }

    if (handshake) {
        handshake(); // ignore result

        // now that we've authenticated, try again
        if (in.markSupported()) {
            try {
                in.reset();
            } catch (IOException ioErr) {
                // if we cannot reset the stream - there's nothing else we can do
            }
        }
        context.setCommitResponseOnAuthenticationError(true);
        response = this.connector.call(uri, context, in, out);

        if (logger.isDebugEnabled())
            logger.debug("Received " + response.getStatus().getCode() + " on "
                    + (firstcall ? "first" : "second") + " call to: " + uri);
    }

    return response;
}

From source file:org.fireflow.client.impl.WorkflowStatementLocalImpl.java

public ProcessDescriptor uploadProcessStream(InputStream stream, boolean publishState, String bizCategory,
        String ownerDeptId) throws InvalidModelException {
    InputStream inStream = stream;
    if (!inStream.markSupported()) {
        inStream = new BufferedInputStream(stream);
    }//from   www . j  a va2 s . c  o  m

    RuntimeContext ctx = this.session.getRuntimeContext();

    ProcessUtil processUtil = ctx.getEngineModule(ProcessUtil.class, processType);
    //Process???
    //WorkflowProcess ?Import2012-04-30
    Object processObject = processUtil.deserializeXml2Process(inStream);

    //?Xml

    String processXml = null;
    try {
        String charset = Utils.findXmlCharset(inStream);
        processXml = Utils.inputStream2String(inStream, charset);
    } catch (IOException e) {
        throw new InvalidModelException(e);
    }

    return this._uploadProcess(processObject, processXml, publishState, bizCategory, ownerDeptId);
}

From source file:eu.medsea.mimeutil.MimeUtil2.java

/**
 * Get all of the matching mime types for this InputStream object.
 * The method delegates down to each of the registered MimeHandler(s) and returns a
 * normalised list of all matching mime types. If no matching mime types are found the returned
 * Collection will contain the unknownMimeType passed in.
 * @param in the InputStream object to detect.
 * @param unknownMimeType./*ww w  . j  a  v a 2s.  co m*/
 * @return the Collection of matching mime types. If the collection would be empty i.e. no matches then this will
 * contain the passed in parameter unknownMimeType
 * @throws MimeException if there are problems such as reading files generated when the MimeHandler(s)
 * executed.
 */
public final Collection getMimeTypes(final InputStream in, final MimeType unknownMimeType)
        throws MimeException {
    Collection mimeTypes = new MimeTypeHashSet();

    if (in == null) {
        log.error("InputStream reference cannot be null.");
    } else {
        if (!in.markSupported()) {
            throw new MimeException("InputStream must support the mark() and reset() methods.");
        }
        if (log.isDebugEnabled()) {
            log.debug("Getting MIME types for InputSteam [" + in + "].");
        }
        mimeTypes.addAll(mimeDetectorRegistry.getMimeTypes(in));

        // We don't want the unknownMimeType added to the collection by MimeDetector(s)
        mimeTypes.remove(unknownMimeType);
    }
    // If the collection is empty we want to add the unknownMimetype
    if (mimeTypes.isEmpty()) {
        mimeTypes.add(unknownMimeType);
    }
    if (log.isDebugEnabled()) {
        log.debug("Retrieved MIME types [" + mimeTypes.toString() + "]");
    }
    return mimeTypes;
}

From source file:org.apache.tika.gui.TikaGUI.java

private void handleStream(InputStream input, Metadata md) throws Exception {
    StringWriter htmlBuffer = new StringWriter();
    StringWriter textBuffer = new StringWriter();
    StringWriter textMainBuffer = new StringWriter();
    StringWriter xmlBuffer = new StringWriter();
    StringBuilder metadataBuffer = new StringBuilder();

    ContentHandler handler = new TeeContentHandler(getHtmlHandler(htmlBuffer),
            getTextContentHandler(textBuffer), getTextMainContentHandler(textMainBuffer),
            getXmlContentHandler(xmlBuffer));

    context.set(DocumentSelector.class, new ImageDocumentSelector());

    input = TikaInputStream.get(new ProgressMonitorInputStream(this, "Parsing stream", input));

    if (input.markSupported()) {
        int mark = -1;
        if (input instanceof TikaInputStream) {
            if (((TikaInputStream) input).hasFile()) {
                mark = (int) ((TikaInputStream) input).getLength();
            }//from ww  w  . j a  va2  s  . co  m
        }
        if (mark == -1) {
            mark = MAX_MARK;
        }
        input.mark(mark);
    }
    parser.parse(input, handler, md, context);

    String[] names = md.names();
    Arrays.sort(names);
    for (String name : names) {
        for (String val : md.getValues(name)) {
            metadataBuffer.append(name);
            metadataBuffer.append(": ");
            metadataBuffer.append(val);
            metadataBuffer.append("\n");
        }
    }

    String name = md.get(Metadata.RESOURCE_NAME_KEY);
    if (name != null && name.length() > 0) {
        setTitle("Apache Tika: " + name);
    } else {
        setTitle("Apache Tika: unnamed document");
    }

    setText(metadata, metadataBuffer.toString());
    setText(xml, xmlBuffer.toString());
    setText(text, textBuffer.toString());
    setText(textMain, textMainBuffer.toString());
    setText(html, htmlBuffer.toString());
    if (!input.markSupported()) {
        setText(json, "InputStream does not support mark/reset for Recursive Parsing");
        layout.show(cards, "metadata");
        return;
    }
    boolean isReset = false;
    try {
        input.reset();
        isReset = true;
    } catch (IOException e) {
        setText(json,
                "Error during stream reset.\n" + "There's a limit of " + MAX_MARK
                        + " bytes for this type of processing in the GUI.\n"
                        + "Try the app with command line argument of -J.");
    }
    if (isReset) {
        RecursiveParserWrapper wrapper = new RecursiveParserWrapper(parser,
                new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.BODY, -1));
        wrapper.parse(input, null, new Metadata(), new ParseContext());
        StringWriter jsonBuffer = new StringWriter();
        JsonMetadataList.setPrettyPrinting(true);
        JsonMetadataList.toJson(wrapper.getMetadata(), jsonBuffer);
        setText(json, jsonBuffer.toString());
    }
    layout.show(cards, "metadata");
}

From source file:SoundManagerTest.java

/**
 * Creates an AudioInputStream from a sound from an input stream
 *//*w  ww.  ja  v a2  s  .  c om*/
public AudioInputStream getAudioInputStream(InputStream is) {

    try {
        if (!is.markSupported()) {
            is = new BufferedInputStream(is);
        }
        // open the source stream
        AudioInputStream source = AudioSystem.getAudioInputStream(is);

        // convert to playback format
        return AudioSystem.getAudioInputStream(playbackFormat, source);
    } catch (UnsupportedAudioFileException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:de.micromata.genome.test.web.SimHttpServletRequest.java

protected void setInputStream(final InputStream servletIs) {
    this.servletIs = new ServletInputStream() {

        public int available() throws IOException {
            return servletIs.available();
        }/* w w  w.  ja v  a 2s .  c  o m*/

        public void close() throws IOException {
            servletIs.close();
        }

        public boolean equals(Object obj) {
            return servletIs.equals(obj);
        }

        public int hashCode() {
            return servletIs.hashCode();
        }

        public void mark(int readlimit) {
            servletIs.mark(readlimit);
        }

        public boolean markSupported() {
            return servletIs.markSupported();
        }

        public int read(byte[] b, int off, int len) throws IOException {
            return servletIs.read(b, off, len);
        }

        public int read(byte[] b) throws IOException {
            return servletIs.read(b);
        }

        public void reset() throws IOException {
            servletIs.reset();
        }

        public long skip(long n) throws IOException {
            return servletIs.skip(n);
        }

        public String toString() {
            return servletIs.toString();
        }

        @Override
        public int read() throws IOException {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.apache.tika.parser.pkg.PackageParser.java

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    //lazily load the MediaTypeRegistry at parse time
    //only want to call getDefaultConfig() once, and can't
    //load statically because of the ForkParser
    TikaConfig config = context.get(TikaConfig.class);
    MediaTypeRegistry mediaTypeRegistry = null;
    if (config != null) {
        mediaTypeRegistry = config.getMediaTypeRegistry();
    } else {/*from w  ww  .j a  v a2s  .c  o m*/
        if (bufferedMediaTypeRegistry == null) {
            //buffer this for next time.
            synchronized (lock) {
                //now that we're locked, check again
                if (bufferedMediaTypeRegistry == null) {
                    bufferedMediaTypeRegistry = TikaConfig.getDefaultConfig().getMediaTypeRegistry();
                }
            }
        }
        mediaTypeRegistry = bufferedMediaTypeRegistry;
    }

    // Ensure that the stream supports the mark feature
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }

    TemporaryResources tmp = new TemporaryResources();
    ArchiveInputStream ais = null;
    try {
        ArchiveStreamFactory factory = context.get(ArchiveStreamFactory.class, new ArchiveStreamFactory());
        // At the end we want to close the archive stream to release
        // any associated resources, but the underlying document stream
        // should not be closed

        ais = factory.createArchiveInputStream(new CloseShieldInputStream(stream));

    } catch (StreamingNotSupportedException sne) {
        // Most archive formats work on streams, but a few need files
        if (sne.getFormat().equals(ArchiveStreamFactory.SEVEN_Z)) {
            // Rework as a file, and wrap
            stream.reset();
            TikaInputStream tstream = TikaInputStream.get(stream, tmp);

            // Seven Zip suports passwords, was one given?
            String password = null;
            PasswordProvider provider = context.get(PasswordProvider.class);
            if (provider != null) {
                password = provider.getPassword(metadata);
            }

            SevenZFile sevenz;
            if (password == null) {
                sevenz = new SevenZFile(tstream.getFile());
            } else {
                sevenz = new SevenZFile(tstream.getFile(), password.getBytes("UnicodeLittleUnmarked"));
            }

            // Pending a fix for COMPRESS-269 / TIKA-1525, this bit is a little nasty
            ais = new SevenZWrapper(sevenz);
        } else {
            tmp.close();
            throw new TikaException("Unknown non-streaming format " + sne.getFormat(), sne);
        }
    } catch (ArchiveException e) {
        tmp.close();
        throw new TikaException("Unable to unpack document stream", e);
    }

    updateMediaType(ais, mediaTypeRegistry, metadata);
    // Use the delegate parser to parse the contained document
    EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);

    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();

    try {
        ArchiveEntry entry = ais.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory()) {
                parseEntry(ais, entry, extractor, metadata, xhtml);
            }
            entry = ais.getNextEntry();
        }
    } catch (UnsupportedZipFeatureException zfe) {
        // If it's an encrypted document of unknown password, report as such
        if (zfe.getFeature() == Feature.ENCRYPTION) {
            throw new EncryptedDocumentException(zfe);
        }
        // Otherwise throw the exception
        throw new TikaException("UnsupportedZipFeature", zfe);
    } catch (PasswordRequiredException pre) {
        throw new EncryptedDocumentException(pre);
    } finally {
        ais.close();
        tmp.close();
    }

    xhtml.endDocument();
}

From source file:org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.java

/**
 * Unmarshal XML data from the specified InputStream and return the 
 * resulting content tree.  Validation event location information may
 * be incomplete when using this form of the unmarshal API.
 *
 * <p>//from   w  w w  .  ja  v  a2 s  .com
 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
 * 
 * @param is the InputStream to unmarshal XML data from
 * @return the newly created root object of the java content tree 
 *
 * @throws JAXBException 
 *     If any unexpected errors occur while unmarshalling
 */
@Override
public E unmarshal(java.io.InputStream is) throws JAXBException {
    try {

        log.debug("For " + this.getClass().getName() + ", unmarshall via binder");
        // InputStream to Document
        org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is);

        // 
        binder = jc.createBinder();

        log.debug("info: " + binder.getClass().getName());

        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
        eventHandler.setContinue(false);
        binder.setEventHandler(eventHandler);

        try {
            unwrapUsually(binder, doc); // unlikely to need this in the code below

        } catch (Exception ue) {

            if (ue instanceof UnmarshalException) {
                // Usually..
            } else {
                // eg java.lang.NumberFormatException
                log.warn(ue.getMessage(), ue);
            }

            if (is.markSupported()) {
                // When reading from zip, we use a ByteArrayInputStream,
                // which does support this.

                log.info("encountered unexpected content in " + this.getPartName() + "; pre-processing");
                /* Always try our preprocessor, since if what is first encountered is
                 * eg:
                 * 
                       <w14:glow w14:rad="101600"> ...
                 *
                 * the error would be:
                 *  
                 *    unexpected element (uri:"http://schemas.microsoft.com/office/word/2010/wordml", local:"glow")
                 *
                 * but there could well be mc:AlternateContent somewhere 
                 * further down in the document.
                 */

                // mimic docx4j 2.7.0 and earlier behaviour; this will 
                // drop w14:glow etc; the preprocessor doesn't need to 
                // do that            
                eventHandler.setContinue(true);

                // There is no JAXBResult(binder),
                // so use a 
                DOMResult result = new DOMResult();

                Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();
                XmlUtils.transform(doc, mcPreprocessorXslt, null, result);

                doc = (org.w3c.dom.Document) result.getNode();
                try {
                    jaxbElement = (E) XmlUtils.unwrap(binder.unmarshal(doc));
                } catch (ClassCastException cce) {
                    /* 
                     * Work around for issue with JAXB binder, in Java 1.6 
                     * encountered with /src/test/resources/jaxb-binder-issue.docx 
                     * See http://old.nabble.com/BinderImpl.associativeUnmarshal-ClassCastException-casting-to-JAXBElement-td32456585.html
                     * and  http://java.net/jira/browse/JAXB-874
                     * 
                     * java.lang.ClassCastException: org.docx4j.wml.PPr cannot be cast to javax.xml.bind.JAXBElement
                       at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$IntercepterLoader.intercept(Unknown Source)
                       at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(Unknown Source)
                       at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.endElement(Unknown Source)
                       at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source)
                       at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source)
                       at com.sun.xml.internal.bind.v2.runtime.BinderImpl.associativeUnmarshal(Unknown Source)
                       at com.sun.xml.internal.bind.v2.runtime.BinderImpl.unmarshal(Unknown Source)
                       at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.unmarshal(MainDocumentPart.java:321)
                     */

                    log.warn("Binder not available for this docx");
                    Unmarshaller u = jc.createUnmarshaller();
                    jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(doc));

                }
            } else {
                log.warn("problem in " + this.getPartName());
                log.warn(ue.getMessage(), ue);
                log.warn(".. and mark not supported");
                throw ue;
            }
        }

        return jaxbElement;

    } catch (Exception e) {
        //         e.printStackTrace();
        //         return null;

        /* java.lang.NullPointerException
           at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDProcessor.startDTD(Unknown Source)
           at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDInternalSubset(Unknown Source)
           at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(Unknown Source)
           at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(Unknown Source)
           at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
         */
        for (int i = 0; i < e.getStackTrace().length; i++) {
            if (e.getStackTrace()[i].getClassName().contains("DTD")
                    || e.getStackTrace()[i].getMethodName().contains("DTD")) {
                // Mimic Word 2010 message
                throw new JAXBException("DTD is prohibited", e);
            }
        }

        throw new JAXBException(e.getMessage(), e);
    }
}

From source file:core.com.qiniu.http.AmazonHttpClient.java

/**
 * Returns true if a failed request should be retried.
 *
 * @param originalRequest The original service request that is being
 *            executed.// w  w w . j  av  a 2 s . com
 * @param inputStream The current HTTP method being executed.
 * @param exception The client/service exception from the failed request.
 * @param requestCount The number of times the current request has been
 *            attempted.
 * @return True if the failed request should be retried.
 */
private boolean shouldRetry(AmazonWebServiceRequest originalRequest, InputStream inputStream,
        AmazonClientException exception, int requestCount, RetryPolicy retryPolicy) {
    final int retries = requestCount - 1;

    int maxErrorRetry = config.getMaxErrorRetry();
    // We should use the maxErrorRetry in
    // the RetryPolicy if either the user has not explicitly set it in
    // ClientConfiguration, or the RetryPolicy is configured to take
    // higher precedence.
    if (maxErrorRetry < 0 || !retryPolicy.isMaxErrorRetryInClientConfigHonored()) {
        maxErrorRetry = retryPolicy.getMaxErrorRetry();
    }

    // Immediately fails when it has exceeds the max retry count.
    if (retries >= maxErrorRetry)
        return false;

    // Never retry on requests containing non-repeatable entity
    if (inputStream != null && !inputStream.markSupported()) {
        if (log.isDebugEnabled()) {
            log.debug("Content not repeatable");
        }
        return false;
    }

    // Pass all the context information to the RetryCondition and let it
    // decide whether it should be retried.
    return retryPolicy.getRetryCondition().shouldRetry(originalRequest, exception, retries);
}