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:org.openspotlight.common.util.Sha1.java

/**
 * Returns a sha-1 signature for that content.
 * //from   ww  w.j a va2 s .  c om
 * @param content
 * @return a byte array representing the signature
 */
public static byte[] getSha1Signature(final InputStream content) {
    checkNotNull("content", content);//$NON-NLS-1$
    try {
        if (content.markSupported()) {
            content.reset();
        }
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtils.copy(content, output);
        if (content.markSupported()) {
            content.reset();
        }
        return DIGESTER.digest(output.toByteArray());
    } catch (final Exception e) {
        throw logAndReturnNew(e, SLRuntimeException.class);
    }
}

From source file:org.infoscoop.request.filter.XMLFilter.java

/**
 * @param is//from w  w w.  j ava 2s  .  com
 * @return skippedCount
 * @throws IOException
 */
public static int skipEmptyLine(InputStream is) throws IOException {
    is.mark(1);
    int count = 0;
    for (int temp = 0; (temp = is.read()) != -1; count++) {
        char tempCh = (char) temp;
        if (tempCh == '<') {
            break;
        } else {
            is.mark(1);
        }
    }
    is.reset();
    return count;
}

From source file:se.tillvaxtverket.ttsigvalws.ttwssigvalidation.document.DocTypeIdentifier.java

/**
 * Guess the document format and return an appropriate document type string
 *
 * @param is An InputStream holding the document
 * @return "xml" if the document is an XML document or "pdf" if the document
 * is a PDF document, or else an error message.
 *//*  w  w  w  .j av  a 2 s.  c  o m*/
