Example usage for java.io InputStream mark

List of usage examples for java.io InputStream mark

Introduction

In this page you can find the example usage for java.io InputStream mark.

Prototype

public synchronized void mark(int readlimit) 

Source Link

Document

Marks the current position in this input stream.

Usage

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

/**
 * Return true if the specified file is a bzip2 input stream.
 *
 * @since 1.3//from www.  j  a v a2s.co  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:org.bndtools.rt.repository.server.RepositoryResourceComponent.java

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

    bufferedStream.mark(2);
    int magic = readUShort(bufferedStream);
    bufferedStream.reset();//from w  w  w  . ja  va  2  s.  c  o m

    return magic == GZIPInputStream.GZIP_MAGIC;
}

From source file:com.exzogeni.dk.graphics.Bitmaps.java

@Nullable
public static Bitmap decodeStream(@NonNull InputStream stream, Rect outPadding, int hwSize) {
    if (hwSize > 0) {
        final InputStream localIn = new BufferPoolInputStream(stream);
        try {/*from   ww w .  ja v  a 2s.co  m*/
            final BitmapFactory.Options ops = new BitmapFactory.Options();
            ops.inJustDecodeBounds = true;
            localIn.mark(BITMAP_HEAD);
            BitmapFactory.decodeStream(localIn, outPadding, ops);
            ops.inSampleSize = calculateInSampleSize(ops, hwSize);
            ops.inJustDecodeBounds = false;
            localIn.reset();
            return BitmapFactory.decodeStream(localIn, outPadding, ops);
        } catch (IOException e) {
            Logger.error(e);
        } finally {
            IOUtils.closeQuietly(localIn);
        }
        return null;
    }
    return BitmapFactory.decodeStream(stream);
}

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

/**
 * @param stream aa/*from w w  w .java  2s .c  o 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:org.xlcloud.commons.compress.CompressUtils.java

/**
 * Checks if an input stream is gzipped.
 * //  w w w. j a va2  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 isJPEG(InputStream source) throws IOException {
    InputStream iis = source;

    if (!source.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }/*from   w w  w.j  av  a2  s .  c o m*/

    iis.mark(30);
    // If the first two bytes are a JPEG SOI marker, it's probably
    // a JPEG file. If they aren't, it definitely isn't a JPEG file.
    try {
        int byte1 = iis.read();
        int byte2 = iis.read();
        if ((byte1 == 0xFF) && (byte2 == 0xD8)) {

            return true;
        }
    } finally {
        iis.reset();
    }

    return false;
}

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

private static Manifest readManifest(InputStream stream) throws IOException {
    if (!stream.markSupported())
        throw new IOException("Stream must support mark/reset");

    stream.mark(100000);
    try {//ww  w. j  a va  2 s.  c om
        @SuppressWarnings("resource")
        JarInputStream jarStream = new JarInputStream(stream);
        return jarStream.getManifest();
    } finally {
        stream.reset();
    }
}

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

private static boolean isCalDAV(InputStream is) throws IOException {
    is.mark(1);
    byte[] xmldec = new byte[500];
    is.read(xmldec);/*from   www. j  a v  a2  s  .  c  o m*/

    String xmlDecStr = new String(xmldec);
    boolean isCalDAV = false;
    if (xmlDecStr.indexOf("multistatus") > 0) {
        isCalDAV = true;
    }
    is.reset();
    if (log.isDebugEnabled()) {
        log.debug(isCalDAV ? "Process caldav." : "Process feed.");
    }

    return isCalDAV;
}

From source file:org.ebayopensource.turmeric.eclipse.typelibrary.ui.wst.WTPCopyUtil.java

private static InputStream getMarkSupportedStream(InputStream inputStream) {
    if (!inputStream.markSupported()) {
        inputStream = new BufferedInputStream(inputStream, CodedIO.MAX_BUF_SIZE);
    }/*from www  .  j  av  a2  s . c  o m*/
    inputStream.mark(CodedIO.MAX_MARK_SIZE);
    return inputStream;
}

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 w  w. j av  a2s .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));
}