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:com.effektif.adapter.helpers.RequestLogger.java

private InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset)
        throws IOException {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }/*from  w w  w  . j a  va2s. com*/
    stream.mark(maxEntitySize + 1);
    final byte[] entity = new byte[maxEntitySize + 1];
    final int entitySize = stream.read(entity);
    b.append(REQUEST_PREFIX);
    String entityString = new String(entity, 0, Math.min(entitySize, maxEntitySize), charset);
    if (logEntityJsonPretty && (entitySize <= maxEntitySize)) {
        entityString = getJsonPrettyString(entityString);
    }
    b.append(entityString);
    if (entitySize > maxEntitySize) {
        b.append("...more...");
    }
    stream.reset();
    return stream;
}

From source file:com.microsoft.windowsazure.services.core.RetryPolicyFilter.java

private InputStream getEntityStream(Request request) {
    if (request.getEntity() == null)
        return null;

    if (!(request.getEntity() instanceof InputStream))
        return null;

    InputStream entityStream = (InputStream) request.getEntity();

    // If the entity is an InputStream that doesn't support "mark/reset", we can't
    // implement a retry logic, so we simply throw.
    if (!entityStream.markSupported()) {
        throw new IllegalArgumentException("The input stream for the request entity must support 'mark' and "
                + "'reset' to be compatible with a retry policy filter.");
    }/*from   w ww  .  ja va 2 s  .c o  m*/

    return entityStream;
}

From source file:org.apache.jackrabbit.core.data.DBDataStoreTest.java

public void testDbInputStreamReset() throws Exception {
    DataRecord record = store.getRecord(identifier);
    InputStream in = record.getStream();
    try {/*from  w w  w. j a  va 2 s  .com*/
        // test whether mark and reset works
        assertTrue(in.markSupported());
        in.mark(data.length);
        while (-1 != in.read()) {
            // loop
        }
        assertTrue(in.markSupported());
        try {
            in.reset();
        } catch (Exception e) {
            fail("Unexpected exception while resetting input stream: " + e.getMessage());
        }

        // test integrity of replayed bytes
        byte[] replayedBytes = new byte[data.length];
        int length = in.read(replayedBytes);
        assertEquals(length, data.length);

        for (int i = 0; i < data.length; i++) {
            log.append(i + " data: " + data[i] + " replayed: " + replayedBytes[i] + "\n");
            assertEquals(data[i], replayedBytes[i]);
        }

        assertEquals(-1, in.read());

    } finally {
        in.close();
        log.flush();
    }
}

From source file:org.openmastery.logging.LoggingFilter.java

private InputStream logInboundEntity(StringBuilder b, InputStream stream, Charset charset) throws IOException {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }// w w w .j  a  v a2 s  .c o m
    stream.mark(maxEntitySize + 1);
    byte[] entity = new byte[maxEntitySize + 1];
    int entitySize = stream.read(entity);
    entitySize = entitySize < 0 ? 0 : entitySize;
    b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset));
    if (entitySize > maxEntitySize) {
        b.append("...more...");
    }
    b.append('\n');
    stream.reset();
    return stream;
}

From source file:org.cytoscape.io.internal.read.GenericReaderManager.java

/**
 * Gets the GraphReader that is capable of reading the specified file.
 * /*from w  ww .ja  va  2  s .  co  m*/
 * @param uri
 *            URI of file to be read.
 * @return GraphReader capable of reading the specified file. Null if file
 *         cannot be read.
 */
public R getReader(final URI uri, final String inputName) {

    // Data location is always required.
    if (uri == null) {
        throw new NullPointerException("Data source URI is null");
    }

    // This is the default reader, which accepts files with no extension.
    // Usually, this is ImportNetworkTableReaderFactory (Manual table
    // import)
    T defaultFactory = null;

    final List<T> factoryList = new ArrayList<T>();
    final Map<String, T> factoryTable = new HashMap<String, T>();

    // Pick compatible reader factories.
    for (final T factory : factories) {
        final CyFileFilter cff = factory.getFileFilter();
        // Got "Accepted" flag. Need to check it's default or not.
        if (cff.accepts(uri, category)) {
            logger.info("Filter returns Accepted.  Need to check priority: " + factory);
            if (factory.getClass().toString().contains(DEFAULT_READER_FACTORY_CLASS)) {
                defaultFactory = factory;
            } else {
                factoryList.add(factory);
                for (final String extension : cff.getExtensions()) {
                    factoryTable.put(extension, factory);
                }
            }
        }
    }

    T chosenFactory = null;

    // No compatible factory is available.
    if (factoryTable.isEmpty() && defaultFactory == null) {
        logger.warn("No reader found for uri: " + uri.toString());
        throw new NullPointerException("Could not find reader.");
    } else if (factoryList.size() == 1) {
        // There is only one compatible reader factory.  Use it:
        chosenFactory = factoryList.get(0);
    } else {
        if (factoryList.isEmpty() && defaultFactory != null) {
            // There is only one factory
            chosenFactory = defaultFactory;
        } else {
            // Well, we cannot decide which one is correct.  Try to use ext...
            String extension = FilenameUtils.getExtension(uri.toString());
            if (factoryTable.containsKey(extension))
                chosenFactory = factoryTable.get(extension);
            else {
                if (factoryTable.containsKey(""))
                    chosenFactory = factoryTable.get("");
                else
                    throw new NullPointerException("Could not find reader factory.");
            }
        }
    }

    try {
        logger.info("Successfully found compatible ReaderFactory " + chosenFactory);
        // This returns strean using proxy if it exists.
        InputStream stream = streamUtil.getInputStream(uri.toURL());
        if (!stream.markSupported()) {
            stream = new BufferedInputStream(stream);
        }
        return (R) chosenFactory.createTaskIterator(stream, inputName).next();

    } catch (IOException e) {
        logger.warn("Error opening stream to URI: " + uri.toString(), e);
        throw new IllegalStateException("Could not open stream for reader.", e);
    }
}

