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

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

Introduction

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

Prototype

public abstract OutputStream openStream() throws IOException;

Source Link

Document

Opens a new OutputStream for writing to this sink.

Usage

From source file:com.comphenix.attributes.NbtFactory.java

/**
 * Save the content of a NBT compound to a stream.
 * <p>//  w  w  w .j av  a 2  s  .  c  o m
 * Use {@link Files#asByteSink(File, FileWriteMode...)} to provide a stream supplier to a file.
 *
 * @param source - the NBT compound to save.
 * @param stream - the stream.
 * @param option - whether or not to compress the output.
 * @throws IOException If anything went wrong.
 */
public static void saveStream(NbtCompound source, ByteSink stream, StreamOptions option) throws IOException {
    OutputStream output = null;
    DataOutputStream data = null;
    boolean suppress = true;

    try {
        output = stream.openStream();
        data = new DataOutputStream(
                option == StreamOptions.GZIP_COMPRESSION ? new GZIPOutputStream(output) : output);

        invokeMethod(get().SAVE_COMPOUND, null, source.getHandle(), data);
        suppress = false;

    } finally {
        if (data != null)
            Closeables.close(data, suppress);
        else if (output != null)
            Closeables.close(output, suppress);
    }
}

From source file:net.derquinse.common.io.BytesTransformer.java

/** Transforms a stream of bytes. */
public void transform(InputStream input, ByteSink output) throws IOException {
    checkInput(input);//from  w  w  w .  ja va 2 s . c o m
    checkOutput(output);
    final Closer closer = Closer.create();
    try {
        transform(input, closer.register(output.openStream()));
    } finally {
        closer.close();
    }
}

From source file:net.derquinse.common.io.BytesTransformer.java

/** Transforms a byte source. */
public void transform(ByteSource input, ByteSink output) throws IOException {
    checkInput(input);/* ww w.  j  a v a2  s  . c  om*/
    checkOutput(output);
    final Closer closer = Closer.create();
    try {
        transform(closer.register(input.openStream()), closer.register(output.openStream()));
    } finally {
        closer.close();
    }
}

From source file:io.druid.segment.StringDimensionMergerLegacy.java

@Override
public void writeIndexesToFiles(final ByteSink invertedIndexFile,
        final OutputSupplier<FileOutputStream> spatialIndexFile) throws IOException {
    final SerializerUtils serializerUtils = new SerializerUtils();
    final OutputSupplier<OutputStream> invertedIndexOutputSupplier = new OutputSupplier<OutputStream>() {
        @Override/*  w ww  . j av a  2s.  co  m*/
        public OutputStream getOutput() throws IOException {
            return invertedIndexFile.openStream();
        }
    };

    bitmapWriter.close();
    serializerUtils.writeString(invertedIndexOutputSupplier, dimensionName);
    ByteStreams.copy(bitmapWriter.combineStreams(), invertedIndexOutputSupplier);

    if (capabilities.hasSpatialIndexes()) {
        spatialWriter.close();
        serializerUtils.writeString(spatialIndexFile, dimensionName);
        ByteStreams.copy(spatialWriter.combineStreams(), spatialIndexFile);
        spatialIoPeon.cleanup();
    }
}

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

/**
 * Copies the entire contents of {@code from} into {@code to}.
 *
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 *//*  ww  w . j av  a2  s.co  m*/
@ThreadLocalArray(8192)
public static void copy(@NonNull ByteBuffer from, @NonNull ByteSink to) throws IOException {
    final Closer closer = Closer.create();
    try {
        OutputStream out = closer.register(to.openStream());
        copy(from, out);
        out.flush();
    } 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  www.  j  a  va2 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 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: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 from} is not closed.
 * //from  www. ja v  a  2s .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 @WillNotClose InputStream 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(from, out, limit);
        out.flush();
        return total;
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

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

/**
 * Copies the entire contents of {@code from}, starting at index {@code off}
 * (inclusive) and ending at index {@code off + len} (exclusive), into
 * {@code to}. This provides an alternative to
 * {@link ByteSink#write(byte[])}.//from ww w.  j  ava  2s .c o m
 *
 * @param from the source to read bytes from
 * @param off the offset into {@code from} to start copying
 * @param len the number of bytes to copy starting at index {@code off}
 * @param to the destination to copy bytes read from {@code from} into
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @see ByteSink#write(byte[])
 */
public static void copy(byte[] from, int off, int len, @NonNull ByteSink to) throws IOException {
    checkPositionIndexes(off, off + len, from.length);
    final Closer closer = Closer.create();
    try {
        OutputStream out = closer.register(to.openStream());
        out.write(from, off, len);
        out.flush();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

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

@Override
public long copyTo(ByteSink sink) throws IOException {
    Closer closer = Closer.create();//from   w w  w.  j av  a2  s.c  om
    try {
        if (preferChannel() && sink instanceof ChannelSink && ((ChannelSink) sink).preferChannel()) {
            return copyTo(closer.register(((ChannelSink) sink).openChannel()));
        } else {
            OutputStream out = closer.register(sink.openStream());
            long total = copyTo(out);
            out.flush();
            return total;
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } 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 from} is not closed.
 * /*  ww  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 @WillNotClose ReadableByteChannel from, @NonNull ByteSink to, long limit)
        throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        if (to instanceof ChannelSink) {
            return copy(from, closer.register(((ChannelSink) to).openChannel()), limit);
        } else {
            OutputStream out = closer.register(to.openStream());
            long total = copy(from, out, limit);
            out.flush();
            return total;
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}