List of usage examples for org.apache.commons.io IOUtils copyLarge
public static long copyLarge(Reader input, Writer output) throws IOException
Reader
to a Writer
. From source file:org.apache.hive.ptest.api.client.PTestClient.java
private void downloadTestResults(String testHandle, String testOutputDir) throws Exception { HttpGet request = new HttpGet(mLogsEndpoint + testHandle + "/test-results.tar.gz"); FileOutputStream output = null; try {// ww w.ja va 2 s. co m HttpResponse httpResponse = mHttpClient.execute(request); StatusLine statusLine = httpResponse.getStatusLine(); if (statusLine.getStatusCode() != 200) { throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase()); } output = new FileOutputStream(new File(testOutputDir, "test-results.tar.gz")); IOUtils.copyLarge(httpResponse.getEntity().getContent(), output); output.flush(); } finally { request.abort(); if (output != null) { output.close(); } } }
From source file:org.apache.hyracks.control.cc.web.ApplicationInstallationHandler.java
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try {//w w w . j a v a 2s . com while (target.startsWith("/")) { target = target.substring(1); } while (target.endsWith("/")) { target = target.substring(0, target.length() - 1); } String[] parts = target.split("/"); if (parts.length != 1) { return; } final String[] params = parts[0].split("&"); String deployIdString = params[0]; String rootDir = ccs.getServerContext().getBaseDir().toString(); final String deploymentDir = rootDir.endsWith(File.separator) ? rootDir + "applications/" + deployIdString : rootDir + File.separator + "/applications/" + File.separator + deployIdString; switch (HttpMethod.valueOf(request.getMethod())) { case PUT: { class OutputStreamGetter extends SynchronizableWork { private OutputStream os; @Override protected void doRun() throws Exception { FileUtils.forceMkdir(new File(deploymentDir)); String fileName = params[1]; File jarFile = new File(deploymentDir, fileName); os = new FileOutputStream(jarFile); } } OutputStreamGetter r = new OutputStreamGetter(); try { ccs.getWorkQueue().scheduleAndSync(r); } catch (Exception e) { throw new IOException(e); } try { IOUtils.copyLarge(request.getInputStream(), r.os); } finally { r.os.close(); } break; } case GET: { class InputStreamGetter extends SynchronizableWork { private InputStream is; @Override protected void doRun() throws Exception { String fileName = params[1]; File jarFile = new File(deploymentDir, fileName); is = new FileInputStream(jarFile); } } InputStreamGetter r = new InputStreamGetter(); try { ccs.getWorkQueue().scheduleAndSync(r); } catch (Exception e) { throw new IOException(e); } if (r.is == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } else { response.setContentType("application/octet-stream"); response.setStatus(HttpServletResponse.SC_OK); try { IOUtils.copyLarge(r.is, response.getOutputStream()); } finally { r.is.close(); } } break; } default: throw new IllegalArgumentException(request.getMethod()); } baseRequest.setHandled(true); } catch (IOException e) { e.printStackTrace(); throw e; } }
From source file:org.apache.jackrabbit.core.data.FileDataStore.java
/** * Creates a new data record./*from w w w . j ava2 s. co m*/ * The stream is first consumed and the contents are saved in a temporary file * and the SHA-1 message digest of the stream is calculated. If a * record with the same SHA-1 digest (and length) is found then it is * returned. Otherwise the temporary file is moved in place to become * the new data record that gets returned. * * @param input binary stream * @return data record that contains the given stream * @throws DataStoreException if the record could not be created */ public DataRecord addRecord(InputStream input) throws DataStoreException { File temporary = null; try { temporary = newTemporaryFile(); DataIdentifier tempId = new DataIdentifier(temporary.getName()); usesIdentifier(tempId); // Copy the stream to the temporary file and calculate the // stream length and the message digest of the stream long length = 0; MessageDigest digest = MessageDigest.getInstance(DIGEST); OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest); try { length = IOUtils.copyLarge(input, output); } finally { output.close(); } DataIdentifier identifier = new DataIdentifier(digest.digest()); File file; synchronized (this) { // Check if the same record already exists, or // move the temporary file in place if needed usesIdentifier(identifier); file = getFile(identifier); File parent = file.getParentFile(); if (!parent.isDirectory()) { parent.mkdirs(); } if (!file.exists()) { temporary.renameTo(file); if (!file.exists()) { throw new IOException("Can not rename " + temporary.getAbsolutePath() + " to " + file.getAbsolutePath() + " (media read only?)"); } } else { long now = System.currentTimeMillis(); if (file.lastModified() < now) { file.setLastModified(now); } } // Sanity checks on the record file. These should never fail, // but better safe than sorry... if (!file.isFile()) { throw new IOException("Not a file: " + file); } if (file.length() != length) { throw new IOException(DIGEST + " collision: " + file); } } // this will also make sure that // tempId is not garbage collected until here inUse.remove(tempId); return new FileDataRecord(identifier, file); } catch (NoSuchAlgorithmException e) { throw new DataStoreException(DIGEST + " not available", e); } catch (IOException e) { throw new DataStoreException("Could not add record", e); } finally { if (temporary != null) { temporary.delete(); } } }
From source file:org.apache.jackrabbit.core.data.CachingDataStore.java
/** * Creates a new data record in {@link Backend}. The stream is first * consumed and the contents are saved in a temporary file and the SHA-1 * message digest of the stream is calculated. If a record with the same * SHA-1 digest (and length) is found then it is returned. Otherwise new * record is created in {@link Backend} and the temporary file is moved in * place to {@link LocalCache}.//from w w w. ja va2 s . com * * @param input * binary stream * @return {@link CachingDataRecord} * @throws DataStoreException * if the record could not be created. */ @Override public DataRecord addRecord(InputStream input) throws DataStoreException { File temporary = null; long startTime = System.currentTimeMillis(); long length = 0; try { temporary = newTemporaryFile(); DataIdentifier tempId = new DataIdentifier(temporary.getName()); usesIdentifier(tempId); // Copy the stream to the temporary file and calculate the // stream length and the message digest of the stream MessageDigest digest = MessageDigest.getInstance(DIGEST); OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest); try { length = IOUtils.copyLarge(input, output); } finally { output.close(); } long currTime = System.currentTimeMillis(); DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest())); LOG.debug("SHA1 of [{}], length =[{}] took [{}]ms ", new Object[] { identifier, length, (currTime - startTime) }); String fileName = getFileName(identifier); AsyncUploadCacheResult result = null; synchronized (this) { usesIdentifier(identifier); // check if async upload is already in progress if (!asyncWriteCache.hasEntry(fileName, true)) { result = cache.store(fileName, temporary, true); } } LOG.debug("storing [{}] in localCache took [{}] ms", identifier, (System.currentTimeMillis() - currTime)); if (result != null) { if (result.canAsyncUpload()) { backend.writeAsync(identifier, result.getFile(), this); } else { backend.write(identifier, result.getFile()); } } // this will also make sure that // tempId is not garbage collected until here inUse.remove(tempId); LOG.debug("addRecord [{}] of length [{}] took [{}]ms.", new Object[] { identifier, length, (System.currentTimeMillis() - startTime) }); return new CachingDataRecord(this, identifier); } catch (NoSuchAlgorithmException e) { throw new DataStoreException(DIGEST + " not available", e); } catch (IOException e) { throw new DataStoreException("Could not add record", e); } finally { if (temporary != null) { // try to delete - but it's not a big deal if we can't temporary.delete(); } } }
From source file:org.apache.jackrabbit.core.data.FileDataStore.java
/** * Creates a new data record./*from w ww. j av a2 s .co m*/ * The stream is first consumed and the contents are saved in a temporary file * and the SHA-1 message digest of the stream is calculated. If a * record with the same SHA-1 digest (and length) is found then it is * returned. Otherwise the temporary file is moved in place to become * the new data record that gets returned. * * @param input binary stream * @return data record that contains the given stream * @throws DataStoreException if the record could not be created */ public DataRecord addRecord(InputStream input) throws DataStoreException { File temporary = null; try { temporary = newTemporaryFile(); DataIdentifier tempId = new DataIdentifier(temporary.getName()); usesIdentifier(tempId); // Copy the stream to the temporary file and calculate the // stream length and the message digest of the stream long length = 0; MessageDigest digest = MessageDigest.getInstance(DIGEST); OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest); try { length = IOUtils.copyLarge(input, output); } finally { output.close(); } DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest())); File file; synchronized (this) { // Check if the same record already exists, or // move the temporary file in place if needed usesIdentifier(identifier); file = getFile(identifier); if (!file.exists()) { File parent = file.getParentFile(); parent.mkdirs(); if (temporary.renameTo(file)) { // no longer need to delete the temporary file temporary = null; } else { throw new IOException("Can not rename " + temporary.getAbsolutePath() + " to " + file.getAbsolutePath() + " (media read only?)"); } } else { long now = System.currentTimeMillis(); if (getLastModified(file) < now + ACCESS_TIME_RESOLUTION) { setLastModified(file, now + ACCESS_TIME_RESOLUTION); } } if (file.length() != length) { // Sanity checks on the record file. These should never fail, // but better safe than sorry... if (!file.isFile()) { throw new IOException("Not a file: " + file); } throw new IOException(DIGEST + " collision: " + file); } } // this will also make sure that // tempId is not garbage collected until here inUse.remove(tempId); return new FileDataRecord(this, identifier, file); } catch (NoSuchAlgorithmException e) { throw new DataStoreException(DIGEST + " not available", e); } catch (IOException e) { throw new DataStoreException("Could not add record", e); } finally { if (temporary != null) { temporary.delete(); } } }
From source file:org.apache.jackrabbit.core.data.LocalCache.java
/** * Store an item in the cache and return the input stream. If cache is in * purgeMode or file doesn't exists, inputstream from a * {@link TransientFileFactory#createTransientFile(String, String, File)} is * returned. Otherwise inputStream from cached file is returned. This method * doesn't close the incoming inputstream. * //w w w . j a va 2 s . co m * @param fileName the key of cache. * @param in {@link InputStream} * @return the (new) input stream. */ public InputStream store(String fileName, final InputStream in) throws IOException { fileName = fileName.replace("\\", "/"); File f = getFile(fileName); long length = 0; synchronized (this) { if (!f.exists() || isInPurgeMode()) { OutputStream out = null; File transFile = null; try { TransientFileFactory tff = TransientFileFactory.getInstance(); transFile = tff.createTransientFile("s3-", "tmp", tmp); out = new BufferedOutputStream(new FileOutputStream(transFile)); length = IOUtils.copyLarge(in, out); } finally { IOUtils.closeQuietly(out); } // rename the file to local fs cache if (canAdmitFile(length) && (f.getParentFile().exists() || f.getParentFile().mkdirs()) && transFile.renameTo(f) && f.exists()) { if (transFile.exists() && transFile.delete()) { LOG.info("tmp file [{}] not deleted successfully", transFile.getAbsolutePath()); } transFile = null; LOG.debug("file [{}] added to local cache.", fileName); cache.put(fileName, f.length()); } else { f = transFile; } } else { // f.exists and not in purge mode f.setLastModified(System.currentTimeMillis()); cache.put(fileName, f.length()); } cache.tryPurge(); return new LazyFileInputStream(f); } }
From source file:org.apache.jackrabbit.core.value.BLOBInTempFile.java
/** * Creates a new instance from a stream. * The input stream is always closed by this method. * * @param in the input stream//from ww w .ja va 2s .c o m * @param temp * @throws RepositoryException */ private BLOBInTempFile(InputStream in, boolean temp) throws RepositoryException { this.temp = temp; OutputStream out = null; try { TransientFileFactory fileFactory = TransientFileFactory.getInstance(); file = fileFactory.createTransientFile("bin", null, null); out = new FileOutputStream(file); length = IOUtils.copyLarge(in, out); } catch (IOException e) { throw new RepositoryException("Error creating temporary file", e); } finally { IOUtils.closeQuietly(in); if (out != null) { try { out.close(); } catch (IOException e) { throw new RepositoryException("Error creating temporary file", e); } } } }
From source file:org.apache.jackrabbit.oak.plugins.blob.datastore.OakFileDataStore.java
@Override public void addMetadataRecord(InputStream input, String name) throws DataStoreException { try {/*from w ww. j a va 2 s. c o m*/ File file = new File(getPath(), name); FileOutputStream os = new FileOutputStream(file); try { IOUtils.copyLarge(input, os); } finally { Closeables.close(os, true); Closeables.close(input, true); } } catch (IOException e) { LOG.error("Exception while adding root record with name {}, {}", new Object[] { name, e }); throw new DataStoreException("Could not add root record", e); } }
From source file:org.apache.jmeter.protocol.system.StreamCopier.java
/** * @see java.lang.Thread#run()// ww w . ja v a2s . c o m */ @Override public void run() { try { IOUtils.copyLarge(is, os); os.close(); is.close(); } catch (IOException e) { log.warn("Error writing stream", e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
From source file:org.apache.jorphan.exec.StreamCopier.java
/** * @see java.lang.Thread#run()/*ww w . ja v a 2 s .c o m*/ */ @Override public void run() { final boolean isSystemOutput = os.equals(System.out) || os.equals(System.err); try { IOUtils.copyLarge(is, os); if (!isSystemOutput) { os.close(); } is.close(); } catch (IOException e) { log.warn("Error writing stream", e); } finally { IOUtils.closeQuietly(is); if (!isSystemOutput) { IOUtils.closeQuietly(os); } } }