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:net.sf.sail.webapp.dao.sds.impl.AbstractHttpRestCommand.java

private void logResponse(InputStream responseStream) throws IOException {
    byte[] responseBuffer = new byte[responseStream.available()];
    responseStream.read(responseBuffer);
    logger.debug(new String(responseBuffer));
    responseStream.reset();
}

From source file:org.deegree.portal.owswatch.validator.AbstractValidator.java

/**
 * This method is called To read the ServiceExceptionReport from the xml file
 *
 * @param method// w w w.  j ava2 s. c  o  m
 *            the httpmethod after executing it
 * @return an instance of ValidatorResponse with the necessary information after validation
 */
protected ValidatorResponse validateXmlServiceException(HttpMethodBase method) {

    Document doc = null;
    String lastMessage = null;
    Status status = null;
    try {
        InputStream stream = method.getResponseBodyAsStream();
        stream.reset();
        doc = instantiateParser().parse(stream);
    } catch (Exception e) {
        status = Status.RESULT_STATE_INVALID_XML;
        lastMessage = "Error: MalFormed XML Response";
        return new ValidatorResponse(lastMessage, status);
    }
    try {
        status = Status.RESULT_STATE_SERVICE_UNAVAILABLE;
        lastMessage = XMLTools.getNodeAsString(doc.getDocumentElement(), "./ServiceException", null,
                "Service Unavailable. Unknown error");
        return new ValidatorResponse(lastMessage, status);
    } catch (XMLParsingException e) {
        status = Status.RESULT_STATE_SERVICE_UNAVAILABLE;
        lastMessage = status.getStatusMessage();
        return new ValidatorResponse(lastMessage, status);
    }
}

From source file:com.jaromin.alfresco.extractor.AbstractBitmapExtractor.java

/**
 * Check if the next series of bytes from the stream matches the sequence.
 * The int 'b' is the most recently-read byte and the input stream is positioned
 * to read the byte immediately following 'b'.
 * If the match fails, the stream is reset. If it matches, the stream is NOT reset.
 * @param seq/*from   w w w  .  ja  v  a  2s  .  c om*/
 * @param in
 * @param b
 * @return
 * @throws IOException
 */
protected boolean matches(byte[] seq, InputStream in, int b) throws IOException {
    if (((byte) b) == seq[0]) {
        // potential match, read next 7 bytes
        byte[] buf = new byte[seq.length - 1];
        in.mark(buf.length);
        if (in.read(buf) == -1) {
            in.reset();
            return false;
        } else {
            if (ArrayUtils.isEquals(buf, ArrayUtils.subarray(seq, 1, seq.length))) { // last 7 bytes
                return true;
            }
            in.reset(); // rewind
        }
    }
    return false;
}

From source file:de.fanero.uncompress.stream.StreamDetectorImpl.java

private byte[] readHeader(InputStream in, int headerSize) throws IOException {

    in.mark(headerSize);/* ww w  .  j  av  a 2 s  .c o m*/
    byte[] header = new byte[headerSize];
    try {
        ByteStreams.readFully(in, header);
    } catch (IOException e) {
        // its ok
    }

    in.reset();
    return header;
}

From source file:org.modeshape.jcr.value.binary.CassandraBinaryStore.java

/**
 * Converts input stream into ByteBuffer.
 * //from w  ww . j  a  v  a 2  s. c o m
 * @param stream
 * @return the byte buffer
 * @throws IOException
 */
private ByteBuffer buffer(InputStream stream) throws IOException {
    stream.reset();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(stream, bout);
    return ByteBuffer.wrap(bout.toByteArray());
}

From source file:de.betterform.agent.web.filter.BufferedHttpServletResponseWrapper.java

public void debugPrint() {
    try {//from w w w  .  ja  v a 2  s .c o  m
        int x;
        InputStream inputStream = this.getInputStream();
        while ((x = inputStream.read()) != -1) {
            System.out.print((char) x);
        }
        inputStream.reset();
    } catch (IOException ex) {
        Logger.getLogger(BufferedHttpServletResponseWrapper.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.deegree.portal.owswatch.validator.AbstractValidator.java

/**
 * Validates the HttpMethodBase and checks if the execution was successful or not
 *
 * @param method/*from  ww w  .  j ava2s .c om*/
 *            the httpmethod after executing it
 * @return an instance of ValidatorResponse with the necessary information after validation
 */
protected ValidatorResponse validateXml(HttpMethodBase method) {

    String lastMessage = null;
    Status status = null;

    String contentType = method.getResponseHeader("Content-Type").getValue();
    if (!contentType.contains("xml")) {
        status = Status.RESULT_STATE_UNEXPECTED_CONTENT;
        lastMessage = StringTools.concat(100, "Error: Response Content is ", contentType, " not xml");
        return new ValidatorResponse(lastMessage, status);
    }

    String xml = null;
    try {
        InputStream stream = copyStream(method.getResponseBodyAsStream());
        stream.reset();
        xml = parseStream(stream);
    } catch (IOException e) {
        status = Status.RESULT_STATE_BAD_RESPONSE;
        lastMessage = status.getStatusMessage();
        return new ValidatorResponse(lastMessage, status);
    }

    if (xml.length() == 0) {
        status = Status.RESULT_STATE_BAD_RESPONSE;
        lastMessage = "Error: XML Response is empty";
        return new ValidatorResponse(lastMessage, status);
    }
    if (xml.contains("ServiceException")) {
        return validateXmlServiceException(method);
    }
    // If its an xml, and there's no service exception, then don't really parse the xml,
    // we assume that its well formed, since there might be huge xmls, which would take time to be parsed
    status = Status.RESULT_STATE_AVAILABLE;
    lastMessage = status.getStatusMessage();
    return new ValidatorResponse(lastMessage, status);
}

From source file:VASSAL.tools.io.RereadableInputStreamTest.java

@Test
public void testMarkAndReset() throws IOException {
    final byte[] expected = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    final InputStream in = new RereadableInputStream(new ByteArrayInputStream(expected));

    in.mark(4);/*from   w  ww.  j  a  v  a 2  s  .  c  om*/

    int count;

    final byte[] buf = new byte[4];
    count = in.read(buf, 0, 4);

    assertEquals(4, count);

    in.reset();

    final byte[] actual = new byte[expected.length];
    count = IOUtils.read(in, actual);

    assertEquals(expected.length, count);
    assertEquals(-1, in.read());
    assertArrayEquals(expected, actual);
}

From source file:com.norconex.importer.parser.impl.quattro.QPWTextExtractor.java

private boolean hasNext(InputStream in) throws IOException {
    try {/* w w  w.  j  av  a  2 s .  co  m*/
        in.mark(1);
        return in.read() != -1;
    } finally {
        in.reset();
    }
}

From source file:ir.rasen.charsoo.controller.image_loader.core.decode.BaseImageDecoder.java

protected InputStream resetStream(InputStream imageStream, ImageDecodingInfo decodingInfo)
        throws IOException, JSONException {
    try {/*from  w  w w .j a  va  2  s.  c o  m*/
        imageStream.reset();
    } catch (IOException e) {
        IoUtils.closeSilently(imageStream);
        imageStream = getImageStream(decodingInfo);
    }
    return imageStream;
}