com.google.common.io.ByteSink Java Examples

The following examples show how to use com.google.common.io.ByteSink. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JarFileServiceWrapper.java    From bistoury with GNU General Public License v3.0 6 votes vote down vote up
private void unPackJar(final String jarFilePath, final File target) {
    try (JarFile jarFile = new JarFile(URLUtil.removeProtocol(jarFilePath))) {
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                new File(target, entry.getName()).mkdirs();
            } else {
                File file = new File(target, entry.getName());
                if (file.createNewFile()) {
                    try (InputStream inputStream = jarFile.getInputStream(entry)) {
                        ByteSink byteSink = Files.asByteSink(file);
                        byteSink.writeFrom(inputStream);
                    }

                }
            }

        }
    } catch (Exception e) {
        logger.error("", "unpack jar error", e);
    }
}
 
Example #2
Source File: RemoteHttpFile.java    From copybara with Apache License 2.0 6 votes vote down vote up
protected synchronized void download() throws RepoException, ValidationException {
  if (downloaded) {
    return;
  }
  URL remote = getRemote();
  try {
    console.progressFmt("Fetching %s", remote);
    ByteSink sink = getSink();
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    try (ProfilerTask task = profiler.start("remote_file_" + remote)) {
      try (DigestInputStream is = new DigestInputStream(transport.open(remote), digest)) {
        sink.writeFrom(is);
        sha256 = Optional.of(Bytes.asList(is.getMessageDigest().digest()).stream()
            .map(b -> String.format("%02X", b)).collect(Collectors.joining()).toLowerCase());
      }
      downloaded = true;
    }
  } catch (IOException | NoSuchAlgorithmException e) {
    throw new RepoException(String.format("Error downloading %s", remote), e);
  }
}
 
Example #3
Source File: QuoteFilter.java    From tac-kbp-eal with MIT License 6 votes vote down vote up
public void saveTo(ByteSink sink) throws IOException {
  final PrintWriter out = new PrintWriter(sink.asCharSink(Charsets.UTF_8).openBufferedStream());

  out.println(docIdToBannedRegions.size());

  for (final Map.Entry<Symbol, ImmutableRangeSet<Integer>> entry : docIdToBannedRegions
      .entrySet()) {
    out.println(entry.getKey());
    final List<String> parts = Lists.newArrayList();
    for (final Range<Integer> r : entry.getValue().asRanges()) {
      // we know by construction these ranges are bounded above and below
      parts.add(String.format("%d-%d", r.lowerEndpoint(), r.upperEndpoint()));
    }
    out.println(StringUtils.spaceJoiner().join(parts));
  }

  out.close();
}
 
Example #4
Source File: EAScoringObserver.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
private void writeArray(DoubleArrayList numbers, File file) throws IOException {
  final ByteSink sink = GZIPByteSink.gzipCompress(Files.asByteSink(file));
  final PrintWriter out = new PrintWriter(sink.asCharSink(Charsets.UTF_8).openBufferedStream());
  for (final DoubleCursor cursor : numbers) {
    out.println(cursor.value);
  }
  out.close();
  ;
}
 
Example #5
Source File: NonIoByteSource.java    From mongowp with Apache License 2.0 5 votes vote down vote up
public long copyTo(ByteSink sink) {
  try {
    return delegate.copyTo(sink);
  } catch (IOException ex) {
    throw new AssertionError("An illegal IOException was throw");
  }
}
 
Example #6
Source File: FileSystemUtils.java    From bazel with Apache License 2.0 5 votes vote down vote up
public static ByteSink asByteSink(final Path path, final boolean append) {
  return new ByteSink() {
    @Override public OutputStream openStream() throws IOException {
      return path.getOutputStream(append);
    }
  };
}
 
Example #7
Source File: GuavaIOUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenWriteUsingByteSink_thenWritten() throws IOException {
    final String expectedValue = "Hello world";
    final File file = new File("src/test/resources/test.out");
    final ByteSink sink = Files.asByteSink(file);

    sink.write(expectedValue.getBytes());

    final String result = Files.toString(file, Charsets.UTF_8);
    assertEquals(expectedValue, result);
}
 
Example #8
Source File: Streams.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the specified string to stream with buffering and closes the stream.
 *
 * @param str String to write.
 * @param outputStream Stream to write to.
 * @param charset The charset to write in.
 * @throws IOException If there is an exception while writing.
 */
