Search in sources :

Example 6 with ByteSource

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));
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteSource(com.google.common.io.ByteSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 7 with ByteSource

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);
    }
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) Request(okhttp3.Request) HttpResponse(com.facebook.buck.slb.HttpResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) ByteSource(com.google.common.io.ByteSource) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 8 with ByteSource

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);
        }
    }
}
Also used : BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) Path(java.nio.file.Path) RuleKey(com.facebook.buck.rules.RuleKey) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) HttpArtifactCacheBinaryProtocol(com.facebook.buck.artifact_cache.HttpArtifactCacheBinaryProtocol) CacheResult(com.facebook.buck.artifact_cache.CacheResult) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException)

Example 9 with ByteSource

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);
        }
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ByteSource(com.google.common.io.ByteSource) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Example 10 with ByteSource

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();
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(com.google.common.io.ByteSource) Test(org.junit.Test)

Aggregations

ByteSource (com.google.common.io.ByteSource)138 IOException (java.io.IOException)58 Test (org.junit.Test)58 InputStream (java.io.InputStream)42 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)18 Metacard (ddf.catalog.data.Metacard)17 ContentItem (ddf.catalog.content.data.ContentItem)16 StringWriter (java.io.StringWriter)14 FileInputStream (java.io.FileInputStream)13 Test (org.junit.jupiter.api.Test)12 URI (java.net.URI)11 Path (java.nio.file.Path)11 ArrayList (java.util.ArrayList)11 URL (java.net.URL)10 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)9 FilterInputStream (java.io.FilterInputStream)8