List of usage examples for org.apache.commons.io.input CountingInputStream CountingInputStream
public CountingInputStream(InputStream in)
From source file:org.hardisonbrewing.s3j.FileResponseHandler.java
@Override public File handleResponse(HttpResponse httpResponse) throws HttpResponseException, IOException { System.out.println(" Response Headers"); HttpUtil.printHeaders(httpResponse); HttpUtil.validateResponseCode(httpResponse); File file = FileUtils.createTempFile(); long contentLength = 0; HttpEntity entity = null;//from w w w .j a v a 2 s. c o m InputStream inputStream = null; CountingInputStream countingInputStream = null; OutputStream outputStream = null; try { entity = httpResponse.getEntity(); contentLength = entity.getContentLength(); validateContentLength(contentLength); inputStream = entity.getContent(); inputStream = new ProgressInputStream(inputStream, contentLength); // put this before the cipher so we get the encrypted length countingInputStream = new CountingInputStream(inputStream); inputStream = countingInputStream; if (cipher != null) { inputStream = new CipherInputStream(inputStream, cipher); } outputStream = new FileOutputStream(file); IOUtil.copy(inputStream, outputStream); } finally { IOUtil.close(inputStream); EntityUtils.consume(entity); IOUtil.close(outputStream); } long readLength = countingInputStream.getByteCount(); validateDownloadLength(contentLength, readLength); return file; }
From source file:org.jcodec.codecs.mjpeg.JpegParser.java
public CodedImage parse(InputStream is) throws IOException { CountingInputStream counter = new CountingInputStream(is); return parse(new PushbackInputStream(counter, 2), counter); }
From source file:org.jcodec.codecs.mjpeg.JpegParser.java
public CodedImage parse(PushbackInputStream is, CountingInputStream counter) throws IOException { CodedImage image = new CodedImage(); int curQTable = 0; while (true) { int marker = is.read(); if (marker == -1) return image; if (marker == 0) continue; if (marker != 0xFF) throw new RuntimeException("@" + Long.toHexString(counter.getByteCount()) + " Marker expected: 0x" + Integer.toHexString(marker)); int b = is.read(); Debug.trace("%s", Markers.toString(b)); switch (b) { case Markers.SOF0: image.frame = FrameHeader.read(is); Debug.trace(" %s", image.frame); break; case Markers.DHT: int len1 = readShort(is); CountingInputStream cis = new CountingInputStream(is); while (cis.getCount() < len1 - 2) { readHuffmanTable(cis, image); }/*from w w w. j av a 2 s .com*/ break; case Markers.DQT: int len4 = readShort(is); CountingInputStream cis1 = new CountingInputStream(is); while (cis1.getCount() < len4 - 2) { QuantTable quantTable = readQuantTable(cis1); if (curQTable == 0) image.setQuantLum(quantTable); else image.setQuantChrom(quantTable); curQTable++; } break; case Markers.SOS: if (image.scan != null) { throw new IllegalStateException("unhandled - more than one scan header"); } image.scan = ScanHeader.read(is); Debug.trace(" %s", image.scan); image.setData(readData(is)); break; case Markers.SOI: break; case Markers.EOI: return image; case Markers.APP0: // int len10 = readShort(is); // byte[] id = new byte[4]; // is.read(id); // if (!Arrays.equals(JFIF, id)) // throw new RuntimeException("Not a JFIF file"); // is.skip(1); // // is.skip(2); // int units = is.read(); // int dx = readShort(is); // int dy = readShort(is); // int tx = is.read(); // int ty = is.read(); // is.skip(tx * ty * 3); // break; case Markers.APP1: case Markers.APP2: case Markers.APP3: case Markers.APP4: case Markers.APP5: case Markers.APP6: case Markers.APP7: case Markers.APP8: case Markers.APP9: case Markers.APPA: case Markers.APPB: case Markers.APPC: case Markers.APPD: case Markers.APPE: case Markers.APPF: int len3 = readShort(is); StringReader.sureSkip(is, len3 - 2); break; case Markers.DRI: /* * Lr: Define restart interval segment length Specifies the * length of the parameters in the DRI segment shown in Figure * B.9 (see B.1.1.4). */ int lr = readShort(is); // Ri: Restart interval Specifies the number of MCU in the // restart interval. int ri = readShort(is); Debug.trace("DRI Lr: %d Ri: %d", lr, ri); // A DRI marker segment with Ri equal to zero shall disable // restart intervals for the following scans. Asserts.assertEquals(0, ri); break; default: { throw new IllegalStateException("unhandled marker " + Markers.toString(b)); } } } }
From source file:org.jcodec.codecs.mjpeg.MJPEGParser.java
@Test @Ignore//from w ww. j a v a 2 s . c o m public void testPerformance() throws Exception { JpegDecoder dec = new JpegDecoder(); JpegParser parser = new JpegParser(); InputStream is = new BufferedInputStream(new FileInputStream("/Users/zhukov/Movies/MVI_0636.mjpg")); CountingInputStream counter = new CountingInputStream(is); PushbackInputStream push = new PushbackInputStream(counter, 2); CodedImage codedImage = parser.parse(is); DecodedImage decode = new DecodedImage(codedImage.getWidth(), codedImage.getHeight()); dec.decode(codedImage, decode); long start = System.currentTimeMillis(); for (int i = 1; i < 2; i++) { if (i % 10 == 0) { long time = System.currentTimeMillis() - start; long fps = i * 1000 / time; System.out.println(i + " " + fps + "fps"); } codedImage = parser.parse(push, counter); dec.decode(codedImage, decode); } }
From source file:org.jcodec.codecs.wav.WavHeader.java
public static WavHeader read(InputStream in) throws IOException { CountingInputStream cin = new CountingInputStream(in); String chunkId = readString(cin, 4); int chunkSize = readInt(cin); String format = readString(cin, 4); FmtChunk fmt = null;/*from w w w . ja v a 2 s. c o m*/ if (!"RIFF".equals(chunkId) || !"WAVE".equals(format)) { return null; } String fourcc; int size = 0; do { fourcc = readString(cin, 4); size = ReaderLE.readInt(cin); if ("fmt ".equals(fourcc) && size >= 14 && size <= 1024 * 1024) { switch (size) { case 16: case 18: fmt = FmtChunk.read(cin); StringReader.sureSkip(cin, 2); break; case 40: fmt = FmtChunkExtended.read(cin); StringReader.sureSkip(cin, 12); break; case 28: fmt = FmtChunkExtended.read(cin); break; default: throw new IllegalStateException("Don't know how to handle fmt size: " + size); } } else if (!"data".equals(fourcc)) { StringReader.sureRead(cin, size); } } while (!"data".equals(fourcc)); return new WavHeader(chunkId, chunkSize, format, fmt, cin.getCount(), size); }
From source file:org.lockss.plugin.taylorandfrancis.TafHtmlHashFilterFactory.java
@Override public InputStream createFilteredInputStream(final ArchivalUnit au, InputStream in, String encoding) throws PluginException { HtmlFilterInputStream filtered = new HtmlFilterInputStream(in, encoding, new HtmlCompoundTransform( /*/*from w w w .j ava2 s .co m*/ * KEEP: throw out everything but main content areas */ HtmlNodeFilterTransform.include(new OrFilter(new NodeFilter[] { // KEEP manifest page elements (no distinctive characteristics) [manifest] HtmlNodeFilters.tagWithAttributeRegex("a", "href", "^(https?://[^/]+)?/toc/[^/]+/[^/]+/[^/]+/?$"), // KEEP top part of main content area [TOC, abs, full, ref] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "overview"), // KEEP each article block [TOC] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "\\barticle\\b"), // avoid match on pageArticle // KEEP abstract and preview [abs, full, ref] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "abstract"), HtmlNodeFilters.tagWithAttribute("div", "id", "preview"), // KEEP active content area [abs, full, ref, suppl] HtmlNodeFilters.tagWithAttribute("div", "id", "informationPanel"), // article info [abs] HtmlNodeFilters.tagWithAttribute("div", "id", "fulltextPanel"), // full text [full] HtmlNodeFilters.tagWithAttribute("div", "id", "referencesPanel"), // references [ref] HtmlNodeFilters.tagWithAttribute("div", "id", "supplementaryPanel"), // supplementary materials [suppl] // KEEP citation format form [showCitFormats] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "citationContainer"), // KEEP popup window content area [showPopup] HtmlNodeFilters.tagWithAttribute("body", "class", "popupBody"), })), /* * DROP: filter remaining content areas */ HtmlNodeFilterTransform.exclude(new OrFilter(new NodeFilter[] { // DROP scripts, styles, comments HtmlNodeFilters.tag("script"), HtmlNodeFilters.tag("noscript"), HtmlNodeFilters.tag("style"), HtmlNodeFilters.comment(), // DROP social media bar [overview] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "social"), // DROP access box (changes e.g. when the article becomes free) [article block, abs/full/ref/suppl overview] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "accessmodule"), // DROP number of article views [article block, abs/full/ref/suppl overview] HtmlNodeFilters.tagWithAttribute("div", "class", "articleUsage"), // DROP "Related articles" variants [article block, abs/full/ref/suppl overview] HtmlNodeFilters.tagWithAttribute("a", "class", "relatedLink"), // old? HtmlNodeFilters.tagWithAttribute("li", "class", "relatedArticleLink"), // [article block] HtmlNodeFilters.tagWithText("h3", "Related articles"), // [abs/full/ref/suppl overview] HtmlNodeFilters.tagWithAttribute("a", "class", "searchRelatedLink"), // [abs/full/ref/suppl overview] // DROP title options (e.g. 'Publication History', 'Sample this title') [TOC overview] HtmlNodeFilters.tagWithAttribute("div", "class", "options"), // DROP title icons (e.g. 'Routledge Open Select') [TOC overview] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "journalOverviewAds"), // DROP Google Translate artifacts [abs/full/ref/suppl overview] HtmlNodeFilters.tagWithAttribute("div", "id", "google_translate_element"), // current HtmlNodeFilters.tagWithAttribute("div", "id", "goog-gt-tt"), // old HtmlNodeFilters.tagWithText("a", "Translator disclaimer"), HtmlNodeFilters.tagWithText("a", "Translator disclaimer"), // DROP "Alert me" variants [abs/full/ref overview] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "alertDiv"), // current HtmlNodeFilters.tagWithAttributeRegex("div", "id", "alertDiv"), // old // DROP "Publishing models and article dates explained" link [abs/full/ref/suppl overview] HtmlNodeFilters.tagWithAttributeRegex("a", "href", "models-and-dates-explained"), // DROP outgoing links and SFX links [article block, full, ref] HtmlNodeFilters.allExceptSubtree( HtmlNodeFilters.tagWithAttribute("span", "class", "referenceDiv"), HtmlNodeFilters.tagWithAttribute("a", "class", "dropDownLabel")), // popup at each inline citation [full] HtmlNodeFilters.tagWithAttributeRegex("a", "href", "^/servlet/linkout\\?"), // [article block, full/ref referencesPanel] HtmlNodeFilters.tagWithAttribute("a", "class", "sfxLink"), // [article block, full/ref referencesPanel] // DROP "Jump to section" popup menus [full] HtmlNodeFilters.tagWithAttributeRegex("div", "class", "summationNavigation"), })))); // FIXME 1.67.4 filtered.registerTag(new CompositeTag() { @Override public String[] getIds() { return new String[] { "CENTER" }; } }); Reader reader = FilterUtil.getReader(filtered, encoding); Reader stringFilter = StringFilter.makeNestedFilter(reader, new String[][] { // Markup changes over time { " ", " " }, { "&", "&" }, // Wording change over time [article block, abs/full/ref/suppl overview] { "<strong>Published online:</strong>" }, // current { "<strong>Available online:</strong>" }, // old { "<strong>Version of record first published:</strong>" } // old }, true); Reader tagFilter = HtmlTagFilter.makeNestedFilter(stringFilter, Arrays.asList( // Alternate forms of citation links [article block] new TagPair("<li><div><strong>Citing Articles:", "</li>", true), // current new TagPair("<li><strong>Citations:", "</li>", true), // old? new TagPair("<li><strong><a href=\"/doi/citedby/", "</li>", true), // old? new TagPair("<li><strong>Citation information:", "</li>", true), // old? // Leftover commas after outgoing/SFX links removed [full/ref referencesPanel] new TagPair("</pub-id>", "</li>", true))); // Remove all inner tag content Reader noTagFilter = new HtmlTagFilter(new StringFilter(tagFilter, "<", " <"), new TagPair("<", ">")); // Remove white space Reader noWhiteSpace = new WhiteSpaceFilter(noTagFilter); InputStream ret = new ReaderInputStream(noWhiteSpace); // Instrumentation return new CountingInputStream(ret) { @Override public void close() throws IOException { long bytes = getByteCount(); if (bytes <= 100L) { log.debug(String.format("%d byte%s in %s", bytes, bytes == 1L ? "" : "s", au.getName())); } if (log.isDebug2()) { log.debug2(String.format("%d byte%s in %s", bytes, bytes == 1L ? "" : "s", au.getName())); } super.close(); } }; }
From source file:org.openmrs.module.openconceptlab.updater.Updater.java
/** * Runs an update for a configured subscription. * <p>/*from www.ja va 2 s . c o m*/ * It is not run directly, rather by a dedicated scheduler {@link UpdateScheduler}. * * @should start first update with response date * @should start next update with updated since * @should create item for each concept and mapping */ @Override public void run() { Daemon.runInDaemonThreadAndWait(new Runnable() { @Override public void run() { runAndHandleErrors(new Task() { @Override public void run() throws Exception { Subscription subscription = updateService.getSubscription(); Update lastUpdate = updateService.getLastSuccessfulSubscriptionUpdate(); Date updatedSince = null; if (lastUpdate != null) { updatedSince = lastUpdate.getOclDateStarted(); } OclResponse oclResponse; if (updatedSince == null) { oclResponse = oclClient.fetchInitialUpdates(subscription.getUrl(), subscription.getToken()); } else { oclResponse = oclClient.fetchUpdates(subscription.getUrl(), subscription.getToken(), updatedSince); } updateService.updateOclDateStarted(update, oclResponse.getUpdatedTo()); in = new CountingInputStream(oclResponse.getContentStream()); totalBytesToProcess = oclResponse.getContentLength(); processInput(); in.close(); } }); } }, OpenConceptLabActivator.getDaemonToken()); }
From source file:org.openmrs.module.openconceptlab.updater.Updater.java
/** * It can be used to run update from the given input e.g. from a resource bundled with a module. * <p>/*from w w w . j ava 2 s . c o m*/ * It does not require any subscription to be setup. * * @param inputStream to JSON in OCL format */ public void run(final InputStream inputStream) { runAndHandleErrors(new Task() { @Override public void run() throws IOException { in = new CountingInputStream(inputStream); processInput(); in.close(); } }); }
From source file:org.phoenicis.win32.pe.PEReader.java
public PEFile parseExecutable(InputStream inputStream) throws IOException { try (CountingInputStream executableInputStream = new CountingInputStream(inputStream)) { final ImageDOSHeader imageDOSHeader = readDosHeader(executableInputStream); final byte[] realModeStubProgram = readRealModeStubProgram(executableInputStream, imageDOSHeader); final ImageNTHeaders imageNTHeaders = readImageNTHeaders(executableInputStream); final SectionHeader[] sectionHeaders = readSectionHeaders(executableInputStream, imageNTHeaders); final RsrcSection resourceSection = readResourceSection(executableInputStream, sectionHeaders); return new PEFile(imageDOSHeader, realModeStubProgram, imageNTHeaders, sectionHeaders, resourceSection); }//from w w w. jav a 2 s.c o m }
From source file:org.sead.repositories.reference.RefRepository.java
@Path("/researchobjects/{id}/metadata") @Produces(MediaType.APPLICATION_JSON)/*w w w. j a va 2s .co m*/ @GET public Response getResourceMetadata(@PathParam(value = "id") String 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(); } log.debug(result.getAbsolutePath()); CountingInputStream cis = null; try { // Check for index files File indexFile = getIndexFile(id); File oremap = getOREMapFile(id); // Find/open base ORE map file // Note - limited to maxint size for oremap file size cis = new CountingInputStream( new BufferedInputStream(new FileInputStream(oremap), Math.min((int) oremap.length(), 1000000))); JsonNode resultNode = getAggregation(id, indexFile, cis, true, oremap.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); } }