From source file:com.adaptris.core.lms.StreamWrapperCase.java

@Test
public void testInputStream_Mark() throws Exception {
    StreamWrapper wrapper = createWrapper(false);
    File file = writeFile(TempFileUtils.createTrackedFile(wrapper));
    InputStream in = wrapper.openInputStream(file, NO_OP);
    try (InputStream closeable = in) {
        tryQuietly(() -> {/*from  ww  w.j  a  v a  2  s. co  m*/
            in.markSupported();
        });
        tryQuietly(() -> {
            in.mark(10);
        });
        tryQuietly(() -> {
            in.reset();
        });
    }
}

From source file:org.apache.sling.distribution.packaging.impl.AbstractDistributionPackageBuilder.java

@Nonnull
@Override//  www .  j  a v  a 2s  . co  m
public DistributionPackageInfo installPackage(@Nonnull ResourceResolver resourceResolver,
        @Nonnull InputStream stream) throws DistributionException {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    DistributionPackageInfo packageInfo = new DistributionPackageInfo(type);

    DistributionPackageUtils.readInfo(stream, packageInfo);

    DistributionPackage distributionPackage = SimpleDistributionPackage.fromStream(stream, type);

    boolean installed;
    // not a simple package
    if (distributionPackage == null) {
        installed = installPackageInternal(resourceResolver, stream);
    } else {
        installed = installPackage(resourceResolver, distributionPackage);
        packageInfo.putAll(distributionPackage.getInfo());
    }

    if (installed) {
        return packageInfo;
    } else {
        throw new DistributionException("could not install package from stream");
    }
}

From source file:com.microsoft.windowsazure.core.pipeline.jersey.RetryPolicyFilter.java

private InputStream getEntityStream(ServiceRequestContext request) {
    if (request.getEntity() == null) {
        return null;
    }/*  ww w.  j a va 2  s.com*/

    if (!(request.getEntity() instanceof InputStream)) {
        return null;
    }

    InputStream entityStream = (InputStream) request.getEntity();

    // If the entity is an InputStream that doesn't support "mark/reset", we
    // can't
    // implement a retry logic, so we simply throw.
    if (!entityStream.markSupported()) {
        throw new IllegalArgumentException("The input stream for the request entity must support 'mark' and "
                + "'reset' to be compatible with a retry policy filter.");
    }

    return entityStream;
}

From source file:org.apache.xmlgraphics.image.loader.impl.PNGFile.java

public PNGFile(InputStream stream) throws IOException, ImageException {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }/*  w  w  w  .  j  av a2s. c  o m*/
    DataInputStream distream = null;
    try {
        distream = new DataInputStream(stream);
        final long magic = distream.readLong();
        if (magic != PNG_SIGNATURE) {
            final String msg = PropertyUtil.getString("PNGImageDecoder0");
            throw new ImageException(msg);
        }
        // only some chunks are worth parsing in the current implementation
        do {
            try {
                PNGChunk chunk;
                final String chunkType = PNGChunk.getChunkType(distream);
                if (chunkType.equals(PNGChunk.ChunkType.IHDR.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    parse_IHDR_chunk(chunk);
                } else if (chunkType.equals(PNGChunk.ChunkType.PLTE.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    parse_PLTE_chunk(chunk);
                } else if (chunkType.equals(PNGChunk.ChunkType.IDAT.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    this.streamVec.add(new ByteArrayInputStream(chunk.getData()));
                } else if (chunkType.equals(PNGChunk.ChunkType.IEND.name())) {
                    // chunk = PNGChunk.readChunk(distream);
                    PNGChunk.skipChunk(distream);
                    break; // fall through to the bottom
                } else if (chunkType.equals(PNGChunk.ChunkType.tRNS.name())) {
                    chunk = PNGChunk.readChunk(distream);
                    parse_tRNS_chunk(chunk);
                } else {
                    // chunk = PNGChunk.readChunk(distream);
                    PNGChunk.skipChunk(distream);
                }
            } catch (final Exception e) {
                log.error("Exception", e);
                final String msg = PropertyUtil.getString("PNGImageDecoder2");
                throw new RuntimeException(msg);
            }
        } while (true);
    } finally {
        IOUtils.closeQuietly(distream);
    }
}

From source file:org.apache.sling.distribution.packaging.impl.AbstractDistributionPackageBuilder.java

@Nonnull
public DistributionPackage readPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream)
        throws DistributionException {

    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }/*from   ww  w. j  a  va  2s. c  om*/
    Map<String, Object> headerInfo = new HashMap<String, Object>();
    DistributionPackageUtils.readInfo(stream, headerInfo);

    try {
        stream.reset();
    } catch (IOException e) {
        // do nothing
    }

    DistributionPackage distributionPackage = SimpleDistributionPackage.fromStream(stream, type);

    try {
        stream.reset();
    } catch (IOException e) {
        // do nothing
    }

    // not a simple package
    if (distributionPackage == null) {
        distributionPackage = readPackageInternal(resourceResolver, stream);
    }

    distributionPackage.getInfo().putAll(headerInfo);
    return distributionPackage;
}