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.codice.ddf.spatial.ogc.csw.catalog.source.CswResponseExceptionMapper.java

@Override
public CswException fromResponse(Response response) {
    CswException cswException = null;/*from   w ww. ja va2s.  c o m*/

    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                if (is.markSupported()) {
                    is.reset();
                }
                msg = IOUtils.toString(is);
            } catch (IOException e) {
                cswException = new CswException(
                        "Error received from remote Csw server" + (msg != null ? ": " + msg : ""));
                LOGGER.warn("Unable to parse exception report: {}", e);
            }
            if (msg != null) {
                try {
                    JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<ExceptionReport>();
                    Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class)
                            .createUnmarshaller();
                    ExceptionReport report = (ExceptionReport) um.unmarshal(new StringReader(msg));
                    cswException = convertToCswException(report);
                } catch (JAXBException e) {
                    cswException = new CswException("Error received from remote Csw server: " + msg, e);
                    LOGGER.warn("Error parsing the exception report: {}", e);
                }
            }
        } else {
            cswException = new CswException("Error reading response, entity type not understood: "
                    + response.getEntity().getClass().getName());
        }
        cswException.setHttpStatus(response.getStatus());
    } else {
        cswException = new CswException("Error handling response, response is null");
    }

    return cswException;
}

From source file:org.codice.ddf.spatial.ogc.csw.catalog.common.source.CswResponseExceptionMapper.java

@Override
public CswException fromResponse(Response response) {
    CswException cswException = null;// w  w w .  j av a2 s  .  c  o m

    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                if (is.markSupported()) {
                    is.reset();
                }
                msg = IOUtils.toString(is);
            } catch (IOException e) {
                cswException = new CswException(
                        "Error received from remote Csw server" + (msg != null ? ": " + msg : ""));
                LOGGER.warn("Unable to parse exception report: {}", e.getMessage());
                LOGGER.debug("Unable to parse exception report: {}", e);
            }
            if (msg != null) {
                try {
                    JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<ExceptionReport>();
                    Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class)
                            .createUnmarshaller();
                    ExceptionReport report = (ExceptionReport) um.unmarshal(new StringReader(msg));
                    cswException = convertToCswException(report);
                } catch (JAXBException e) {
                    cswException = new CswException("Error received from remote Csw server: " + msg, e);
                    LOGGER.warn("Error parsing the exception report: {}", e.getMessage());
                    LOGGER.debug("Error parsing the exception report", e);
                }
            }
        } else {
            cswException = new CswException("Error reading response, entity type not understood: "
                    + response.getEntity().getClass().getName());
        }
        cswException.setHttpStatus(response.getStatus());
    } else {
        cswException = new CswException("Error handling response, response is null");
    }

    return cswException;
}

From source file:org.gytheio.messaging.jackson.QpidJsonBodyCleanerObjectMapper.java

public <T> T readValue(InputStream inputStream, Class<T> valueType)
        throws JsonParseException, JsonMappingException, IOException {
    try {/* w ww  . j  av  a2 s  .  c om*/
        // Try to unmarshal normally
        if (inputStream.markSupported()) {
            inputStream.mark(1024 * 512);
        }
        return super.readValue(inputStream, valueType);
    } catch (JsonParseException e) {
        if (!inputStream.markSupported()) {
            // We can't reset this stream, bail out
            throw e;
        }
        // Reset the stream
        inputStream.reset();
    }
    // Clean the message body and try again
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, DEFAULT_ENCODING);
    String content = writer.toString();
    content = content.substring(content.indexOf("{"), content.length());
    return readValue(content, valueType);
}

From source file:org.archive.wayback.core.Resource.java

protected void setInputStream(InputStream is) {
    if (is.markSupported()) {
        this.is = is;
    } else {//from w ww . j  a  v a  2s .  c o  m
        this.is = new BufferedInputStream(is);
    }
}

From source file:edu.utah.further.core.cxf.ResponseWsExceptionMapper.java

/**
 * @param r// w  ww  .  j  a va 2s  .  co  m
 * @return
 * @see org.apache.cxf.jaxrs.client.ResponseExceptionMapper#fromResponse(javax.ws.rs.core.Response)
 */
