List of usage examples for java.io InputStream mark
public synchronized void mark(int readlimit)
From source file:com.effektif.adapter.helpers.RequestLogger.java
private InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) throws IOException { if (!stream.markSupported()) { stream = new BufferedInputStream(stream); }//from w ww . j av a 2 s . co m stream.mark(maxEntitySize + 1); final byte[] entity = new byte[maxEntitySize + 1]; final int entitySize = stream.read(entity); b.append(REQUEST_PREFIX); String entityString = new String(entity, 0, Math.min(entitySize, maxEntitySize), charset); if (logEntityJsonPretty && (entitySize <= maxEntitySize)) { entityString = getJsonPrettyString(entityString); } b.append(entityString); if (entitySize > maxEntitySize) { b.append("...more..."); } stream.reset(); return stream; }
From source file:com.redhat.rhn.domain.config.ConfigurationFactory.java
/** * Convert input stream to byte array// w ww . j a va 2 s . com * @param stream input stream * @param size stream size * @return byte array */ public static byte[] bytesFromStream(InputStream stream, Long size) { byte[] foo = new byte[size.intValue()]; try { //this silly bit of logic is to ensure that we read as much from the file //as we possibly can. Most likely, stream.read(foo) would do the exact same //thing, but according to the javadoc, that may not always be the case. int offset = 0; int read = 0; // mark and reset stream, so that stream can be re-read later stream.mark(size.intValue()); do { read = stream.read(foo, offset, (foo.length - offset)); offset += read; } while (read > 0 && offset < foo.length); stream.reset(); } catch (IOException e) { log.error("IOException while reading config content from input stream!", e); throw new RuntimeException("IOException while reading config content from" + " input stream!"); } return foo; }
From source file:mj.ocraptor.extraction.tika.parser.iwork.IWorkPackageParser.java
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { ZipArchiveInputStream zip = new ZipArchiveInputStream(stream); ZipArchiveEntry entry = zip.getNextZipEntry(); TikaImageHelper helper = new TikaImageHelper(metadata); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); IWORKDocumentType type = null;//from w ww . j a v a2 s . c om try { while (entry != null) { // ------------------------------------------------ // // -- handle image files // ------------------------------------------------ // String entryExtension = null; try { entryExtension = FilenameUtils.getExtension(new File(entry.getName()).getName()); } catch (Exception e) { e.printStackTrace(); } if (entryExtension != null && FileType.isValidImageFileExtension(entryExtension)) { File imageFile = null; try { imageFile = TikaImageHelper.saveZipEntryToTemp(zip, entry); helper.addImage(imageFile); } catch (Exception e) { e.printStackTrace(); } finally { if (imageFile != null) { imageFile.delete(); } } } // ------------------------------------------------ // // -- // ------------------------------------------------ // if (!IWORK_CONTENT_ENTRIES.contains(entry.getName())) { entry = zip.getNextZipEntry(); continue; } InputStream entryStream = new BufferedInputStream(zip, 4096); entryStream.mark(4096); type = IWORKDocumentType.detectType(entryStream); entryStream.reset(); if (type != null) { ContentHandler contentHandler; switch (type) { case KEYNOTE: contentHandler = new KeynoteContentHandler(xhtml, metadata); break; case NUMBERS: contentHandler = new NumbersContentHandler(xhtml, metadata); break; case PAGES: contentHandler = new PagesContentHandler(xhtml, metadata); break; case ENCRYPTED: // We can't do anything for the file right now contentHandler = null; break; default: throw new TikaException("Unhandled iWorks file " + type); } metadata.add(Metadata.CONTENT_TYPE, type.getType().toString()); xhtml.startDocument(); if (contentHandler != null) { context.getSAXParser().parse(new CloseShieldInputStream(entryStream), new OfflineContentHandler(contentHandler)); } } entry = zip.getNextZipEntry(); } helper.addTextToHandler(xhtml); xhtml.endDocument(); } catch (Exception e) { // TODO: logging e.printStackTrace(); } finally { if (zip != null) { zip.close(); } if (helper != null) { helper.close(); } } }
From source file:no.nordicsemi.android.nrftoolbox.dfu.HexInputStream.java
private int calculateBinSize() throws IOException { int binSize = 0; final InputStream in = this.in; in.mark(in.available()); int b, lineSize, type; try {/* ww w .j ava 2s . c o m*/ b = in.read(); while (true) { checkComma(b); lineSize = readByte(in); // reading the length of the data in this line in.skip(4); // skipping address part type = readByte(in); // reading the line type switch (type) { case 0x00: // data type line binSize += lineSize; break; case 0x01: // end of file return binSize; case 0x02: // extended segment address record case 0x04: // extended linear address record default: break; } in.skip(lineSize * 2 /* 2 hex per one byte */ + 2 /* check sum */); // skip end of line while (true) { b = in.read(); if (b != '\n' && b != '\r') { break; } } } } finally { try { in.reset(); } catch (IOException ex) { this.in = new BufferedInputStream( new DefaultHttpClient().execute(this.httpGet).getEntity().getContent()); } } }
From source file:com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPlugin.java
/** * @param filecontent the content of the file * @return true if the file is in zip format (.zip, .jar etc) or false otherwise *//*from w ww.ja v a2s . c o m*/ protected boolean isZipFile(InputStream filecontent) { int standardZipHeader = 0x504b0304; filecontent.mark(8); try { DataInputStream datastream = new DataInputStream(filecontent); int fileHeader = datastream.readInt(); return (standardZipHeader == fileHeader); } catch (IOException e) { // The file doesn't have 4 bytes, so it isn't a zip file } finally { // Reset the input stream to the beginning. This may be needed for further reading the archive. try { filecontent.reset(); } catch (IOException e) { e.printStackTrace(); } } return false; }
From source file:com.adaptris.core.lms.StreamWrapperCase.java
@Test public void testInputStream_Mark() throws Exception { StreamWrapper wrapper = createWrapper(false); File file = writeFile(TempFileUtils.createTrackedFile(wrapper)); InputStream in = wrapper.openInputStream(file, NO_OP); try (InputStream closeable = in) { tryQuietly(() -> {// www . j av a2 s .co m in.markSupported(); }); tryQuietly(() -> { in.mark(10); }); tryQuietly(() -> { in.reset(); }); } }
From source file:org.openmastery.logging.LoggingFilter.java
private InputStream logInboundEntity(StringBuilder b, InputStream stream, Charset charset) throws IOException { if (!stream.markSupported()) { stream = new BufferedInputStream(stream); }/* w w w. j a v a 2 s. co m*/ stream.mark(maxEntitySize + 1); byte[] entity = new byte[maxEntitySize + 1]; int entitySize = stream.read(entity); entitySize = entitySize < 0 ? 0 : entitySize; b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset)); if (entitySize > maxEntitySize) { b.append("...more..."); } b.append('\n'); stream.reset(); return stream; }
From source file:se.inera.axel.shs.processor.ShsMessageMarshaller.java
/** * Reads the beginning of the stream to parse the label. * * @param inputStream a stream that must either support mark or be a StreamCache so that it can be reset. * * @return the label parsed from the stream * * @throws IllegalMessageStructureException if the label cannot be parsed or if the stream cannot be * reset after the label has been parsed. *///from ww w .j av a2 s . c o m public ShsLabel parseLabel(InputStream inputStream) throws IllegalMessageStructureException { try { // Mark might not be supported inputStream.mark(4096); byte[] buffer = new byte[4096]; IOUtils.read(inputStream, buffer, 0, 4096); // Will throw IOException if the InputStream mark has been invalidated // or if the stream does not support mark and is not a StreamCache inputStream.reset(); String xml = StringUtils.substringBetween(new String(buffer, Charset.forName("ISO-8859-1")), "<shs.label ", "</shs.label>"); if (xml == null) { throw new IllegalMessageStructureException("shs label not found in: " + new String(buffer)); } ShsLabel label = shsLabelMarshaller.unmarshal("<shs.label " + xml + "</shs.label>"); return label; } catch (IOException e) { throw new IllegalMessageStructureException("Error parsing label xml", e); } }
From source file:org.jbpm.console.ng.documents.backend.server.DocumentViewServlet.java
private void uploadFile(final FileItem uploadItem, String folder) throws IOException { InputStream fileData = uploadItem.getInputStream(); // GAV gav = uploadItem.getGav(); try {// www . ja va 2 s.co m // if ( gav == null ) { if (!fileData.markSupported()) { fileData = new BufferedInputStream(fileData); } // is available() safe? fileData.mark(fileData.available()); byte[] bytes = IOUtils.toByteArray(fileData); DocumentSummary documenSummary = new DocumentSummary(uploadItem.getName(), "", folder); documenSummary.setContent(bytes); this.documentService.createDocument(documenSummary); } catch (Exception e) { } }
From source file:com.myapps.upesse.upes_spefest.ui.activity.NewPostUploadTaskFragment.java
public Bitmap decodeSampledBitmapFromUri(Uri fileUri, int reqWidth, int reqHeight) throws IOException { InputStream stream = new BufferedInputStream( mApplicationContext.getContentResolver().openInputStream(fileUri)); stream.mark(stream.available()); BitmapFactory.Options options = new BitmapFactory.Options(); //options./*from w ww .java2s .c o m*/ // First decode with inJustDecodeBounds=true to check dimensions options.inJustDecodeBounds = true; BitmapFactory.decodeStream(stream, null, options); stream.reset(); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; BitmapFactory.decodeStream(stream, null, options); // Decode bitmap with inSampleSize set stream.reset(); return BitmapFactory.decodeStream(stream, null, options); }