Java Code Examples for com.google.common.io.ByteStreams#toByteArray()

The following examples show how to use com.google.common.io.ByteStreams#toByteArray() . 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: OfflineDbImpl.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public List<String> listWorkflows() {
    List<String> ls = new ArrayList<>();
    HistoDbConfig config = new HistoDbConfig(connectParams, null, storeName, null);
    try (HistoDbHttpClientImpl httpClient = new HistoDbHttpClientImpl(null)) {
        try (InputStream is = httpClient.getHttpRequest(new HistoDbUrl(config, ".json", Collections.emptyMap()))) {
            String json = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
            JSONObject jsonObj = JSONObject.fromObject(json);
            for (Object name : jsonObj.names()) {
                if (((String) name).startsWith(SIMULATIONS_PREFIX)) {
                    String workflowId = ((String) name).substring(SIMULATIONS_PREFIX.length());
                    ls.add(workflowId);
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return ls;
}
 
Example 2
Source File: BloomFilter.java    From presto-bloomfilter with Apache License 2.0 6 votes vote down vote up
public static BloomFilter fromUrl(String url) throws Exception
{
    log.info("Loading bloom filter from " + url);

    Request request = BloomFilterScalarFunctions.HTTP_CLIENT.newRequest(url);
    request.method("GET");
    InputStreamResponseListener listener = new InputStreamResponseListener();
    request.send(listener);

    // Wait for the response headers to arrive
    Response response = listener.get(10, TimeUnit.SECONDS);

    // Look at the response
    if (response.getStatus() == 200) {
        // Use try-with-resources to close input stream.
        try (InputStream responseContent = listener.getInputStream()) {
            byte[] bytes = ByteStreams.toByteArray(responseContent);
            return newInstance(bytes);
        }
    }
    log.warn("Non-200 response status " + response.getStatus());
    return null;
}
 
Example 3
Source File: BaseProcessorTest.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * Write generated file.
 *
 * @param basePath
 *            the base path
 * @param javaFileObject
 *            the java file object
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
protected static void writeGeneratedFile(PathSourceType basePath, JavaFileObject javaFileObject) throws IOException {
	PathSourceType pathSourceType = basePath;
	Path path = Paths.get(pathSourceType.getPath(), javaFileObject.getName().replace("SOURCE_OUTPUT", ""));

	if (path == null)
		throw new IOException("can not work with " + javaFileObject.getName());

	Path checkPath = Files.createDirectories(path.getParent());
	if (checkPath == null)
		throw new IOException("can not work with " + javaFileObject.getName());

	byte[] bytes = ByteStreams.toByteArray(javaFileObject.openInputStream());

	String pathString = javaFileObject.getName().toString().replace("/SOURCE_OUTPUT", "");
	// if (pathString.startsWith("/")) pathString=pathString.substring(1);
	pathString = basePath.path + pathString;

	pathString = pathString.replaceFirst("//", "/");

	System.out.println("Generate file " + pathString);
	Files.write(path, bytes);
}
 
Example 4
Source File: JDBCLinkStore.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private Object getBlobAsAmqpObject(final ResultSet resultSet, final int index) throws SQLException
{
    byte[] sourceBytes;
    if (_isUseBytesMethodsForBlob)
    {
        sourceBytes = resultSet.getBytes(index);
    }
    else
    {
        Blob blob = resultSet.getBlob(index);
        try (InputStream is = blob.getBinaryStream())
        {
            sourceBytes = ByteStreams.toByteArray(is);
        }
        catch (IOException e)
        {
            throw new StoreException("Cannot convert blob to string", e);
        }
        finally
        {
            blob.free();
        }
    }
    return LinkStoreUtils.amqpBytesToObject(sourceBytes);
}
 
Example 5
Source File: KcaUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 6 votes vote down vote up
public static JsonArray getJsonArrayFromAsset(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    JsonArray data = new JsonArray();
    AssetManager am = cw.getAssets();
    try {
        AssetManager.AssetInputStream ais =
                (AssetManager.AssetInputStream) am.open(name);
        byte[] bytes = ByteStreams.toByteArray(ais);
        data = new JsonParser().parse(new String(bytes)).getAsJsonArray();
        ais.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        if (helper != null) helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonArrayFromStorage", "1", getStringFromException(e1));
    }
    return data;
}
 
Example 6
Source File: BodyReaderHttpServletRequestWrapper.java    From qconfig with MIT License 5 votes vote down vote up
public BodyReaderHttpServletRequestWrapper(final HttpServletRequest request)
        throws IOException {
    super(request);
    try (InputStream inputStream = request.getInputStream()) {
        body = ByteStreams.toByteArray(inputStream);
    }
}
 
Example 7
Source File: AdHocReportDownloadHelperTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandleSuccessfulResponse() throws Exception {
  String responseBody = "Successful,report,response";
  RawReportDownloadResponse rawResponse =
      new RawReportDownloadResponse(
          200,
          ByteSource.wrap(responseBody.getBytes(REPORT_CHARSET)).openStream(),
          REPORT_CHARSET,
          "CSV");
  ReportDownloadResponse reportResponse = helper.handleResponse(rawResponse, exceptionBuilder);
  String actualResponseBody =
      new String(ByteStreams.toByteArray(reportResponse.getInputStream()), REPORT_CHARSET);
  assertEquals("Response body not expected value", responseBody, actualResponseBody);
}
 
Example 8
Source File: TraceRegionSerializerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRewriteV4TracesWithCompressedInt() throws IOException {
	TraceRegionSerializer serializer = new TraceRegionSerializer();
	InputStream in = getClass().getResourceAsStream("version4.trace");
	byte[] v4bytes = ByteStreams.toByteArray(in);
	in.close();
	AbstractTraceRegion traceRegion = serializer.readTraceRegionFrom(new ByteArrayInputStream(v4bytes));
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	serializer.writeTraceRegionTo(traceRegion, outputStream);
	byte[] currentBytes = outputStream.toByteArray();
	Assert.assertTrue(v4bytes.length > currentBytes.length);
	Assert.assertEquals(4610 /* magic number */, v4bytes.length); 
	Assert.assertEquals(1655 /* magic number */, currentBytes.length);
	Assert.assertEquals(traceRegion, serializer.readTraceRegionFrom(new ByteArrayInputStream(currentBytes)));
}
 
Example 9
Source File: ParallelGZIPOutputStreamTest.java    From parallelgzip with Apache License 2.0 5 votes vote down vote up
private void testThreads(@Nonnull ByteArrayOutputBuffer out, @Nonnegative int nthreads) throws Exception {
    Random r = new Random();

    ThreadPoolExecutor executor = ParallelGZIPEnvironment.newThreadPoolExecutor(nthreads);
    try {
        for (int i = 0; i < 3; i++) {
            out.reset();
            // The randomness throws the perf results off a bit, but fuzzes the block sizes.
            byte[] data = new byte[256 * 1024 * 1024 + r.nextInt(1048576)];
            r.nextBytes(data);
            LOG.info("Data is " + data.length + " bytes.");
            {
                Stopwatch stopwatch = Stopwatch.createStarted();
                ParallelGZIPOutputStream gzip = new ParallelGZIPOutputStream(out, executor);
                gzip.write(data);
                gzip.close();
                long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
                LOG.info("nthreads=" + nthreads + "; parallel=" + elapsed);
                gzip = null;
            }
            ParallelGZIPInputStream in = new ParallelGZIPInputStream(out.toInput());
            byte[] copy = ByteStreams.toByteArray(in);
            assertArrayEquals(data, copy);
        }
    } finally {
        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);
    }
}
 
Example 10
Source File: ResettableInputStreamFilter.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public ServletInputStream getInputStream() throws IOException {
    if (servletStream.rawData == null) {
        servletStream.rawData = ByteStreams.toByteArray(request.getInputStream());
        servletStream.reset();
    }
    return servletStream;
}
 
Example 11
Source File: GenericApplicationUploadBean.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected byte[] readStreamContents() throws IOException {
    try (final InputStream stream = this.getStream()) {
        if (stream == null || getFileSize() == 0) {
            return null;
        }
        if (getFileSize() > MAX_FILE_SIZE) {
            throw new DomainException("error.file.to.big");
        }
        byte[] contents = ByteStreams.toByteArray(stream);
        // Ensure the submitted file is a PDF
        new PdfReader(contents);
        return contents;
    }
}
 
Example 12
Source File: OutboundMobilityContextBean.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected String readStreamContents() throws IOException {
    try (final InputStream stream = this.getStream()) {
        if (stream == null || getFileSize() == 0) {
            return null;
        }
        return new String(ByteStreams.toByteArray(stream), StandardCharsets.UTF_8);
    }
}
 
Example 13
Source File: UsedResourcesFinderTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private byte[] getContent(String path) {
  try {
    return ByteStreams.toByteArray(apkZip.getInputStream(apkZip.getEntry(path)));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 14
Source File: HttpTestHelper.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private byte[] readConnectionInputStream(HttpURLConnection connection) throws IOException
{
    try (InputStream is = connection.getInputStream())
    {
        final byte[] bytes = ByteStreams.toByteArray(is);
        if (LOGGER.isTraceEnabled())
        {
            LOGGER.trace("RESPONSE:" + new String(bytes, UTF_8));
        }
        return bytes;
    }
}
 
Example 15
Source File: LicenseDetectionTest.java    From openvsx with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMIT() throws Exception {
    try (
        var stream = getClass().getResourceAsStream("MIT.txt");
    ) {
        var bytes = ByteStreams.toByteArray(stream);
        var detection = new LicenseDetection();
        var result = detection.detectLicense(bytes);
        assertEquals("MIT", result);
    }
}
 
Example 16
Source File: JenkinsHttpClient.java    From jenkins-client-java with MIT License 5 votes vote down vote up
private <T extends BaseModel> T objectFromResponse(Class<T> cls, HttpResponse response) throws IOException {
        InputStream content = response.getEntity().getContent();
        byte[] bytes = ByteStreams.toByteArray(content);
        T result = mapper.readValue(bytes, cls);
        // TODO: original:
        // T result = mapper.readValue(content, cls);
//        result.setClient(this);
        return result;
    }
 
Example 17
Source File: JStackPidExecutor.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
private String readJStackOutput(HotSpotVirtualMachine hotSpotVirtualMachine) throws IOException {
    try (InputStream inputStream = hotSpotVirtualMachine.remoteDataDump(new String[0])) {
        byte[] bytes = ByteStreams.toByteArray(inputStream);
        return new String(bytes, Charsets.UTF_8);
    }
}
 
Example 18
Source File: VisageDistributor.java    From Visage with MIT License 4 votes vote down vote up
public RenderResponse renderRpc(RenderMode mode, int width, int height, GameProfile profile, byte[] skin, Map<String, String[]> switches) throws RenderFailedException, NoRenderersAvailableException {
	if (mode == RenderMode.SKIN) return null;
	try {
		byte[] response = null;
		String corrId = UUID.randomUUID().toString();
		BasicProperties props = new BasicProperties.Builder().correlationId(corrId).replyTo(replyQueue).build();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DeflaterOutputStream defos = new DeflaterOutputStream(baos);
		DataOutputStream dos = new DataOutputStream(defos);
		dos.writeByte(mode.ordinal());
		dos.writeShort(width);
		dos.writeShort(height);
		Profiles.writeGameProfile(dos, profile);
		dos.writeShort(switches.size());
		for (Entry<String, String[]> en : switches.entrySet()) {
			dos.writeUTF(en.getKey());
			dos.writeByte(en.getValue().length);
			for (String s : en.getValue()) {
				dos.writeUTF(s);
			}
		}
		dos.writeInt(skin.length);
		dos.write(skin);
		dos.flush();
		defos.finish();
		channel.basicPublish("", config.getString("rabbitmq.queue"), props, baos.toByteArray());
		if (Visage.debug) Visage.log.finer("Requested a "+width+"x"+height+" "+mode.name().toLowerCase()+" render for "+(profile == null ? "null" : profile.getName()));
		final Object waiter = new Object();
		queuedJobs.put(corrId, new Runnable() {
			@Override
			public void run() {
				if (Visage.debug) Visage.log.finer("Got response");
				synchronized (waiter) {
					waiter.notify();
				}
			}
		});
		long start = System.currentTimeMillis();
		long timeout = config.getDuration("render.timeout", TimeUnit.MILLISECONDS);
		synchronized (waiter) {
			while (queuedJobs.containsKey(corrId) && (System.currentTimeMillis()-start) < timeout) {
				if (Visage.trace) Visage.log.finest("Waiting...");
				waiter.wait(timeout);
			}
		}
		if (queuedJobs.containsKey(corrId)) {
			if (Visage.trace) Visage.log.finest("Queue still contains this request, assuming timeout");
			queuedJobs.remove(corrId);
			throw new RenderFailedException("Request timed out");
		}
		response = responses.get(corrId);
		responses.remove(corrId);
		if (response == null)
			throw new RenderFailedException("Response was null");
		ByteArrayInputStream bais = new ByteArrayInputStream(response);
		String renderer = new DataInputStream(bais).readUTF();
		int type = bais.read();
		byte[] payload = ByteStreams.toByteArray(bais);
		if (type == 0) {
			if (Visage.trace) Visage.log.finest("Got type 0, success");
			RenderResponse resp = new RenderResponse();
			resp.renderer = renderer;
			resp.png = payload;
			Visage.log.info("Receieved a "+mode.name().toLowerCase()+" render from "+resp.renderer);
			return resp;
		} else if (type == 1) {
			if (Visage.trace) Visage.log.finest("Got type 1, failure");
			ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(payload));
			Throwable t = (Throwable)ois.readObject();
			throw new RenderFailedException("Renderer reported error", t);
		} else
			throw new RenderFailedException("Malformed response from '"+renderer+"' - unknown response id "+type);
	} catch (Exception e) {
		if (e instanceof RenderFailedException)
			throw (RenderFailedException) e;
		throw new RenderFailedException("Unexpected error", e);
	}
}
 
Example 19
Source File: IntegrationTestUtils.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
private ResponseInfo(HttpResponse response) throws IOException {
  this.status = response.getStatusCode();
  this.payload = ByteStreams.toByteArray(response.getContent());
}
 
Example 20
Source File: NewProxyServerTestUtil.java    From browserup-proxy with Apache License 2.0 3 votes vote down vote up
/**
 * Reads and closes the input stream, converting it to a String using the UTF-8 charset. The input stream is guaranteed to be closed, even
 * if the reading/conversion throws an exception.
 *
 * @param in UTF-8-encoded InputStream to read
 * @return String of InputStream's contents
 * @throws IOException if an error occurs reading from the stream
 */
public static String toStringAndClose(InputStream in) throws IOException {
    try {
        return new String(ByteStreams.toByteArray(in), StandardCharsets.UTF_8);
    } finally {
        Closeables.closeQuietly(in);
    }
}