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.kimios.client.controller.FileTransferController.java
public void downloadFileVersion(String sessionId, long documentVersionId, OutputStream os, boolean isCompressed) throws Exception, DMSException, ConfigException, AccessDeniedException { try {//from ww w .j a va2 s. c om DataTransaction transac = client.startDownloadTransaction(sessionId, documentVersionId, isCompressed); if (restMode) { if (!isCompressed) { IOUtils.copyLarge(client.downloadDocumentVersion(sessionId, transac.getUid()), os); try { os.flush(); os.close(); } catch (Exception e) { } } else { String tmpFileName = ""; OutputStream tmp; tmpFileName = FileCompressionHelper.getTempFilePath(temporaryFilesPath, "dl" + sessionId); tmp = new BufferedOutputStream(new FileOutputStream(temporaryFilesPath + tmpFileName)); IOUtils.copyLarge(client.downloadDocumentVersion(sessionId, transac.getUid()), tmp); InputStream inFull = new FileInputStream(temporaryFilesPath + tmpFileName); IOUtils.copyLarge(inFull, os); } } else { int bufferSize = chunkSize; String tmpFileName = ""; OutputStream tmp; if (isCompressed) { tmpFileName = FileCompressionHelper.getTempFilePath(temporaryFilesPath, "dl" + sessionId); tmp = new BufferedOutputStream(new FileOutputStream(temporaryFilesPath + tmpFileName)); } else { tmp = os; } // Read Stream from server... long offset = 0; InputStream in; int readBytes; // Read packets ... while (offset < transac.getSize()) { if (offset + bufferSize > transac.getSize()) { bufferSize = new Long(transac.getSize() - offset).intValue(); } byte[] t = client.getChunck(sessionId, transac.getUid(), offset, bufferSize); try { tmp.write(t, 0, t.length); } catch (Exception e) { e.printStackTrace(); } offset += bufferSize; } tmp.flush(); tmp.close(); if (isCompressed) { InputStream inFull = new FileInputStream(temporaryFilesPath + tmpFileName); InputStream toSend = null; // Uncompress toSend = new ZipInputStream(inFull); if (((ZipInputStream) toSend).getNextEntry() == null) { throw new Exception("Zip error"); } int d = -1; while ((d = toSend.read()) != -1 && toSend.available() > 0) { os.write(d); } os.flush(); os.close(); } } } catch (Exception e) { throw new ExceptionHelper().convertException(e); } }
From source file:org.lilie.services.eliot.tice.jackrabbit.core.data.version_2_4_0.FileDataStore.java
/** * Creates a new data record./*from w ww . j a va 2s.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); 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(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.lilyproject.rest.BlobByVersionAndFieldResource.java
protected static Response getBlob(String id, String version, String fieldName, UriInfo uriInfo, final Repository repository) { RecordId recordId = repository.getIdGenerator().fromString(id); QName fieldQName = ResourceClassUtil.parseQName(fieldName, uriInfo.getQueryParameters()); Long versionNr = null;/* ww w . j a v a 2s . c o m*/ if (version != null) { versionNr = Long.parseLong(version); } Record record; try { record = repository.read(recordId, versionNr, Collections.singletonList(fieldQName)); if (!record.hasField(fieldQName)) { throw new ResourceException("Record " + id + " has no field " + fieldQName, NOT_FOUND.getStatusCode()); } Object value = record.getField(fieldQName); if (!(value instanceof Blob)) { throw new ResourceException( "Specified field is not a blob field. Record " + id + ", field " + fieldQName, BAD_REQUEST.getStatusCode()); } final Blob blob = (Blob) value; StreamingOutput output = new StreamingOutput() { public void write(OutputStream output) throws IOException, WebApplicationException { InputStream is = null; try { is = repository.getInputStream(blob); IOUtils.copyLarge(is, output); } catch (BlobNotFoundException e) { throw new ResourceException(e, NOT_FOUND.getStatusCode()); } catch (Exception e) { throw new ResourceException(e, INTERNAL_SERVER_ERROR.getStatusCode()); } finally { Closer.close(is); } } }; return Response.ok(output, MediaType.valueOf(blob.getMediaType())).build(); } catch (RecordNotFoundException e) { throw new ResourceException(e, NOT_FOUND.getStatusCode()); } catch (Exception e) { throw new ResourceException("Error loading record.", e, INTERNAL_SERVER_ERROR.getStatusCode()); } }
From source file:org.lilyproject.rest.BlobCollectionResource.java
@POST @Consumes("*/*")/*from www. j av a 2s. c o m*/ @Produces("application/json") public Response post(@Context HttpHeaders headers, @Context UriInfo uriInfo, InputStream is) { String lengthHeader = headers.getRequestHeaders().getFirst(HttpHeaders.CONTENT_LENGTH); if (lengthHeader == null) { throw new ResourceException("Content-Length header is required for uploading blobs.", BAD_REQUEST.getStatusCode()); } // TODO do we want the mediatype to include the parameters? String mediaType = headers.getMediaType().getType() + "/" + headers.getMediaType().getSubtype(); long length = Long.parseLong(lengthHeader); Blob blob = new Blob(mediaType, length, null); if (length == 0 && is == null) { // Apparently when the length is 0, no InputStream is provided, therefore the following is = new NullInputStream(0); } OutputStream os = null; try { os = getTable(uriInfo).getOutputStream(blob); IOUtils.copyLarge(is, os); } catch (Exception e) { throw new ResourceException("Error writing blob.", e, INTERNAL_SERVER_ERROR.getStatusCode()); } finally { Closer.close(os); } return Response.ok().entity(blob).build(); }
From source file:org.lilyproject.rest.providers.BlobAccessBodyWriter.java
@Override public void writeTo(BlobAccess blobAccess, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { InputStream is = null;/*from w w w . j a v a2 s.c o m*/ try { is = blobAccess.getInputStream(); IOUtils.copyLarge(blobAccess.getInputStream(), entityStream); } catch (BlobException e) { throw new IOException("Error reading blob.", e); } finally { Closer.close(is); } }
From source file:org.marketcetera.util.exec.InputThread.java
@Override public void run() { CloseableRegistry r = new CloseableRegistry(); r.register(mIn);// w ww . j a v a 2 s . com if (mCloseOut) { r.register(mOut); } try { IOUtils.copyLarge(mIn, mOut); } catch (IOException ex) { Messages.CANNOT_COPY_OUTPUT.error(this, ex, mCommand); } finally { r.close(); } }
From source file:org.marketcetera.util.file.CopyBytesUtils.java
/** * Copies a byte stream from the given source to the given sink. * * @param in The byte source, as interpreted by {@link * InputStreamWrapper#InputStreamWrapper(InputStream,boolean)}. * @param inSkipClose True if the source stream should not be * closed.//from w ww .j a v a 2 s . co m * @param out The byte sink, as interpreted by {@link * OutputStreamWrapper#OutputStreamWrapper(OutputStream,boolean)}. * @param outSkipClose True if the sink stream should not be * closed. * * @return The number of bytes copied. * * @throws I18NException Thrown if there is a data read/write * error. */ public static long copy(InputStream in, boolean inSkipClose, OutputStream out, boolean outSkipClose) throws I18NException { CloseableRegistry registry = new CloseableRegistry(); try { InputStreamWrapper inW = new InputStreamWrapper(in, inSkipClose); registry.register(inW); OutputStreamWrapper outW = new OutputStreamWrapper(out, outSkipClose); registry.register(outW); return IOUtils.copyLarge(inW.getStream(), outW.getStream()); } catch (IOException ex) { throw ExceptUtils.wrap(ex, Messages.CANNOT_COPY_STREAMS); } finally { registry.close(); } }
From source file:org.marketcetera.util.file.CopyBytesUtils.java
/** * Copies a byte stream from one given location to another. * * @param in The name of the byte source, as interpreted by {@link * InputStreamWrapper#InputStreamWrapper(String)}. * @param out The name of the byte sink, as interpreted by {@link * OutputStreamWrapper#OutputStreamWrapper(String)}. * * @return The number of bytes copied./*from w ww . ja v a2s . co m*/ * * @throws I18NException Thrown if there is a data read/write * error. */ public static long copy(String in, String out) throws I18NException { CloseableRegistry registry = new CloseableRegistry(); try { InputStreamWrapper inW = new InputStreamWrapper(in); registry.register(inW); OutputStreamWrapper outW = new OutputStreamWrapper(out); registry.register(outW); return IOUtils.copyLarge(inW.getStream(), outW.getStream()); } catch (IOException ex) { throw ExceptUtils.wrap(ex, new I18NBoundMessage2P(Messages.CANNOT_COPY_FILES, in, out)); } finally { registry.close(); } }
From source file:org.marketcetera.util.file.CopyBytesUtils.java
/** * Copies a byte stream from the given source to the given * location.// w w w . ja va 2s . c o m * * @param in The byte source, as interpreted by {@link * InputStreamWrapper#InputStreamWrapper(InputStream,boolean)}. * @param skipClose True if the source stream should not be * closed. * @param out The name of the byte sink, as interpreted by {@link * OutputStreamWrapper#OutputStreamWrapper(String)}. * * @return The number of bytes copied. * * @throws I18NException Thrown if there is a data read/write * error. */ public static long copy(InputStream in, boolean skipClose, String out) throws I18NException { CloseableRegistry registry = new CloseableRegistry(); try { InputStreamWrapper inW = new InputStreamWrapper(in, skipClose); registry.register(inW); OutputStreamWrapper outW = new OutputStreamWrapper(out); registry.register(outW); return IOUtils.copyLarge(inW.getStream(), outW.getStream()); } catch (IOException ex) { throw ExceptUtils.wrap(ex, new I18NBoundMessage1P(Messages.CANNOT_COPY_ISTREAM, out)); } finally { registry.close(); } }
From source file:org.marketcetera.util.file.CopyBytesUtils.java
/** * Copies a byte stream from the given location to the given sink. * * @param in The name of the byte source, as interpreted by {@link * InputStreamWrapper#InputStreamWrapper(String)}. * @param out The byte sink, as interpreted by {@link * OutputStreamWrapper#OutputStreamWrapper(OutputStream,boolean)}. * @param skipClose True if the sink stream should not be closed. * * @return The number of bytes copied.// w w w. j a v a2 s. com * * @throws I18NException Thrown if there is a data read/write * error. */ public static long copy(String in, OutputStream out, boolean skipClose) throws I18NException { CloseableRegistry registry = new CloseableRegistry(); try { InputStreamWrapper inW = new InputStreamWrapper(in); registry.register(inW); OutputStreamWrapper outW = new OutputStreamWrapper(out, skipClose); registry.register(outW); return IOUtils.copyLarge(inW.getStream(), outW.getStream()); } catch (IOException ex) { throw ExceptUtils.wrap(ex, new I18NBoundMessage1P(Messages.CANNOT_COPY_OSTREAM, in)); } finally { registry.close(); } }