public static DocType getDocType(InputStream is) {
    InputStream input = null;

    try {
        input = new BufferedInputStream(is);
        input.mark(5);
        byte[] preamble = new byte[5];
        int read = 0;
        try {
            read = input.read(preamble);
            input.reset();
        } catch (IOException ex) {
            return DocType.UNKNOWN;
        }
        if (read < 5) {
            return DocType.UNKNOWN;
        }
        String preambleString = new String(preamble);
        byte[] xmlPreable = new byte[] { '<', '?', 'x', 'm', 'l' };
        byte[] xmlUtf8 = new byte[] { -17, -69, -65, '<', '?' };
        if (Arrays.equals(preamble, xmlPreable) || Arrays.equals(preamble, xmlUtf8)) {
            return DocType.XML;
        } else if (preambleString.equals("%PDF-")) {
            return DocType.PDF;
        } else if (preamble[0] == 'P' && preamble[1] == 'K') {
            ZipInputStream asics = new ZipInputStream(new BufferedInputStream(is));
            ByteArrayOutputStream datafile = null;
            ByteArrayOutputStream signatures = null;
            ZipEntry entry;
            try {
                while ((entry = asics.getNextEntry()) != null) {
                    if (entry.getName().equals("META-INF/signatures.p7s")) {
                        signatures = new ByteArrayOutputStream();
                        IOUtils.copy(asics, signatures);
                        signatures.close();
                    } else if (entry.getName().equalsIgnoreCase("META-INF/signatures.p7s")) {
                        /* Wrong case */
                        return DocType.ASICS_NON_ETSI;
                    } else if (entry.getName().indexOf("/") == -1) {
                        if (datafile == null) {
                            datafile = new ByteArrayOutputStream();
                            IOUtils.copy(asics, datafile);
                            datafile.close();
                        } else {
                            return DocType.ASICS_S;
                        }
                    }
                }
            } catch (Exception ex) {
                return DocType.UNKNOWN;
            }
            if (datafile == null || signatures == null) {
                return DocType.UNKNOWN;
            }
            return DocType.ASICS_CADES;

        } else if (preambleString.getBytes()[0] == 0x30) {
            return DocType.CADES;
        } else {
            return DocType.UNKNOWN;
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.bndtools.rt.repository.server.RepositoryResourceComponent.java

private static boolean isGZip(InputStream bufferedStream) throws IOException {
    assert bufferedStream.markSupported();

    bufferedStream.mark(2);/*from  w w w  . j av a  2 s  .  c  o  m*/
    int magic = readUShort(bufferedStream);
    bufferedStream.reset();

    return magic == GZIPInputStream.GZIP_MAGIC;
}

From source file:Main.java

public static String readSingleLineFromFile(InputStream in, int lineNumber) {
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line = "";

    try {//from ww w  .  java 2s  .  c o  m
        for (int i = 0; i < lineNumber - 1; ++i) {
            br.readLine();
        }

        line = br.readLine();
    } catch (IOException e) {
        Log.e("LineParseError", "Could not read line");
    }

    try {
        in.reset();
    } catch (IOException e) {
        Log.e("LineParser", "Could not reset InputStream");
    }
    return line;
}

From source file:org.apache.sling.distribution.packaging.impl.SimpleDistributionPackage.java

public static SimpleDistributionPackage fromStream(InputStream stream, String type) {

    try {/*from  w  w  w  .  j  a  v a2s.co m*/
        int size = SimpleDistributionPackage.PACKAGE_START.getBytes("UTF-8").length;
        stream.mark(size);
        byte[] buffer = new byte[size];
        int bytesRead = stream.read(buffer, 0, size);
        stream.reset();
        String s = new String(buffer, "UTF-8");

        if (bytesRead > 0 && buffer[0] > 0 && s.startsWith(SimpleDistributionPackage.PACKAGE_START)) {
            String streamString = IOUtils.toString(stream, "UTF-8");

            return fromIdString(streamString, type);
        }
    } catch (IOException e) {
        log.error("cannot read stream", e);
    }

    return null;
}

From source file:com.aaasec.sigserv.cscommon.DocTypeIdentifier.java

/**
 * Guess the document format and return an appropriate document type string
 *
 * @param is An InputStream holding the document
 * @return "xml" if the document is an XML document or "pdf" if the document
 * is a PDF document, or else an error message.
 *///w w  w.  ja  v  a 2s  . c  om
public static SigDocumentType getDocType(InputStream is) {
    InputStream input = null;

    try {
        input = new BufferedInputStream(is);
        input.mark(5);
        byte[] preamble = new byte[5];
        int read = 0;
        try {
            read = input.read(preamble);
            input.reset();
        } catch (IOException ex) {
            return SigDocumentType.Unknown;
        }
        if (read < 5) {
            return SigDocumentType.Unknown;
        }
        String preambleString = new String(preamble);
        byte[] xmlPreable = new byte[] { '<', '?', 'x', 'm', 'l' };
        byte[] xmlUtf8 = new byte[] { -17, -69, -65, '<', '?' };
        if (Arrays.equals(preamble, xmlPreable) || Arrays.equals(preamble, xmlUtf8)) {
            return SigDocumentType.XML;
        } else if (preambleString.equals("%PDF-")) {
            return SigDocumentType.PDF;
        } else if (preamble[0] == 'P' && preamble[1] == 'K') {
            ZipInputStream asics = new ZipInputStream(new BufferedInputStream(is));
            ByteArrayOutputStream datafile = null;
            ByteArrayOutputStream signatures = null;
            ZipEntry entry;
            try {
                while ((entry = asics.getNextEntry()) != null) {
                    if (entry.getName().equals("META-INF/signatures.p7s")) {
                        signatures = new ByteArrayOutputStream();
                        IOUtils.copy(asics, signatures);
                        signatures.close();
                    } else if (entry.getName().equalsIgnoreCase("META-INF/signatures.p7s")) {
                        /* Wrong case */
                        // asics;Non ETSI compliant
                        return SigDocumentType.Unknown;
                    } else if (entry.getName().indexOf("/") == -1) {
                        if (datafile == null) {
                            datafile = new ByteArrayOutputStream();
                            IOUtils.copy(asics, datafile);
                            datafile.close();
                        } else {
                            //                              // asics;ASiC-S profile support only one data file
                            return SigDocumentType.Unknown;
                        }
                    }
                }
            } catch (Exception ex) {
                // null;Invalid ASiC-S
                return SigDocumentType.Unknown;
            }
            if (datafile == null || signatures == null) {
                // asics;ASiC-S profile support only one data file with CAdES signature
                return SigDocumentType.Unknown;
            }
            // asics/cades
            return SigDocumentType.Unknown;

        } else if (preambleString.getBytes()[0] == 0x30) {
            // cades;
            return SigDocumentType.Unknown;
        } else {
            // null;Document format not recognized/handled
            return SigDocumentType.Unknown;
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.digidoc4j.utils.Helper.java

/**
 * @param stream aa/*from ww  w  .  ja va 2 s.co  m*/
 * @return aa
 * @throws IOException aa
 */
public static boolean isZipFile(InputStream stream) throws IOException {
    DataInputStream in = new DataInputStream(stream);

    if (stream.markSupported())
        stream.mark(INT_LENGTH);

    int test = in.readInt();

    if (stream.markSupported())
        stream.reset();

    final int zipVerificationCode = ZIP_VERIFICATION_CODE;
    return test == zipVerificationCode;
}

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.ZipUtils.java

/**
 * check if the {@link InputStream} provided is a zip file
 * /* w  ww .ja  va 2 s  .  co m*/
 * @param in the stream.
 * @return if it is a ZIP file.
 */
public static boolean isZipStream(InputStream in) {
    if (!in.markSupported()) {
        in = new BufferedInputStream(in);
    }
    boolean isZip = true;
    try {
        in.mark(MAGIC.length);
        for (byte element : MAGIC) {
            if (element != (byte) in.read()) {
                isZip = false;
                break;
            }
        }
        in.reset();
    } catch (IOException e) {
        isZip = false;
    }
    return isZip;
}

From source file:com.meterware.httpunit.HttpUnitUtils.java

/**
 * parse the given inputStream with a new Parser
 * @param inputStream/*  w ww  . j  a v  a2s .  co m*/
 * @return the document parsed from the input Stream
 */
public static Document parse(InputStream inputStream) throws SAXException, IOException {
    DocumentBuilder db = newParser();
    try {
        Document doc = db.parse(inputStream);
        return doc;
    } catch (java.net.MalformedURLException mue) {
        if (EXCEPTION_DEBUG) {
            String msg = mue.getMessage();
            if (msg != null) {
                System.err.println(msg);
            }
            InputStream is = inputStream;
            is.reset();
            String content = parseISToString(is);
            System.err.println(content);
        }
        throw mue;
    }
}