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.infoscoop.request.filter.RssFilter.java

public static boolean isAtom(InputStream is) throws IOException {
    is.mark(1);//from   w ww  .j av a 2 s  .  c  o  m
    byte[] xmldec = new byte[500];
    is.read(xmldec);

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

    return isAtom;
}

From source file:org.jahia.utils.zip.ZipEntryCharsetDetector.java

/**
 * Returns the detected character set, which is suitable to read the entries of the provided ZIP file properly. The set of charsets to
 * try can be specified by the system property {@link #ZIP_ENTRY_ALTERNATIVE_ENCODING} and also by the corresponding jahia.properties
 * entry. If the method fails to detect the charset it returns <code>null</code>.
 *
 * @param zipFileResettableInputStream/*w  ww . j av a  2 s .  com*/
 *            a ZIP file stream that has proper support for {@link InputStream#reset()} method, e.g. is a {@link ByteArrayInputStream}
 * @return the detected character set, which is suitable to read the entries of the provided ZIP file properly or <code>null</code> if
 *         we fail to detect the charset
 */
public static Charset detect(InputStream resettableInputStream) {
    try {
        for (Charset c : getCharsetTryChain()) {
            try {
                if (canRead(resettableInputStream, c)) {
                    return c;
                }
            } finally {
                resettableInputStream.reset();
            }
        }
    } catch (IOException e) {
        logger.warn("Error checking charset for the input stream", e);
    }

    logger.warn("Unable to find a charset to read the entries of a provided ZIP file stream");

    return null;
}

From source file:com.adguard.filter.proxy.ProtocolDetector.java

/**
 * Detects protocol of the input stream//from w ww .  j  a  va2  s.c  o  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;
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromSteam(InputStream stream, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from w  w  w.j av a  2  s .co  m*/
    BitmapFactory.decodeStream(stream, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    try {
        stream.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return BitmapFactory.decodeStream(stream, null, options);
}

From source file:com.norconex.commons.lang.io.IOUtil.java

/**
 * Gets and resets the specified number of bytes from the input stream.
 * @param is input stream//from   www . j av  a2  s.c o  m
 * @param qty number of bytes to read
 * @return byte array of length matching the quantity requested
 * @throws IOException if {@link InputStream#markSupported()} returns false
 */
public static byte[] borrowBytes(InputStream is, int qty) throws IOException {
    if (is == null) {
        throw new IllegalArgumentException("Input stream cannot be null.");
    } else if (!is.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark.");
    }
    is.mark(qty);
    byte[] bytes = new byte[qty];
    is.read(bytes);
    is.reset();
    return bytes;
}

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 {/*  ww w.  j  a  v a 2 s.  c om*/
            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:Main.java

public static Bitmap decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
        throws IOException {
    is.mark(is.available());//from   w ww  .  j a v  a2s. co m

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

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    is.reset();

    return BitmapFactory.decodeStream(is, null, options);
}

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);/* w  w  w .ja va2 s . c o  m*/
    try {
        @SuppressWarnings("resource")
        JarInputStream jarStream = new JarInputStream(stream);
        return jarStream.getManifest();
    } finally {
        stream.reset();
    }
}

From source file:org.apache.xmlgraphics.image.loader.util.ImageUtil.java

/**
 * Indicates whether an InputStream is GZIP compressed. The InputStream must support
 * mark()/reset()./*from   w  w w . j a v a  2  s .com*/
 * @param in the InputStream (must return true on markSupported())
 * @return true if the InputStream is GZIP compressed
 * @throws IOException in case of an I/O error
 */
public static boolean isGZIPCompressed(InputStream in) throws IOException {
    if (!in.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark()!");
    }
    byte[] data = new byte[2];
    in.mark(2);
    in.read(data);
    in.reset();
    return ((data[0] == GZIP_MAGIC[0]) && (data[1] == GZIP_MAGIC[1]));
}

From source file:com.predic8.membrane.core.util.HttpUtil.java

public static String readLine(InputStream in) throws IOException, EndOfStreamException {

    StringBuilder line = new StringBuilder(128);

    int b;//from   w  w w .  j  a v a2s  .  co m
    while ((b = in.read()) != -1) {
        if (b == 13) {
            in.read();
            return line.toString();
        }
        if (b == 10) {
            in.mark(2);
            if (in.read() != 13)
                in.reset();
            return line.toString();
        }

        line.append((char) b);
    }

    throw new EOFWhileReadingLineException(line.toString());
}