类com.google.common.io.ByteSink源码实例Demo

下面列出了怎么用com.google.common.io.ByteSink的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: bistoury   文件: JarFileServiceWrapper.java
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);
    }
}
 
源代码2 项目: copybara   文件: RemoteHttpFile.java
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);
  }
}
 
源代码3 项目: tac-kbp-eal   文件: QuoteFilter.java
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();
}
 
源代码4 项目: tac-kbp-eal   文件: EAScoringObserver.java
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();
  ;
}
 
源代码5 项目: mongowp   文件: NonIoByteSource.java
public long copyTo(ByteSink sink) {
  try {
    return delegate.copyTo(sink);
  } catch (IOException ex) {
    throw new AssertionError("An illegal IOException was throw");
  }
}
 
源代码6 项目: bazel   文件: FileSystemUtils.java
public static ByteSink asByteSink(final Path path, final boolean append) {
  return new ByteSink() {
    @Override public OutputStream openStream() throws IOException {
      return path.getOutputStream(append);
    }
  };
}
 
源代码7 项目: tutorials   文件: GuavaIOUnitTest.java
@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);
}
 
源代码8 项目: googleads-java-lib   文件: Streams.java
/**
 * 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);
}
 
源代码9 项目: googleads-java-lib   文件: Streams.java
/**
 * 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;
    }
  });
}
 
源代码10 项目: googleads-java-lib   文件: Media.java
/**
 * 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);
}
 
源代码11 项目: copybara   文件: GithubArchive.java
@Override
protected ByteSink getSink() throws ValidationException {
  return NullByteSink.INSTANCE;
}
 
源代码12 项目: jopenfst   文件: FstInputOutput.java
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);
  }
}
 
源代码13 项目: bazel   文件: FileSystemUtils.java
public static ByteSink asByteSink(final Path path) {
  return asByteSink(path, false);
}
 
源代码14 项目: googleads-java-lib   文件: TestHttpServer.java
@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());
}
 
源代码15 项目: AdditionsAPI   文件: NbtFactory.java
/**
 * 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;
}
 
源代码16 项目: copybara   文件: RemoteHttpFile.java
/**
 * Sink that receives the downloaded files.
 */
protected abstract ByteSink getSink() throws ValidationException;