List of usage examples for com.google.common.io ByteStreams copy
public static long copy(ReadableByteChannel from, WritableByteChannel to) throws IOException
From source file:com.cisco.oss.foundation.directory.utils.HttpUtils.java
/** * Invoke REST Service using POST method. * * @param urlStr//from w w w . j a v a 2 s . c o m * the REST service URL String * @param body * the Http Body String. * @return * the HttpResponse. * @throws IOException */ public static HttpResponse postJson(String urlStr, String body) throws IOException { URL url = new URL(urlStr); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.addRequestProperty("Accept", "application/json"); urlConnection.setRequestMethod("POST"); urlConnection.addRequestProperty("Content-Type", "application/json"); urlConnection.addRequestProperty("Content-Length", Integer.toString(body.length())); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.setUseCaches(false); OutputStream out = urlConnection.getOutputStream(); out.write(body.getBytes()); ByteStreams.copy(new ByteArrayInputStream(body.getBytes()), out); BufferedReader in = null; try { int errorCode = urlConnection.getResponseCode(); if ((errorCode <= 202) && (errorCode >= 200)) { in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); } else { InputStream error = urlConnection.getErrorStream(); if (error != null) { in = new BufferedReader(new InputStreamReader(error)); } } String json = null; if (in != null) { json = CharStreams.toString(in); } return new HttpResponse(errorCode, json); } finally { if (in != null) { in.close(); } } }
From source file:org.prebake.service.tools.CpProcess.java
public byte run(Path workingDir, String... argv) throws IOException { for (int i = 0, n = argv.length; i < n; i += 2) { Path src = workingDir.resolve(argv[i]); Path tgt = workingDir.resolve(argv[i + 1]); InputStream in = src.newInputStream(); try {//from ww w. java2 s. c om // Fail if the target already exists. OutputStream out = tgt.newOutputStream(StandardOpenOption.CREATE_NEW); try { ByteStreams.copy(in, out); } finally { out.close(); } } finally { in.close(); } } return 0; }
From source file:com.example.appengine.xmpp.ErrorServlet.java
@Override public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { // Parse the POST data, which is sent as a MIME stream containing the stanza. ByteArrayOutputStream baos = new ByteArrayOutputStream(); ServletInputStream inputStream = req.getInputStream(); ByteStreams.copy(inputStream, baos); // Log the error log.warning("Error stanza received: " + baos.toString()); }
From source file:ninja.uploads.MemoryFileItemProvider.java
@Override public FileItem create(FileItemStream item) { // build output stream to get bytes ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // do copy//from w ww . j av a2 s. c om try { ByteStreams.copy(item.openStream(), outputStream); } catch (IOException e) { throw new RuntimeException("Failed to create temporary uploaded file in memory", e); } // return final String name = item.getName(); final byte[] bytes = outputStream.toByteArray(); final String contentType = item.getContentType(); final FileItemHeaders headers = item.getHeaders(); return new FileItem() { @Override public String getFileName() { return name; } @Override public InputStream getInputStream() { return new ByteArrayInputStream(bytes); } @Override public File getFile() { throw new UnsupportedOperationException("Not supported in MemoryFileProvider"); } @Override public String getContentType() { return contentType; } @Override public FileItemHeaders getHeaders() { return headers; } @Override public void cleanup() { } }; }
From source file:com.urswolfer.gerrit.client.rest.http.LoginSimulationServlet.java
/** * Only handle case when no "Authorization" header is sent. When "Authorization" header is sent, * leave it to GerritRestClientTest#basicAuth. *//*from w w w . j av a 2 s . c o m*/ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getHeader("Authorization") != null) { return; } resp.addCookie(new Cookie("GerritAccount", "value")); ByteStreams.copy( new FileInputStream("src/test/resources/com/urswolfer/gerrit/client/rest/http/login/index.html"), resp.getOutputStream()); }
From source file:com.google.devtools.build.lib.remote.blobstore.OnDiskBlobStore.java
@Override public boolean get(String key, OutputStream out) throws IOException { Path f = toPath(key);/* ww w. ja v a 2 s.c o m*/ if (!f.exists()) { return false; } try (InputStream in = f.getInputStream()) { ByteStreams.copy(in, out); } return true; }
From source file:com.sonatype.nexus.repository.nuget.internal.odata.NugetPackageUtils.java
/** * Determine the metadata for a nuget package. * - nuspec data (comes from .nuspec)//from w ww . j a v a 2 s. c o m * - size (package size) * - hash(es) (package hash sha-512) */ public static Map<String, String> packageMetadata(final InputStream inputStream) throws IOException, NugetPackageException { try (MultiHashingInputStream hashingStream = new MultiHashingInputStream( Arrays.asList(HashAlgorithm.SHA512), inputStream)) { final byte[] nuspec = extractNuspec(hashingStream); Map<String, String> metadata = NuspecSplicer.extractNuspecData(new ByteArrayInputStream(nuspec)); ByteStreams.copy(hashingStream, ByteStreams.nullOutputStream()); metadata.put(PACKAGE_SIZE, String.valueOf(hashingStream.count())); HashCode code = hashingStream.hashes().get(HashAlgorithm.SHA512); metadata.put(PACKAGE_HASH, new String(Base64.encodeBase64(code.asBytes()), Charsets.UTF_8)); metadata.put(PACKAGE_HASH_ALGORITHM, "SHA512"); return metadata; } catch (XmlPullParserException e) { throw new NugetPackageException("Unable to read .nuspec from package stream", e); } }
From source file:com.google.devtools.build.android.dexer.DexFileArchive.java
/** * Copies the content of the given {@link InputStream} into an entry with the given details. *///from w w w.j av a2 s.c om public DexFileArchive copy(ZipEntry entry, InputStream in) throws IOException { out.putNextEntry(entry); ByteStreams.copy(in, out); out.closeEntry(); return this; }
From source file:org.terasology.audio.formats.OggSoundFormat.java
@Override public StaticSoundData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException { try (OggReader reader = new OggReader(inputs.get(0).openStream())) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteStreams.copy(reader, bos); ByteBuffer data = BufferUtils.createByteBuffer(bos.size()).put(bos.toByteArray()); data.flip();//from w w w. ja v a 2 s . co m return new StaticSoundData(data, reader.getChannels(), reader.getRate(), 16); } catch (IOException e) { throw new IOException("Failed to load sound: " + e.getMessage(), e); } }
From source file:org.cirdles.squid.utilities.FileUtilities.java
public static void unpackZipFile(final File archive, final File targetDirectory) throws ZipException, IOException { ZipFile zipFile = new ZipFile(archive); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry zipEntry = entries.nextElement(); if (zipEntry.isDirectory()) { continue; }//from w w w . j a v a2 s. co m final File targetFile = new File(targetDirectory, zipEntry.getName()); com.google.common.io.Files.createParentDirs(targetFile); ByteStreams.copy(zipFile.getInputStream(zipEntry), com.google.common.io.Files.asByteSink(targetFile, FileWriteMode.APPEND).openStream()); } }