@SuppressWarnings("resource")
// is is closed in finally block
@Override
public WsException fromResponse(final Response r) {
    if (log.isDebugEnabled()) {
        log.debug("Mapping r " + r);
    }

    InputStream is = null;
    try {
        is = getResponseInputStream(r);
        if (is.markSupported()) {
            // FUR-1331: if we can create a string copy of the input stream, check if
            // it's empty and protected against unmarshalled exceptions
            final ByteArrayOutputStream copy = IoUtil.copyInputStream(is);
            final String errorXml = new String(copy.toByteArray());
            copy.close();
            if (log.isDebugEnabled()) {
                log.debug("Error XML:" + errorXml);
            }
            if (StringUtils.isBlank(errorXml)) {
                return new WsException(ErrorCode.INTERNAL_ERROR,
                        "An error without an explicit message has occurred");
            }
        }
        final ApplicationError error = getXmlService().unmarshal(is, ApplicationError.class);
        return new WsException(error);
    } catch (final Throwable e) {
        return new WsException(ErrorCode.INTERNAL_ERROR, "Failed to unmarshal exception from XML: ", e);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.cytoscape.io.internal.read.GenericReaderManager.java

public R getReader(InputStream stream, String inputName) {
    try {//w ww . jav a 2 s  .c  o  m
        if (!stream.markSupported()) {
            stream = new BufferedInputStream(stream);
            stream.mark(1025);
        }

        for (T factory : factories) {
            CyFileFilter cff = factory.getFileFilter();
            logger.debug("trying READER: " + factory + " with filter: " + cff);

            // Because we don't know who will provide the file filter or
            // what they might do with the InputStream, we provide a copy
            // of the first 2KB rather than the stream itself.
            if (cff.accepts(CopyInputStream.copyKBytes(stream, 1), category)) {
                logger.debug("successfully matched READER " + factory);
                return (R) factory.createTaskIterator(stream, inputName).next();
            }
        }
    } catch (IOException ioe) {
        logger.warn("Error setting input stream", ioe);
    }

    logger.warn("No reader found for input stream");
    return null;
}

From source file:org.jbpm.console.ng.documents.backend.server.DocumentViewServlet.java

private void uploadFile(final FileItem uploadItem, String folder) throws IOException {
    InputStream fileData = uploadItem.getInputStream();
    // GAV gav = uploadItem.getGav();

    try {/*from  w w w .  j a  v a 2  s .  c o m*/
        // if ( gav == null ) {
        if (!fileData.markSupported()) {
            fileData = new BufferedInputStream(fileData);
        }

        // is available() safe?
        fileData.mark(fileData.available());

        byte[] bytes = IOUtils.toByteArray(fileData);
        DocumentSummary documenSummary = new DocumentSummary(uploadItem.getName(), "", folder);
        documenSummary.setContent(bytes);
        this.documentService.createDocument(documenSummary);
    } catch (Exception e) {

    }
}

From source file:com.smartitengineering.util.rest.client.jersey.cache.CacheableClientHandler.java

protected InputStream getEntityStream(HTTPResponse cachedResponse) {
    final InputStream entity;
    if (cachedResponse.hasPayload()) {
        final InputStream inputStream = cachedResponse.getPayload().getInputStream();
        if (inputStream.markSupported()) {
            entity = inputStream;// ww w .  j  a v  a 2 s . c  o  m
        } else {
            entity = new BufferedInputStream(inputStream, ReaderWriter.BUFFER_SIZE);
        }
    } else {
        entity = new ByteArrayInputStream(new byte[0]);
    }
    return entity;
}

From source file:edu.utah.further.core.xml.chain.UnmarshallRequestProcessorImpl.java

/**
 * @param inputStream/*from ww w  .j  a  v  a2  s  .  com*/
 */
private void printInputXmlForDebugging(final InputStream inputStream) {
    if (log.isTraceEnabled() && inputStream.markSupported()) {
        inputStream.mark(0);
        log.trace("Unmarshalling input is " + IoUtil.getInputStreamAsString(inputStream));
        try {
            inputStream.reset();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:TypeUtil.java

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

    int i = 0;/*from  w w  w  . j a  v a2 s  . c om*/
    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;
}