public static void write(String str, final OutputStream outputStream, Charset charset)
    throws IOException {
  new ByteSink() {
    @Override
    public OutputStream openStream() {
      return outputStream;
    }
  }.asCharSink(charset).write(str);
}
 
Example #9
Source File: Streams.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Copies the {@code inputStream} into the {@code outputSteam} and finally
 * closes the both streams.
 */
public static void copy(final InputStream inputStream, final OutputStream outputStream)
    throws IOException {
  new ByteSource() {
    @Override
    public InputStream openStream() {
      return inputStream;
    }
  }.copyTo(new ByteSink() {
    @Override
    public OutputStream openStream() {
      return outputStream;
    }
  });
}
 
Example #10
Source File: Media.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given {@code byte[]} to a stream.
 *
 * @throws IOException if the steam cannot be written to
 */
@VisibleForTesting
static void writeBytesToStream(byte[] bytes, final OutputStream outputStream)
    throws IOException {
  new ByteSink() {
    @Override
    public OutputStream openStream() {
      return outputStream;
    }
  }.write(bytes);
}
 
Example #11
Source File: GithubArchive.java    From copybara with Apache License 2.0 4 votes vote down vote up
@Override
protected ByteSink getSink() throws ValidationException {
  return NullByteSink.INSTANCE;
}
 
Example #12
Source File: FstInputOutput.java    From jopenfst with MIT License 4 votes vote down vote up
public static void writeFstToBinaryFile(Fst fst, File file) throws IOException {
  ByteSink bs = Files.asByteSink(file);
  try (ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(bs.openBufferedStream()))) {
    writeFstToBinaryStream(fst, oos);
  }
}
 
Example #13
Source File: FileSystemUtils.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static ByteSink asByteSink(final Path path) {
  return asByteSink(path, false);
}
 
Example #14
Source File: TestHttpServer.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
@Override
public HttpContext service(final HttpRequest request, final HttpResponse response)
    throws IOException, HttpException {
  request.setState(HttpMessage.__MSG_EDITABLE);
  this.authorizationHttpHeaders.add(request.getHeader().get("Authorization"));
  
  // Read the raw bytes from the request.
  final byte[] rawRequestBytes = new ByteSource() {
    @Override
    public InputStream openStream() throws IOException {
      return request.getInputStream();
    }
  }.read();
  
  // Inflate the raw bytes if they are in gzip format. 
  boolean isGzipFormat = "gzip".equals(request.getHeader().get(HttpFields.__ContentEncoding));
  
  byte[] requestBytes;
  if (isGzipFormat) {
    requestBytes = new ByteSource(){
      @Override
      public InputStream openStream() throws IOException {
        return new GZIPInputStream(ByteSource.wrap(rawRequestBytes).openStream());
      }
    }.read();
  } else {
    requestBytes = rawRequestBytes;
  }
  
  // Convert the (possibly inflated) request bytes to a string.
  this.requestBodies.add(
      ByteSource.wrap(requestBytes).asCharSource(Charset.forName(UTF_8)).read());
  this.requestBodiesCompressionStates.add(isGzipFormat); 
  
  // Simulate a delay in processing.
  simulateDelay();  
  
  new ByteSink() {
    @Override
    public OutputStream openStream() {
      return response.getOutputStream();
    }
  }.asCharSink(Charset.forName(UTF_8)).write(mockResponseBodies.get(numInteractions++));

  return getContext(getServerUrl());
}
 
Example #15
Source File: NbtFactory.java    From AdditionsAPI with MIT License 2 votes vote down vote up
/**
 * Save the content of a NBT compound to a stream.
 * <p>
 * Use {@link Files#newOutputStreamSupplier(java.io.File)} to provide a stream supplier to a file.
 * @param stream - the output stream.
 * @param option - whether or not to compress the output.
 * @throws IOException If anything went wrong.
 */
public NbtCompound saveTo(ByteSink stream, StreamOptions option) throws IOException {
    saveStream(this, stream, option);
    return this;
}
 
Example #16
Source File: RemoteHttpFile.java    From copybara with Apache License 2.0 2 votes vote down vote up
/**
 * Sink that receives the downloaded files.
 */
protected abstract ByteSink getSink() throws ValidationException;