Example usage for com.google.common.io ByteSource openStream

List of usage examples for com.google.common.io ByteSource openStream

Introduction

In this page you can find the example usage for com.google.common.io ByteSource openStream.

Prototype

public abstract InputStream openStream() throws IOException;

Source Link

Document

Opens a new InputStream for reading from this source.

Usage

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code to} is not closed or
 * flushed./*w  ww  .  j av  a 2  s.  c o m*/
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull @WillNotClose OutputStream to, long limit)
        throws IOException {
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to, limit);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code to} is not closed or
 * flushed./*  w  w  w .  j av  a 2s. c om*/
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull PrimitiveSink to, long limit) throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to, limit);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. Byte processing stops on EOF
 * or when {@link ByteProcessor#processBytes(byte[], int, int) processBytes}
 * returns {@code false}./*  w  w  w. j av  a2  s.c  om*/
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull ByteProcessor<?> to, long limit) throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to, limit);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Provides an alternative to {@link ByteSource#read()}.
 *//*from w  w  w.j  av a  2  s. c  o  m*/
@ThreadLocalArray(8192)
public static byte[] toByteArray(ByteSource source) throws IOException {
    final Closer closer = Closer.create();
    try {
        if (source instanceof ChannelSource && ((ChannelSource) source).hasKnownSize()) {
            return toByteArray(closer.register(source.openStream()), checkByteSourceSize(source));
        } else {
            return toByteArray(closer.register(source.openStream()));
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
static boolean contentEqualsImpl(@NonNull ByteSource source, byte[] bytes, int off, int len)
        throws IOException {
    checkPositionIndexes(off, off + len, bytes.length);
    final Closer closer = Closer.create();
    try {/*from  w  w w  . j  av  a  2s  . c  om*/
        return contentEquals(closer.register(source.openStream()), bytes, off, len);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. This method <i>always</i>
 * copies {@code from} into {@code to} in chunks;
 * {@link ByteSource#copyTo(ByteSink)} or
 * {@link ByteSource#copyTo(OutputStream)} is never used.
 * //from   w  w w.j a v  a  2 s .co m
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull ByteSink to, long limit) throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        OutputStream out = closer.register(to.openStream());
        long total = copy(closer.register(from.openStream()), out, limit);
        out.flush();
        return total;
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.opendaylight.yangtools.yang.parser.impl.YangParserImpl.java

private Map<ByteSource, ParseTree> parseYangSources(final Collection<ByteSource> sources)
        throws IOException, YangSyntaxErrorException {
    final Map<ByteSource, ParseTree> trees = new HashMap<>();
    for (ByteSource source : sources) {
        try (InputStream stream = source.openStream()) {
            trees.put(source, parseYangSource(stream));
        }/*from w  w  w .j  ava 2s .  c om*/
    }
    return trees;
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code to} is not closed.
 * /*from  w w  w .  j  a v  a2 s. c om*/
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull @WillNotClose WritableByteChannel to, long limit)
        throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        if (from instanceof ChannelSource) {
            return copy(closer.register(((ChannelSource) from).openChannel()), to, limit);
        } else {
            return copy(closer.register(from.openStream()), to, limit);
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.onos.yangtools.yang.parser.impl.YangParserImpl.java

private Map<ByteSource, ModuleBuilder> parseSourcesToBuilders(final Collection<ByteSource> sources,
        final SchemaContext context) throws IOException, YangSyntaxErrorException {
    final ParseTreeWalker walker = new ParseTreeWalker();
    final Map<ByteSource, ParseTree> sourceToTree = parseYangSources(sources);
    final Map<ByteSource, ModuleBuilder> sourceToBuilder = new LinkedHashMap<>();

    // validate yang
    new YangModelBasicValidator(walker).validate(sourceToTree.values());

    Map<String, NavigableMap<Date, URI>> namespaceContext = BuilderUtils
            .createYangNamespaceContext(sourceToTree.values(), Optional.fromNullable(context));
    YangParserListenerImpl yangModelParser;
    for (Map.Entry<ByteSource, ParseTree> entry : sourceToTree.entrySet()) {
        ByteSource source = entry.getKey();
        String path = null; // TODO refactor to Optional
        // TODO refactor so that path can be retrieved without opening
        // stream: NamedInputStream -> NamedByteSource ?
        try (InputStream stream = source.openStream()) {
            if (stream instanceof NamedInputStream) {
                path = stream.toString();
            }//from   w  w w .  ja  v  a2 s. co m
        }
        yangModelParser = new YangParserListenerImpl(namespaceContext, path);
        walker.walk(yangModelParser, entry.getValue());
        ModuleBuilder moduleBuilder = yangModelParser.getModuleBuilder();
        moduleBuilder.setSource(source);
        sourceToBuilder.put(source, moduleBuilder);
    }
    return sourceToBuilder;
}

From source file:org.opendaylight.yangtools.yang.parser.impl.YangParserImpl.java

private Map<ByteSource, ModuleBuilder> parseSourcesToBuilders(final Collection<ByteSource> sources,
        final SchemaContext context) throws IOException, YangSyntaxErrorException {
    final ParseTreeWalker walker = new ParseTreeWalker();
    final Map<ByteSource, ParseTree> sourceToTree = parseYangSources(sources);
    final Map<ByteSource, ModuleBuilder> sourceToBuilder = new LinkedHashMap<>();

    // validate yang
    new YangModelBasicValidator(walker).validate(sourceToTree.values());

    Map<String, TreeMap<Date, URI>> namespaceContext = BuilderUtils
            .createYangNamespaceContext(sourceToTree.values(), Optional.fromNullable(context));
    YangParserListenerImpl yangModelParser;
    for (Map.Entry<ByteSource, ParseTree> entry : sourceToTree.entrySet()) {
        ByteSource source = entry.getKey();
        String path = null; // TODO refactor to Optional
        // TODO refactor so that path can be retrieved without opening
        // stream: NamedInputStream -> NamedByteSource ?
        try (InputStream stream = source.openStream()) {
            if (stream instanceof NamedInputStream) {
                path = stream.toString();
            }/* w  w w.  j  a  v  a  2 s .c o m*/
        }
        yangModelParser = new YangParserListenerImpl(namespaceContext, path);
        walker.walk(yangModelParser, entry.getValue());
        ModuleBuilder moduleBuilder = yangModelParser.getModuleBuilder();
        moduleBuilder.setSource(source);
        sourceToBuilder.put(source, moduleBuilder);
    }
    return sourceToBuilder;
}