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.dishevelled.compress.Compress.java

/**
 * Return true if the specified file is a gzip input stream.
 *
 * @since 1.3//w w w.j av  a 2 s.  co  m
 * @param inputStream input stream, must not be null
 * @return true if the specified file is a gzip input stream
 */
public static boolean isGzipInputStream(final InputStream inputStream) {
    checkNotNull(inputStream);
    InputStream in = inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream);
    try {
        in.mark(2);
        return in.read() == 31 && in.read() == 139;
    } catch (IOException e) {
        return false;
    } finally {
        try {
            in.reset();
        } catch (IOException e) {
            // ignore
        }
    }
}

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

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

From source file:org.xlcloud.commons.compress.CompressUtils.java

/**
 * Checks if an input stream is gzipped.
 * //from  www  .  j  ava 2 s .c  om
 * @param in
 * @return
 */
public static boolean isGZipped(InputStream in) {
    if (!in.markSupported()) {
        in = new BufferedInputStream(in);
    }
    in.mark(0);
    int magic = 0;
    try {
        magic = in.read() & 0xff | ((in.read() << 8) & 0xff00);
        in.reset();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        return false;
    }
    return magic == GZIPInputStream.GZIP_MAGIC;
}

From source file:com.alibaba.simpleimage.util.ImageUtils.java

public static boolean isTIFF(InputStream in) throws IOException {
    if (!in.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }/*from w ww . j av  a 2s  .c om*/
    byte[] b = new byte[4];
    try {
        in.mark(30);
        in.read(b);
    } finally {
        in.reset();
    }

    return ((b[0] == (byte) 0x49 && b[1] == (byte) 0x49 && b[2] == (byte) 0x2a && b[3] == (byte) 0x00)
            || (b[0] == (byte) 0x4d && b[1] == (byte) 0x4d && b[2] == (byte) 0x00 && b[3] == (byte) 0x2a));
}

From source file:com.alibaba.simpleimage.util.ImageUtils.java

public static boolean isBMP(InputStream in) throws IOException {
    if (!in.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }/*  w w w  .  j a  va 2 s.  co  m*/

    byte[] b = new byte[2];
    try {
        in.mark(30);
        in.read(b);
    } finally {
        in.reset();
    }

    return (b[0] == 0x42) && (b[1] == 0x4d);
}

From source file:com.alibaba.simpleimage.util.ImageUtils.java

public static boolean isPNG(InputStream in) throws IOException {
    if (!in.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }/*from  w w  w.ja  v a  2  s  .  c  o m*/

    byte[] b = new byte[8];
    try {
        in.mark(30);
        in.read(b);
    } finally {
        in.reset();
    }

    return (b[0] == (byte) 137 && b[1] == (byte) 80 && b[2] == (byte) 78 && b[3] == (byte) 71
            && b[4] == (byte) 13 && b[5] == (byte) 10 && b[6] == (byte) 26 && b[7] == (byte) 10);
}

From source file:com.alibaba.simpleimage.util.ImageUtils.java

public static boolean isGIF(InputStream in) throws IOException {
    if (!in.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }// www. j a va  2  s. c o  m

    byte[] b = new byte[6];

    try {
        in.mark(30);
        in.read(b);
    } finally {
        in.reset();
    }

    return b[0] == 'G' && b[1] == 'I' && b[2] == 'F' && b[3] == '8' && (b[4] == '7' || b[4] == '9')
            && b[5] == 'a';
}

From source file:org.dishevelled.compress.Compress.java

/**
 * Return true if the specified file is a bzip2 input stream.
 *
 * @since 1.3//from ww  w.  ja  v  a2  s. c  o m
 * @param inputStream input stream, must not be null
 * @return true if the specified file is a bzip2 input stream
 */
public static boolean isBzip2InputStream(final InputStream inputStream) {
    checkNotNull(inputStream);
    InputStream in = inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream);
    try {
        in.mark(3);
        return in.read() == 'B' && in.read() == 'Z' && in.read() == 'h';
    } catch (IOException e) {
        return false;
    } finally {
        try {
            in.reset();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:Main.java

/** Gets Bitmap from inputStream downsampled.
 *
 * @param bis//from   www  .java2 s.  co  m
 * @param reqWidth
 * @param reqHeight
 * @return
 */
public static Bitmap decodeSampledBitmapFromStream(InputStream bis, int reqWidth, int reqHeight) {

    InputStream is = new BufferedInputStream(bis);

    try {
        is.mark(is.available());
    } catch (IOException e) {
        e.printStackTrace();
    }

    // First decode with inJustDecodeBounds=true to check dimensions
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bmp0 = BitmapFactory.decodeStream(is, null, options);

    try {
        is.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Calculate inSampleSize
    //options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
    return bmp;
}

From source file:org.craftercms.cstudio.share.forms.impl.FormUtils.java

public static String getMd5ForFile(InputStream input) {
    String result = null;/*w  w w .j  av  a 2  s. co m*/
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");

        md.reset();
        byte[] bytes = new byte[1024];
        int numBytes;
        while ((numBytes = input.read(bytes)) != -1) {
            md.update(bytes, 0, numBytes);
        }
        byte[] digest = md.digest();
        result = new String(Hex.encodeHex(digest));
        input.reset();
    } catch (NoSuchAlgorithmException e) {
        logger.error("Error while creating MD5 digest", e);
    } catch (IOException e) {
        logger.error("Error while reading input stream", e);
    }
    return result;
}