List of usage examples for org.apache.commons.io.input CountingInputStream CountingInputStream
public CountingInputStream(InputStream in)
From source file:org.sead.repositories.reference.RefRepository.java
@Path("/researchobjects/{id}/metadata/{did}") @Produces(MediaType.APPLICATION_JSON)//www . j av a 2 s . co m @GET public Response getResourceSummary(@PathParam(value = "id") String id, @PathParam(value = "did") String dataID) { log.debug("Getting " + dataID + " from " + id); String path = getDataPathTo(id); String bagNameRoot = getBagNameRoot(id); File result = new File(path, bagNameRoot + ".zip"); if (!result.exists()) { return Response.status(Status.NOT_FOUND).build(); } CountingInputStream cis = null; try { File indexFile = getIndexFile(id); // Find/open base ORE map file // Note - limited to maxint size for oremap file size File map = getOREMapFile(id); cis = new CountingInputStream( new BufferedInputStream(new FileInputStream(map), Math.min((int) map.length(), 1000000))); JsonNode resultNode = getItem(dataID, indexFile, cis, true, map.length()); if (resultNode == null) { log.warn("Null item returned"); } return Response.ok(resultNode.toString()).build(); } catch (JsonParseException e) { log.error(e); e.printStackTrace(); return Response.serverError().entity(e.getMessage()).build(); } catch (IOException e) { log.error(e); e.printStackTrace(); return Response.serverError().entity(e.getMessage()).build(); } finally { IOUtils.closeQuietly(cis); } }
From source file:org.springframework.ws.transport.http.AbstractHttpSenderConnectionTest.java
/** * Tests that {@link AbstractHttpSenderConnection} doesn't consume the response stream before * passing it to the message factory. This is a regression test for SWS-707. * * @param chunking/*from w w w. j a v a 2 s . c o m*/ * Specifies whether the test should simulate a response with chunking enabled. * @throws Exception */ private void testSupportsStreaming(boolean chunking) throws Exception { byte[] content = new byte[16 * 1024]; new Random().nextBytes(content); CountingInputStream rawInputStream = new CountingInputStream(new ByteArrayInputStream(content)); AbstractHttpSenderConnection connection = createNiceMock(AbstractHttpSenderConnection.class); expect(connection.getResponseCode()).andReturn(200); // Simulate response with chunking enabled expect(connection.getResponseContentLength()).andReturn(chunking ? -1L : content.length); expect(connection.getRawResponseInputStream()).andReturn(rawInputStream); expect(connection.getResponseHeaders(anyObject())).andReturn(Collections.emptyIterator()); // Create a mock message factory to capture the InputStream passed to it WebServiceMessageFactory messageFactory = createNiceMock(WebServiceMessageFactory.class); WebServiceMessage message = createNiceMock(WebServiceMessage.class); Capture<InputStream> inputStreamCapture = new Capture<>(); expect(messageFactory.createWebServiceMessage(capture(inputStreamCapture))).andReturn(message); replay(connection, messageFactory, message); connection.receive(messageFactory); assertTrue("The raw input stream has been completely consumed", rawInputStream.getCount() < content.length); assertArrayEquals("Unexpected content received by the message factory", content, IOUtils.toByteArray(inputStreamCapture.getValue())); }
From source file:org.zanata.rest.client.FileResourceClientTest.java
private Pair<String, Long> calculateFileHashAndSize(InputStream in) { try (CountingInputStream countingStream = new CountingInputStream(in)) { MessageDigest md = MessageDigest.getInstance("MD5"); try (InputStream digestInputStream = new DigestInputStream(countingStream, md)) { byte[] buffer = new byte[256]; while (digestInputStream.read(buffer) > 0) { // continue }//from w ww . j a v a 2 s . c om } String hash = new String(Hex.encodeHex(md.digest())); return new ImmutablePair<>(hash, countingStream.getByteCount()); } catch (NoSuchAlgorithmException | IOException e) { throw new RuntimeException(e); } }
From source file:password.pwm.util.localdb.LocalDBUtility.java
private void importLocalDB(final InputStream inputStream, final Appendable out, final long totalBytes) throws PwmOperationalException, IOException { this.prepareForImport(); importLineCounter = 0;//from w ww . j av a 2s .c om if (totalBytes > 0) { writeStringToOut(out, "total bytes in localdb import source: " + totalBytes); } writeStringToOut(out, "beginning localdb import..."); final Instant startTime = Instant.now(); final TransactionSizeCalculator transactionCalculator = new TransactionSizeCalculator( new TransactionSizeCalculator.SettingsBuilder() .setDurationGoal(new TimeDuration(100, TimeUnit.MILLISECONDS)).setMinTransactions(50) .setMaxTransactions(5 * 1000).createSettings()); final Map<LocalDB.DB, Map<String, String>> transactionMap = new HashMap<>(); for (final LocalDB.DB loopDB : LocalDB.DB.values()) { transactionMap.put(loopDB, new TreeMap<>()); } final CountingInputStream countingInputStream = new CountingInputStream(inputStream); final EventRateMeter eventRateMeter = new EventRateMeter(TimeDuration.MINUTE); final Timer statTimer = new Timer(true); statTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { String output = ""; if (totalBytes > 0) { final ProgressInfo progressInfo = new ProgressInfo(startTime, totalBytes, countingInputStream.getByteCount()); output += progressInfo.debugOutput(); } else { output += "recordsImported=" + importLineCounter; } output += ", avgTransactionSize=" + transactionCalculator.getTransactionSize() + ", recordsPerMinute=" + eventRateMeter.readEventRate().setScale(2, BigDecimal.ROUND_DOWN); writeStringToOut(out, output); } }, 30 * 1000, 30 * 1000); Reader csvReader = null; try { csvReader = new InputStreamReader(new GZIPInputStream(countingInputStream, GZIP_BUFFER_SIZE), PwmConstants.DEFAULT_CHARSET); for (final CSVRecord record : PwmConstants.DEFAULT_CSV_FORMAT.parse(csvReader)) { importLineCounter++; eventRateMeter.markEvents(1); final String dbName_recordStr = record.get(0); final LocalDB.DB db = JavaHelper.readEnumFromString(LocalDB.DB.class, null, dbName_recordStr); final String key = record.get(1); final String value = record.get(2); if (db == null) { writeStringToOut(out, "ignoring localdb import record #" + importLineCounter + ", invalid DB name '" + dbName_recordStr + "'"); } else { transactionMap.get(db).put(key, value); int cachedTransactions = 0; for (final LocalDB.DB loopDB : LocalDB.DB.values()) { cachedTransactions += transactionMap.get(loopDB).size(); } if (cachedTransactions >= transactionCalculator.getTransactionSize()) { final long startTxnTime = System.currentTimeMillis(); for (final LocalDB.DB loopDB : LocalDB.DB.values()) { localDB.putAll(loopDB, transactionMap.get(loopDB)); transactionMap.get(loopDB).clear(); } transactionCalculator.recordLastTransactionDuration(TimeDuration.fromCurrent(startTxnTime)); } } } } finally { LOGGER.trace("import process completed"); statTimer.cancel(); IOUtils.closeQuietly(csvReader); IOUtils.closeQuietly(countingInputStream); } for (final LocalDB.DB loopDB : LocalDB.DB.values()) { localDB.putAll(loopDB, transactionMap.get(loopDB)); transactionMap.get(loopDB).clear(); } this.markImportComplete(); writeStringToOut(out, "restore complete, restored " + importLineCounter + " records in " + TimeDuration.fromCurrent(startTime).asLongString()); statTimer.cancel(); }