use of com.google.common.io.ByteSource in project druid by druid-io.
the class CompressionUtilsTest method testGunzipBugworkarround.
@Test
public // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7036144
void testGunzipBugworkarround() throws IOException {
testFile.delete();
Assert.assertFalse(testFile.exists());
final ByteArrayOutputStream tripleGzByteStream = new ByteArrayOutputStream(gzBytes.length * 3);
tripleGzByteStream.write(gzBytes);
tripleGzByteStream.write(gzBytes);
tripleGzByteStream.write(gzBytes);
final ByteSource inputStreamFactory = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ZeroRemainingInputStream(new ByteArrayInputStream(tripleGzByteStream.toByteArray()));
}
};
Assert.assertEquals((long) (expected.length * 3), CompressionUtils.gunzip(inputStreamFactory, testFile).size());
try (final InputStream inputStream = new FileInputStream(testFile)) {
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(expected.length * 3)) {
Assert.assertEquals("Read terminated too soon (7036144)", expected.length * 3, ByteStreams.copy(inputStream, outputStream));
final byte[] found = outputStream.toByteArray();
Assert.assertEquals(expected.length * 3, found.length);
Assert.assertArrayEquals(expected, Arrays.copyOfRange(found, expected.length * 0, expected.length * 1));
Assert.assertArrayEquals(expected, Arrays.copyOfRange(found, expected.length * 1, expected.length * 2));
Assert.assertArrayEquals(expected, Arrays.copyOfRange(found, expected.length * 2, expected.length * 3));
}
}
}
use of com.google.common.io.ByteSource in project buck by facebook.
the class HttpArtifactCache method storeImpl.
@Override
protected void storeImpl(ArtifactInfo info, final Path file, final Finished.Builder eventBuilder) throws IOException {
// Build the request, hitting the multi-key endpoint.
Request.Builder builder = new Request.Builder();
final HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest(info, new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return projectFilesystem.newFileInputStream(file);
}
});
eventBuilder.getStoreBuilder().setRequestSizeBytes(storeRequest.getContentLength());
// Wrap the file into a `RequestBody` which uses `ProjectFilesystem`.
builder.put(new RequestBody() {
@Override
public MediaType contentType() {
return OCTET_STREAM_CONTENT_TYPE;
}
@Override
public long contentLength() throws IOException {
return storeRequest.getContentLength();
}
@Override
public void writeTo(BufferedSink bufferedSink) throws IOException {
StoreWriteResult writeResult = storeRequest.write(bufferedSink.outputStream());
eventBuilder.getStoreBuilder().setArtifactContentHash(writeResult.getArtifactContentHashCode().toString());
}
});
// Dispatch the store operation and verify it succeeded.
try (HttpResponse response = storeClient.makeRequest("/artifacts/key", builder)) {
final boolean requestFailed = response.statusCode() != HttpURLConnection.HTTP_ACCEPTED;
if (requestFailed) {
reportFailure("store(%s, %s): unexpected response: [%d:%s].", response.requestUrl(), info.getRuleKeys(), response.statusCode(), response.statusMessage());
}
eventBuilder.getStoreBuilder().setWasStoreSuccessful(!requestFailed);
}
}
use of com.google.common.io.ByteSource in project buck by facebook.
the class ArtifactCacheHandler method handleGet.
private int handleGet(Request baseRequest, HttpServletResponse response) throws IOException {
if (!artifactCache.isPresent()) {
response.getWriter().write("Serving local cache is disabled for this instance.");
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
String path = baseRequest.getUri().getPath();
String[] pathElements = path.split("/");
if (pathElements.length != 4 || !pathElements[2].equals("key")) {
response.getWriter().write("Incorrect url format.");
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
RuleKey ruleKey = new RuleKey(pathElements[3]);
Path temp = null;
try {
projectFilesystem.mkdirs(projectFilesystem.getBuckPaths().getScratchDir());
temp = projectFilesystem.createTempFile(projectFilesystem.getBuckPaths().getScratchDir(), "outgoing_rulekey", ".tmp");
CacheResult fetchResult = artifactCache.get().fetch(ruleKey, LazyPath.ofInstance(temp));
if (!fetchResult.getType().isSuccess()) {
return HttpServletResponse.SC_NOT_FOUND;
}
final Path tempFinal = temp;
HttpArtifactCacheBinaryProtocol.FetchResponse fetchResponse = new HttpArtifactCacheBinaryProtocol.FetchResponse(ImmutableSet.of(ruleKey), fetchResult.getMetadata(), new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return projectFilesystem.newFileInputStream(tempFinal);
}
});
fetchResponse.write(response.getOutputStream());
response.setContentLengthLong(fetchResponse.getContentLength());
return HttpServletResponse.SC_OK;
} finally {
if (temp != null) {
projectFilesystem.deleteFileAtPathIfExists(temp);
}
}
}
use of com.google.common.io.ByteSource in project buck by facebook.
the class StubJarIntegrationTest method abiJarManifestShouldContainHashesOfItsFiles.
@Test
public void abiJarManifestShouldContainHashesOfItsFiles() throws IOException {
Path out = Paths.get("junit-abi.jar");
Path regularJar = testDataDir.resolve("junit.jar");
new StubJar(regularJar).writeTo(filesystem, out);
try (JarFile stubJar = new JarFile(filesystem.resolve(out).toFile())) {
Manifest manifest = stubJar.getManifest();
Enumeration<JarEntry> entries = stubJar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (JarFile.MANIFEST_NAME.equals(entry.getName())) {
continue;
}
String seenDigest = manifest.getAttributes(entry.getName()).getValue("Murmur3-128-Digest");
String expectedDigest;
try (InputStream inputStream = stubJar.getInputStream(entry)) {
ByteSource byteSource = ByteSource.wrap(ByteStreams.toByteArray(inputStream));
expectedDigest = byteSource.hash(Hashing.murmur3_128()).toString();
}
assertEquals(String.format("Digest mismatch for %s", entry.getName()), expectedDigest, seenDigest);
}
}
}
use of com.google.common.io.ByteSource in project druid by druid-io.
the class GoogleTaskLogsTest method testStreamTaskLogWithNegative.
@Test
public void testStreamTaskLogWithNegative() throws Exception {
final String testLog = "hello this is a log";
final String logPath = prefix + "/" + taskid;
expect(storage.exists(bucket, logPath)).andReturn(true);
expect(storage.size(bucket, logPath)).andReturn((long) testLog.length());
expect(storage.get(bucket, logPath)).andReturn(new ByteArrayInputStream(testLog.getBytes(Charsets.UTF_8)));
replayAll();
final Optional<ByteSource> byteSource = googleTaskLogs.streamTaskLog(taskid, -3);
final StringWriter writer = new StringWriter();
IOUtils.copy(byteSource.get().openStream(), writer, "UTF-8");
Assert.assertEquals(writer.toString(), testLog.substring(testLog.length() - 3));
verifyAll();
}
Aggregations