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:Main.java

/**
 * Decodes from an input stream that optimally supports mark and reset
 * operations. If a maximum width and/or height are specified then the
 * passed stream must support mark and reset so that the bitmap can be down
 * sampled properly. If the width and/or height are specified and the input
 * stream does not support mark and reset, then an IllegalArgumentException
 * will be throw./*from ww  w.  j  av a2  s  . c  om*/
 */
public static Bitmap decodeSampledBitmapFromStream(InputStream inputStream, int width, int height) {
    if ((width != 0 || height != 0) && !inputStream.markSupported()) {
        throw new IllegalArgumentException(
                "Bitmap decoding requires an input stream that supports " + "mark and reset");
    }

    // Set a mark for reset. Since we have no idea of the size of this
    // image, just set the maximum value possible.
    inputStream.mark(Integer.MAX_VALUE);

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

    // Reset the stream for the actual decoding phase.
    try {
        inputStream.reset();
    } catch (IOException e) {
        Log.e(TAG, "Failed to reset input stream during bitmap decoding");
        return null;
    }

    // If either width or height is passed in as 0, then use the actual
    // stored image dimension.
    if (width == 0) {
        width = options.outWidth;
    }
    if (height == 0) {
        height = options.outHeight;
    }

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(inputStream, null, options);
}

From source file:org.apache.hadoop.fs.tar.TarFSUtils.java

private static byte[] getFirstFewBytes(InputStream in, int n) throws IOException {
    byte[] b = new byte[n];

    if (in.markSupported()) {
        in.mark(n);// w  ww  .j  a  v  a  2 s.  c  o m
        int bytesRead = in.read(b);
        in.reset();

        if (bytesRead < n)
            return null;
    } else
        throw new IOException("Mark must be supported");

    return b;
}

From source file:com.amazonaws.services.s3.internal.AWSS3V4Signer.java

/**
 * Read the content of the request to get the length of the stream.
 * This method will wrap the stream by RepeatableInputStream if it is 
 * not mark-supported./*w ww  .j  a va  2  s. c  o  m*/
 */
private static long getContentLength(Request<?> request) throws IOException {
    InputStream content = request.getContent();
    if (!content.markSupported()) {
        int streamBufferSize = Constants.getStreamBufferSize();
        content = new RepeatableInputStream(content, streamBufferSize);
        request.setContent(content);
    }

    long contentLength = 0;
    byte[] tmp = new byte[4096];
    int read;
    try {
        content.mark(-1);
        while ((read = content.read(tmp)) != -1) {
            contentLength += read;
        }
        content.reset();
    } finally {
        content.close();
    }

    return contentLength;
}

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");
    }//  ww w.j  a va  2 s  .  c  o  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 isTIFF(InputStream in) throws IOException {
    if (!in.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }/* w  ww  .  ja  v a2  s. c o  m*/
    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 isPNG(InputStream in) throws IOException {
    if (!in.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }//from   ww  w .  java2s. 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");
    }//from   w w  w  .j a va2 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 gzip input stream.
 *
 * @since 1.3/* w w  w . java2  s . c o  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:org.dishevelled.compress.Compress.java

/**
 * Return true if the specified file is a bzip2 input stream.
 *
 * @since 1.3//from   w ww. ja  va2s .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:com.adguard.filter.proxy.ProtocolDetector.java

/**
 * Detects protocol of the input stream/* w  w w  . j  a  v  a  2 s  . co  m*/
 *
 * @param inputStream Stream to check
 * @return Protocol detected
 * @throws IOException                If something is wrong with the input stream
 * @throws IllegalArgumentException If stream does not support "mark"
 */
public static Protocol detect(InputStream inputStream) throws IOException {
    if (!inputStream.markSupported()) {
        throw new IllegalArgumentException("Stream must support mark for protocol detection!");
    }

    byte[] packet = new byte[32];
    inputStream.mark(packet.length);
    int read = inputStream.read(packet);
    inputStream.reset();

    if (read < packet.length) {
        return Protocol.TOO_SMALL;
    } else if (ProtocolDetector.isHttpProtocol(packet)) {
        return Protocol.HTTP;
    } else if (ProtocolDetector.isTlsProtocol(packet)) {
        return Protocol.TLS;
    } else {
        return Protocol.OTHER;
    }
}