List of usage examples for org.apache.commons.io.output CountingOutputStream close
public void close() throws IOException
close() method. From source file:com.datatorrent.contrib.hdht.MockFileAccess.java
@Override public FileWriter getWriter(final long bucketKey, final String fileName) throws IOException { final DataOutputStream dos = getOutputStream(bucketKey, fileName); final CountingOutputStream cos = new CountingOutputStream(dos); final Output out = new Output(cos); return new FileWriter() { @Override/*from w w w . j a va 2 s .c o m*/ public void close() throws IOException { out.close(); cos.close(); dos.close(); } @Override public void append(byte[] key, byte[] value) throws IOException { kryo.writeObject(out, key); kryo.writeObject(out, value); } @Override public long getBytesWritten() { return cos.getCount() + out.position(); } }; }
From source file:com.yenlo.synapse.transport.vfs.VFSTransportSender.java
private void populateResponseFile(FileObject responseFile, MessageContext msgContext, boolean append, boolean lockingEnabled) throws AxisFault { MessageFormatter messageFormatter = getMessageFormatter(msgContext); OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext); try {/*from ww w .j a v a 2 s . co m*/ CountingOutputStream os = new CountingOutputStream(responseFile.getContent().getOutputStream(append)); try { messageFormatter.writeTo(msgContext, format, os, true); } finally { os.close(); } // update metrics metrics.incrementMessagesSent(msgContext); metrics.incrementBytesSent(msgContext, os.getByteCount()); } catch (FileSystemException e) { if (lockingEnabled) { VFSUtils.releaseLock(fsManager, responseFile); } metrics.incrementFaultsSending(); handleException("IO Error while creating response file : " + responseFile.getName(), e); } catch (IOException e) { if (lockingEnabled) { VFSUtils.releaseLock(fsManager, responseFile); } metrics.incrementFaultsSending(); handleException("IO Error while creating response file : " + responseFile.getName(), e); } }
From source file:com.orangeandbronze.jblubble.jdbc.JdbcBlobstoreService.java
@Override public BlobKey createBlob(BlobstoreWriteCallback callback, String name, String contentType) throws IOException, BlobstoreException { try {/* w w w . j a va2 s .c o m*/ try (Connection connection = dataSource.getConnection(); PreparedStatement ps = connection.prepareStatement(getInsertSql(), Statement.RETURN_GENERATED_KEYS);) { ps.setString(1, name); ps.setString(2, contentType); Blob content = connection.createBlob(); try { long size; String md5Hash = null; OutputStream out = new BufferedOutputStream(content.setBinaryStream(1L), getBufferSize()); try { CountingOutputStream countingOutputStream = new CountingOutputStream(out); try { MessageDigest md5; try { md5 = MessageDigest.getInstance(MD5_ALGORITHM_NAME); try (DigestOutputStream digestOutputStream = new DigestOutputStream( countingOutputStream, md5)) { size = callback.writeToOutputStream(digestOutputStream); if (size == -1L) { size = countingOutputStream.getByteCount(); } md5Hash = new String(encodeHex(md5.digest())); } } catch (NoSuchAlgorithmException e) { throw new BlobstoreException(e); } } finally { countingOutputStream.close(); } } finally { out.close(); } ps.setBlob(3, content); ps.setLong(4, size); ps.setTimestamp(5, new java.sql.Timestamp(new java.util.Date().getTime())); ps.setString(6, md5Hash); int rowCount = ps.executeUpdate(); if (rowCount == 0) { throw new BlobstoreException("Creating blob failed, no rows created."); } long generatedId = getGeneratedKey(ps); return new BlobKey(String.valueOf(generatedId)); } finally { content.free(); } } } catch (SQLException e) { throw new BlobstoreException("Error when creating blob", e); } }
From source file:deincraftlauncher.IO.download.FTPDownloader.java
@Override public void start() { if (!prepared) { prepare();/*w ww . j av a 2 s.co m*/ } if (preparing) { try { prepareThread.join(); } catch (InterruptedException ex) { Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex); } } if (started) { return; } started = true; System.out.println("starting ftp download: " + fileName); finished = false; updateNum = 0; downloadThread = new Thread() { @Override public void run() { System.out.println("FTPDownload Thread started"); File targetPath = new File(saveTo); if (!targetPath.exists()) { targetPath.mkdirs(); } saveTo += fileName; System.out.println("Starting Download; Link=" + link + " saveTo=" + saveTo); //Actual download code try { OutputStream output = new FileOutputStream(saveTo); CountingOutputStream cos = new CountingOutputStream(output) { boolean updateReady = true; @Override protected void beforeWrite(int n) { super.beforeWrite(n); if (!updateReady) { return; } totalProgress = this.getCount() / totalSize; Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { updateReady = true; } }, updateDelay); updateReady = false; } }; update(); FTPConnection.getClient().retrieveFile(ftpfile.getName(), cos); cos.close(); output.close(); onFinished.call(instance); onFinishedB.call(instance); finished = true; System.out.println("Download fertig"); started = false; //download fertig downloadThread.interrupt(); } catch (IOException ex) { System.err.println("error while downliading via ftp: " + ex); } } }; downloadThread.start(); started = false; }
From source file:com.orangeandbronze.jblubble.jdbc.springframework.SpringJdbcBlobstoreService.java
@Override public BlobKey createBlob(BlobstoreWriteCallback callback, String name, String contentType) throws IOException, BlobstoreException { try {//w w w . ja v a 2 s . c om return jdbcTemplate.execute(new ConnectionCallback<BlobKey>() { @Override public BlobKey doInConnection(Connection connection) throws SQLException, DataAccessException { try (PreparedStatement ps = connection.prepareStatement(getInsertSql(), Statement.RETURN_GENERATED_KEYS)) { ps.setString(1, name); ps.setString(2, contentType); Blob content = connection.createBlob(); try { long size; String md5Hash = null; OutputStream out = new BufferedOutputStream(content.setBinaryStream(1L), getBufferSize()); try { CountingOutputStream countingOutputStream = new CountingOutputStream(out); try { MessageDigest md5; try { md5 = MessageDigest.getInstance(MD5_ALGORITHM_NAME); try (DigestOutputStream digestOutputStream = new DigestOutputStream( countingOutputStream, md5)) { size = callback.writeToOutputStream(digestOutputStream); if (size == -1L) { size = countingOutputStream.getByteCount(); } md5Hash = new String(encodeHex(md5.digest())); } } catch (NoSuchAlgorithmException e) { throw new BlobstoreException(e); } } finally { countingOutputStream.close(); } } finally { out.close(); } ps.setBlob(3, content); ps.setLong(4, size); ps.setTimestamp(5, new java.sql.Timestamp(new java.util.Date().getTime())); ps.setString(6, md5Hash); int rowCount = ps.executeUpdate(); if (rowCount == 0) { throw new BlobstoreException("Creating blob failed, no rows created."); } long generatedId = getGeneratedKey(ps); return new BlobKey(String.valueOf(generatedId)); } finally { content.free(); } } catch (IOException e) { throw new BlobstoreException("Error when creating blob", e); } } }); } catch (DataAccessException e) { throw new BlobstoreException(e); } }
From source file:com.orangeandbronze.jblubble.jdbc.PgJdbcBlobstoreService.java
@Override public BlobKey createBlob(BlobstoreWriteCallback callback, String name, String contentType) throws IOException, BlobstoreException { boolean resetCommitMode = false; try (Connection connection = dataSource.getConnection()) { if (connection.getAutoCommit()) { connection.setAutoCommit(false); resetCommitMode = true;// w w w . java2s .com } try { int rowCount; try (PreparedStatement ps = connection.prepareStatement(getInsertSql(), Statement.RETURN_GENERATED_KEYS)) { ps.setString(1, name); ps.setString(2, contentType); ps.setTimestamp(3, new java.sql.Timestamp(new java.util.Date().getTime())); rowCount = ps.executeUpdate(); if (rowCount == 0) { throw new BlobstoreException("Creating blob failed, no rows created."); } final long generatedId = getGeneratedKey(ps); long size; String md5Hash = null; try (PreparedStatement ps2 = connection.prepareStatement(getSelectContentByIdSql())) { ps2.setLong(1, generatedId); ResultSet rs = ps2.executeQuery(); if (!rs.next()) { throw new BlobstoreException("Creating blob failed, no rows created."); } Blob contentBlob = rs.getBlob(1); try { OutputStream out = new BufferedOutputStream(contentBlob.setBinaryStream(1L), getBufferSize()); try { CountingOutputStream countingOutputStream = new CountingOutputStream(out); try { MessageDigest md5; try { md5 = MessageDigest.getInstance(MD5_ALGORITHM_NAME); try (DigestOutputStream digestOutputStream = new DigestOutputStream( countingOutputStream, md5)) { size = callback.writeToOutputStream(digestOutputStream); if (size == -1L) { size = countingOutputStream.getByteCount(); } md5Hash = new String(encodeHex(md5.digest())); } } catch (NoSuchAlgorithmException e) { throw new BlobstoreException(e); } } finally { try { countingOutputStream.close(); } catch (IOException e) { // Since digestOutputStream gets closed, // the wrapped countingOutputStream does // not really need to get closed again. } } } finally { try { out.close(); } catch (IOException e) { // Since digestOutputStream gets closed, // the wrapped buffered OutputStream does // not really need to get closed again. } } } finally { contentBlob.free(); } } try (PreparedStatement ps3 = connection.prepareStatement(getUpdateSizeSql())) { ps3.setLong(1, size); ps3.setString(2, md5Hash); ps3.setLong(3, generatedId); rowCount = ps3.executeUpdate(); if (rowCount == 0) { throw new BlobstoreException("Creating blob failed, no rows created."); } } if (resetCommitMode) { connection.commit(); } return new BlobKey(String.valueOf(generatedId)); } } catch (Exception e) { connection.rollback(); throw e; } finally { if (resetCommitMode) { connection.setAutoCommit(true); } } } catch (SQLException e) { throw new BlobstoreException("Error when creating blob", e); } }
From source file:edu.stolaf.cs.wmrserver.testjob.TestJobTask.java
protected TestJobResult.TransformResult runTransform(long id, File executable, File workingDir, InputStream input) throws IOException { // Create the result object TestJobResult.TransformResult result = new TestJobResult.TransformResult(); CountingOutputStream output = null; CountingOutputStream error = null;//from www .j av a 2 s .co m try { // Create and open temporary file for standard output File outputFile = File.createTempFile("job-" + Long.toString(_id), "-output", _tempDir); output = new CountingOutputStream(new FileOutputStream(outputFile)); // Create and open temporary file for standard error File errorFile = File.createTempFile("job-" + Long.toString(_id), "-error", _tempDir); error = new CountingOutputStream(new FileOutputStream(errorFile)); // If executable is relative, try to resolve in working directory // (This emulates the behavior of Streaming) if (!executable.isAbsolute()) { File resolvedExecutable = new File(workingDir, executable.toString()); if (resolvedExecutable.isFile()) { resolvedExecutable.setExecutable(true); executable = resolvedExecutable.getAbsoluteFile(); } } // Run the transform CommandLine command; if (_switchUserCommand == null) command = new CommandLine(executable); else { command = CommandLine.parse(_switchUserCommand); HashMap<String, String> substitutionMap = new HashMap<String, String>(); substitutionMap.put("cmd", executable.toString()); command.setSubstitutionMap(substitutionMap); } DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog dog = new ExecuteWatchdog(EXECUTABLE_TIMEOUT); PumpStreamHandler pump = new PumpStreamHandler(output, error, input); executor.setWorkingDirectory(workingDir); executor.setWatchdog(dog); executor.setStreamHandler(pump); executor.setExitValues(null); int exitCode = executor.execute(command); result.setExitCode(exitCode); // Check whether it produced any output if (output.getByteCount() == 0) { output.close(); outputFile.delete(); } else result.setOutputFile(outputFile); // Check whether it produced any error output if (error.getByteCount() == 0) { error.close(); errorFile.delete(); } else result.setErrorFile(errorFile); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(error); } return result; }
From source file:org.apache.flex.compiler.clients.ASC.java
/** * When {@code -swf} option is set, ASC compiles the source files into a SWF * file.//from w w w . j av a 2 s .com * * @param applicationProject application project * @param compilationUnits compilation unit(s) for the source file(s) * @param sourceFilename source file name * @param startTime time the build was started in nanoseconds * @return true if success * @throws InterruptedException error from compilation threads */ private boolean generateSWF(String outputDirectoryName, String outputBaseName, final ASCProject applicationProject, final Set<ICompilationUnit> compilationUnits, final String sourceFilename, ProblemQuery problemQuery, long startTime) throws InterruptedException { boolean success = true; final ArrayList<ICompilerProblem> problemsBuildingSWF = new ArrayList<ICompilerProblem>(); final ISWFTarget target = new AppSWFTarget(applicationProject, new ASCTargetSettings(sourceFilename), null, compilationUnits); final ISWF swf = target.build(problemsBuildingSWF); if (swf != null) { swf.setTopLevelClass(getSymbolClass()); final ISWFWriter writer = new SWFWriter(swf, Header.Compression.NONE); final String outputFileNameWithExt = outputBaseName + ".swf"; final File outputFile = new File(outputDirectoryName + outputFileNameWithExt); try { CountingOutputStream output = new CountingOutputStream( new BufferedOutputStream(new FileOutputStream(outputFile))); writer.writeTo(output); output.flush(); output.close(); writer.close(); out.format("%s, %d bytes written in %5.3f seconds\n", outputFile.toString(), output.getByteCount(), (System.nanoTime() - startTime) / 1e9); } catch (IOException e) { problemQuery.add(new FileWriteProblem(e)); success = false; } } else { err.println("Unable to build SWF."); success = false; } problemQuery.addAll(problemsBuildingSWF); return success; }
From source file:org.apache.flex.swf.io.SWFWriter.java
@Override public int writeTo(File outputFile) throws FileNotFoundException, IOException { // Ensure that the directory for the SWF exists. final File outputDirectory = new File(outputFile.getAbsoluteFile().getParent()); outputDirectory.mkdirs();//from ww w. j a v a2s . c o m // Write out the SWF, counting how many bytes were written. final CountingOutputStream output = new CountingOutputStream( new BufferedOutputStream(new FileOutputStream(outputFile))); writeTo(output); output.flush(); output.close(); close(); final int swfSize = output.getCount(); return swfSize; }
From source file:org.apache.synapse.transport.vfs.VFSTransportSender.java
private void populateResponseFile(FileObject responseFile, MessageContext msgContext, boolean append, boolean lockingEnabled, FileSystemOptions fso) throws AxisFault { MessageFormatter messageFormatter = getMessageFormatter(msgContext); OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext); try {/*from w w w .j a v a 2s .c o m*/ CountingOutputStream os = new CountingOutputStream(responseFile.getContent().getOutputStream(append)); try { messageFormatter.writeTo(msgContext, format, os, true); } finally { os.close(); } // update metrics metrics.incrementMessagesSent(msgContext); metrics.incrementBytesSent(msgContext, os.getByteCount()); } catch (FileSystemException e) { if (lockingEnabled) { VFSUtils.releaseLock(fsManager, responseFile, fso); } metrics.incrementFaultsSending(); handleException("IO Error while creating response file : " + VFSUtils.maskURLPassword(responseFile.getName().getURI()), e); } catch (IOException e) { if (lockingEnabled) { VFSUtils.releaseLock(fsManager, responseFile, fso); } metrics.incrementFaultsSending(); handleException("IO Error while creating response file : " + VFSUtils.maskURLPassword(responseFile.getName().getURI()), e); } }