下面列出了怎么用com.google.common.io.ByteSink的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
}
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);
}
}
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();
}
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();
;
}
public long copyTo(ByteSink sink) {
try {
return delegate.copyTo(sink);
} catch (IOException ex) {
throw new AssertionError("An illegal IOException was throw");
}
}
public static ByteSink asByteSink(final Path path, final boolean append) {
return new ByteSink() {
@Override public OutputStream openStream() throws IOException {
return path.getOutputStream(append);
}
};
}
@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);
}
/**
* 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);
}
/**
* 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;
}
});
}
/**
* 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);
}
@Override
protected ByteSink getSink() throws ValidationException {
return NullByteSink.INSTANCE;
}
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);
}
}
public static ByteSink asByteSink(final Path path) {
return asByteSink(path, false);
}
@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());
}
/**
* 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;
}
/**
* Sink that receives the downloaded files.
*/
protected abstract ByteSink getSink() throws ValidationException;