Example usage for com.google.common.io ByteStreams limit

List of usage examples for com.google.common.io ByteStreams limit

Introduction

In this page you can find the example usage for com.google.common.io ByteStreams limit.

Prototype

public static InputStream limit(InputStream in, long limit) 

Source Link

Document

Wraps a InputStream , limiting the number of bytes which can be read.

Usage

From source file:com.ibm.og.util.io.Streams.java

private static InputStream create(final byte[] buf, final long size) {
    return ByteStreams.limit(new InfiniteInputStream(buf), size);
}

From source file:com.addthis.hydra.uber.HoconConfigurationFactory.java

static Configuration getReformattingJsonConfiguration(final ConfigurationSource source) {
    try (InputStream configStream = source.getInputStream()) {
        byte[] buffer = ByteStreams.toByteArray(ByteStreams.limit(configStream, MAX_CONFIG_SIZE));
        String configString = new String(buffer, StandardCharsets.UTF_8);
        Config config = ConfigFactory.parseString(configString,
                ConfigParseOptions.defaults().setOriginDescription(source.getLocation()));
        // if it kinda looks like a job config, support its overrides and ignore everything else
        if (config.hasPath("global")) {
            config = config.getConfig("global").withFallback(ConfigFactory.load()).resolve()
                    .getConfig("logging");
        } else {//from   ww w  .j  a va2 s  .  com
            config = config.resolveWith((ConfigFactory.defaultOverrides()),
                    ConfigResolveOptions.defaults().setAllowUnresolved(true));
        }
        config = config.resolve();
        ConfigObject configObject = config.root();
        for (String key : configObject.keySet()) {
            if (key.charAt(0) == '_') {
                config = config.root().withoutKey(key).toConfig();
            }
        }
        String json = config.root().render(ConfigRenderOptions.concise());
        InputStream fakeStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
        ConfigurationSource fakeSource;
        if (source.getFile() != null) {
            fakeSource = new ConfigurationSource(fakeStream, source.getFile());
        } else if (source.getURL() != null) {
            fakeSource = new ConfigurationSource(fakeStream, source.getURL());
        } else {
            fakeSource = new ConfigurationSource(fakeStream);
        }
        return new HoconConfiguration(fakeSource);
    } catch (final Exception ex) {
        LOGGER.error("Error parsing {}", source.getLocation(), ex);
    }
    return null;
}

From source file:com.ansorgit.plugins.bash.util.content.ShebangHeuristic.java

public double isBashFile(File file) {
    try {/*from  w w  w.  ja va 2  s .  c o  m*/
        String data = FileUtil.loadTextAndClose(ByteStreams.limit(new FileInputStream(file), readLimit));

        for (String s : validStarts) {
            if (data.startsWith(s)) {
                return weight;
            }
        }

        return 0;
    } catch (java.io.IOException e) {
        return 0;
    }
}

From source file:org.apache.beam.runners.dataflow.worker.ByteStringCoder.java

@Override
public ByteString decode(InputStream is) throws IOException {
    int size = VarInt.decodeInt(is);
    return ByteString.readFrom(ByteStreams.limit(is, size), size);
}

From source file:com.streamsets.pipeline.lib.parser.binary.BinaryDataParser.java

public byte[] getDataToParse() throws IOException, DataParserException {
    byte[] bytes = ByteStreams.toByteArray(ByteStreams.limit(is, maxDataLength));
    if (maxDataLength == bytes.length) {
        //check if there is more data in the stream than 'maxDataLength'.
        //If yes, the record must be sent to error.
        //Does not make sense truncating binary data as we don't know what it is.
        if (is.read() != -1) {
            throw new DataParserException(Errors.BINARY_PARSER_00, id, maxDataLength);
        }//ww  w  . j ava 2  s  .  co m
    }
    offset = bytes.length;
    return bytes;
}

From source file:org.apache.beam.sdk.coders.LengthPrefixCoder.java

@Override
public T decode(InputStream inStream) throws CoderException, IOException {
    long size = VarInt.decodeLong(inStream);
    return valueCoder.decode(ByteStreams.limit(inStream, size), Context.OUTER);
}

From source file:garmintools.files.NavigationDataFileFactory.java

private void readSection(TableOfContentsEntry entry, CountingInputStream countingInputStream,
        SectionManager.GarminBuilder sectionManagerBuilder) throws IOException {
    Preconditions.checkState(countingInputStream.getCount() == entry.fileOffset);
    InputStream sectionInputStream = ByteStreams.limit(countingInputStream, entry.actualLength);
    ByteBuffer byteBuffer = ByteBuffer.wrap(ByteStreams.toByteArray(sectionInputStream))
            .order(ByteOrder.LITTLE_ENDIAN);
    logger.info(String.format("Reading section %d", entry.sectionNumber));
    sectionManagerBuilder.addSection(entry, byteBuffer);
    Preconditions.checkState(!byteBuffer.hasRemaining(),
            String.format("Trailing input (%d of %d bytes)", byteBuffer.remaining(), entry.actualLength));
}

From source file:org.obm.push.mail.MessageInputStreamProviderImpl.java

@Override
public InputStream createMessageInputStream(IMAPMessage messageToFetch, MimeAddress mimePartAddress,
        Integer limit) {//from w  w  w.  ja va  2  s  .c o m
    IMAPInputStream imapInputStream = new IMAPInputStream(messageToFetch, address(mimePartAddress),
            NO_MAX_BYTE_COUNT, USE_PEEK);
    return ByteStreams.limit(imapInputStream, limit);
}

From source file:net.pterodactylus.sonitus.io.IdentifyingInputStream.java

/**
 * Tries to identify the given input stream.
 *
 * @param inputStream//from   w ww . ja  va 2s  .c o  m
 *       The input stream to identify
 * @return An identifying input stream that delivers the original stream and
 *         the metadata it detected, or {@link Optional#absent()} if no
 *         metadata could be identified
 * @throws IOException
 *       if an I/O error occurs
 */
public static Optional<IdentifyingInputStream> create(InputStream inputStream) throws IOException {

    /* remember everything we read here. */
    RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream);

    /* first, try formats with unambiguous layouts. */
    try {
        Optional<Metadata> metadata = FlacIdentifier.identify(rememberingInputStream);
        if (metadata.isPresent()) {
            return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
        }
    } catch (EOFException eofe1) {
        /* ignore. */
    }

    /* try Ogg Vorbis next. */
    try {
        rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
        Optional<Metadata> metadata = OggVorbisIdentifier.identify(rememberingInputStream);
        if (metadata.isPresent()) {
            return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
        }
    } catch (EOFException eofe1) {
        /* ignore. */
    }

    /* finally, try MP3. */
    try {
        rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
        InputStream limitedInputStream = ByteStreams.limit(rememberingInputStream, 1048576);
        Optional<Metadata> metadata = Mp3Identifier.identify(limitedInputStream);
        if (metadata.isPresent()) {
            return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
        }
    } catch (EOFException eofe1) {
        /* ignore. */
    }

    return Optional.absent();
}

From source file:org.apache.beam.sdk.coders.ByteStringCoder.java

@Override
public ByteString decode(InputStream inStream, Context context) throws IOException {
    if (context.isWholeStream) {
        return ByteString.readFrom(inStream);
    }//  ww  w .j av  a2s  . c  o  m

    int size = VarInt.decodeInt(inStream);
    // ByteString reads to the end of the input stream, so give it a limited stream of exactly
    // the right length. Also set its chunk size so that the ByteString will contain exactly
    // one chunk.
    return ByteString.readFrom(ByteStreams.limit(inStream, size), size);
}