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:org.xlcloud.commons.compress.CompressUtils.java

/**
 * Checks if an input stream is gzipped.
 * //  w  ww  . j a  v a  2s .  c o m
 * @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: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);/*from   www  . j ava 2  s .  co m*/
    try {
        @SuppressWarnings("resource")
        JarInputStream jarStream = new JarInputStream(stream);
        return jarStream.getManifest();
    } finally {
        stream.reset();
    }
}

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  w  ww.  j a va  2  s .co m*/
    inputStream.mark(CodedIO.MAX_MARK_SIZE);
    return inputStream;
}

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 ww  w  .jav  a2  s  .c o m
    int magic = readUShort(bufferedStream);
    bufferedStream.reset();

    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;/*from w  w  w .  j  a va  2  s  .  c  o  m*/

    if (!source.markSupported()) {
        throw new IllegalArgumentException("Input stream must support mark");
    }

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

/**
 * Returns a sha-1 signature for that content.
 * /*w w w  .  j  av  a2 s . c o m*/
 * @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:com.jkoolcloud.tnt4j.streams.configure.sax.StreamsConfigSAXParser.java

/**
 * Reads the configuration and invokes the (SAX-based) parser to parse the configuration file contents.
 *
 * @param config//from  w w w .j av a 2s  .  co  m
 *            input stream to get configuration data from
 * @param validate
 *            flag indicating whether to validate configuration XML against XSD schema
 * @return streams configuration data
 * @throws ParserConfigurationException
 *             if there is an inconsistency in the configuration
 * @throws SAXException
 *             if there was an error parsing the configuration
 * @throws IOException
 *             if there is an error reading the configuration data
 */
public static StreamsConfigData parse(InputStream config, boolean validate)
        throws ParserConfigurationException, SAXException, IOException {
    if (validate) {
        config = config.markSupported() ? config : new ByteArrayInputStream(IOUtils.toByteArray(config));

        Map<OpLevel, List<SAXParseException>> validationErrors = validate(config);

        if (MapUtils.isNotEmpty(validationErrors)) {
            for (Map.Entry<OpLevel, List<SAXParseException>> vee : validationErrors.entrySet()) {
                for (SAXParseException ve : vee.getValue()) {
                    LOGGER.log(OpLevel.WARNING,
                            StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                                    "StreamsConfigSAXParser.xml.validation.error"),
                            ve.getLineNumber(), ve.getColumnNumber(), vee.getKey(), ve.getLocalizedMessage());
                }
            }
        }
    }

    Properties p = Utils.loadPropertiesResource("sax.properties"); // NON-NLS

    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    SAXParser parser = parserFactory.newSAXParser();
    ConfigParserHandler hndlr = null;
    try {
        String handlerClassName = p.getProperty(HANDLER_PROP_KEY, ConfigParserHandler.class.getName());
        if (StringUtils.isNotEmpty(handlerClassName)) {
            hndlr = (ConfigParserHandler) Utils.createInstance(handlerClassName);
        }
    } catch (Exception exc) {
    }

    if (hndlr == null) {
        hndlr = new ConfigParserHandler();
    }

    parser.parse(config, hndlr);

    return hndlr.getStreamsConfigData();
}

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

/**
 * @param stream aa// w w w.  jav  a  2s.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:com.norconex.commons.lang.io.IOUtil.java

/**
 * Gets and resets the specified number of bytes from the input stream.
 * @param is input stream//from   w  w  w .  jav  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:org.apache.xmlgraphics.image.loader.util.ImageUtil.java

/**
 * Decorates an InputStream with a BufferedInputStream if it doesn't support mark()/reset().
 * @param in the InputStream//from  w ww  .  ja va  2 s  . c  o m
 * @return the decorated InputStream
 */
public static InputStream decorateMarkSupported(InputStream in) {
    if (in.markSupported()) {
        return in;
    } else {
        return new java.io.BufferedInputStream(in);
    }
}