List of usage examples for java.io BufferedInputStream reset
public synchronized void reset() throws IOException
reset
method of InputStream
. From source file:org.trustedanalytics.metadata.utils.ContentDetectionUtils.java
private static Optional<MediaType> notConsumingGuessContentTypeFromStream(BufferedInputStream bin) throws IOException { Optional<MediaType> type = Optional.empty(); bin.mark(MAX_BYTES_READ_WHILE_PROBING_TYPE); try {// w w w .jav a 2s . c om String guess = URLConnection.guessContentTypeFromStream(bin); if (guess != null) { type = MediaType.fromString(guess); } } catch (IOException e) { LOGGER.error("Error while guessing stream type", e); } bin.reset(); bin.mark(0); return type; }
From source file:Main.java
/** * Decodes image from inputstream into a new Bitmap of specified dimensions. * * This is a long-running operation that must run in a background thread. * * @param is InputStream containing the image. * @param maxWidth target width of the output Bitmap. * @param maxHeight target height of the output Bitmap. * @return new Bitmap containing the image. * @throws IOException// w w w . j a va 2s . c om */ public static Bitmap decodeBitmapBounded(InputStream is, int maxWidth, int maxHeight) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(is, STREAM_BUFFER_SIZE); try { bufferedInputStream.mark(STREAM_BUFFER_SIZE); // should be enough to read image dimensions. // TODO(mattfrazier): fail more gracefully if mark isn't supported, but it should always be // by bufferedinputstream. BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(bufferedInputStream, null, bmOptions); bufferedInputStream.reset(); bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = calculateInSampleSize(bmOptions.outWidth, bmOptions.outHeight, maxWidth, maxHeight); // TODO(mattfrazier): Samsung devices yield a rotated bitmap no matter what orientation is // captured. Read Exif data and rotate in place or communicate Exif data and rotate display // with matrix. return BitmapFactory.decodeStream(bufferedInputStream, null, bmOptions); } finally { bufferedInputStream.close(); } }
From source file:org.opf_labs.fmts.fidget.TikaIdentifier.java
static final IdentificationResult fromStream(final MimeTypes mimeRepo, final InputStream stream, URI loc) { // Get a buffered input stream that supports marks BufferedInputStream mrkStr = new BufferedInputStream(stream); assert (mrkStr.markSupported()); // put the mark at the begining, and should be comfortable for Tika id // length/* w ww . j ava 2s. c om*/ mrkStr.mark(mimeRepo.getMinLength() * 2); // identify and time long start = new Date().getTime(); MediaType mime = identify(mimeRepo, stream, loc); long duration = new Date().getTime() - start; // Now reset the stream and hash IdentificationResult result; try { mrkStr.reset(); result = new IdentificationResult(hash64K(stream), loc, mime, duration); } catch (IOException excep) { // OK couldn't read or hash stream, record error and what we have result = new IdentificationResult("", IdentificationResult.ERROR_LOC, mime, duration); } return result; }
From source file:www.image.ImageManager.java
public static Bitmap scaleBitmap(BufferedInputStream imagePath, int width, int height) { Bitmap bmp = null;/* w w w.ja v a 2 s . co m*/ BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeStream(imagePath, null, opts); try { imagePath.reset(); int scaleWidth = (int) Math.floor((double) opts.outWidth / width); int scaleHeight = (int) Math.floor((double) opts.outHeight / height); opts.inJustDecodeBounds = false; opts.inSampleSize = Math.min(scaleWidth, scaleHeight); bmp = BitmapFactory.decodeStream(imagePath, null, opts); } catch (IOException e) { e.printStackTrace(); bmp = mFailBitmap; } return bmp; }
From source file:com.github.pascalgn.jiracli.web.HttpClient.java
private static InputStream maybeDecompress(InputStream input) throws IOException { // Due to a bug, Jira sometimes returns double-compressed responses. See JRA-37608 BufferedInputStream buffered = new BufferedInputStream(input, 2); buffered.mark(2);//from w ww .ja v a2 s . c om int[] buf = new int[2]; buf[0] = buffered.read(); buf[1] = buffered.read(); buffered.reset(); int header = (buf[1] << 8) | buf[0]; if (header == GZIPInputStream.GZIP_MAGIC) { return new GZIPInputStream(buffered); } else { return buffered; } }
From source file:Hash.Hash.java
private static byte[] readSubIFDirectory(ifdCursor cursor, BufferedInputStream in) throws IOException { in.reset(); if (!skipBytes(in, cursor.getPointer())) return null; int tagEntryCount = (int) readEndianValue(in, 2, cursor.getEndian()); long subIFDs = 0; long subIFDsPointer = 0; int subIFDsPointerLength = 0; boolean mainImage = false; ArrayList<ifdField> imageLocationFields = new ArrayList<>(); for (int i = 0; i < tagEntryCount; i++) { ifdField field = new ifdField(); field.tag = (int) readEndianValue(in, 2, cursor.getEndian()); field.type = (int) readEndianValue(in, 2, cursor.getEndian()); field.count = readEndianValue(in, 4, cursor.getEndian()); field.offset = readEndianValue(in, 4, cursor.getEndian()); if (field.tag == 50972) System.out.println("!!!!!!!!!!" + field.count + " " + field.offset); if (field.tag == 254 && field.offset == 0) { mainImage = true;/*from w ww. ja va 2s. co m*/ } if (field.tag == 330) { subIFDsPointer = field.offset; subIFDs = field.count; subIFDsPointerLength = field.getTypeLength(); } // System.out.println(field.getTag() + " " + field.getType() + " " + field.getCount() + " " + field.getValue() + " " + field.getPointer()); if (field.tag == 257 || field.tag == 256) imageLocationFields.add(field); //Image if (field.tag == 273 || field.tag == 278 || field.tag == 279) imageLocationFields.add(field); //Stripe if (field.tag == 322 || field.tag == 323 || field.tag == 324 || field.tag == 325) imageLocationFields.add(field); //Tile } if (mainImage) return getPointers(imageLocationFields, cursor.getFile(), cursor.getEndian()); if (subIFDs == 1) { cursor.setPointer(subIFDsPointer); byte[] hash = readSubIFDirectory(cursor, in); if (hash != null) return hash; } else if (subIFDs > 1) { for (int j = 0; j < subIFDs; j++) { in.reset(); // in = new BufferedInputStream(new FileInputStream(cursor.getFile().toString())); if (!skipBytes(in, subIFDsPointer)) return null; if (!skipBytes(in, j * subIFDsPointerLength)) return null; cursor.setPointer(readEndianValue(in, subIFDsPointerLength, cursor.getEndian())); byte[] hash = readSubIFDirectory(cursor, in); if (hash != null) return hash; } } return null; }
From source file:Hash.Hash.java
private static byte[] readIFDirectory(ifdCursor cursor, BufferedInputStream in) throws IOException { in.reset(); if (!skipBytes(in, cursor.getPointer())) return null; int tagEntryCount = (int) readEndianValue(in, 2, cursor.getEndian()); long subIFDs = 0; long subIFDsPointer = 0; int subIFDsPointerLength = 0; long nextIFD = 0; boolean mainImage = false; ArrayList<ifdField> imageLocationFields = new ArrayList<>(); for (int i = 0; i < tagEntryCount; i++) { ifdField field = new ifdField(); field.tag = (int) readEndianValue(in, 2, cursor.getEndian()); field.type = (int) readEndianValue(in, 2, cursor.getEndian()); field.count = readEndianValue(in, 4, cursor.getEndian()); field.offset = readEndianValue(in, 4, cursor.getEndian()); if (field.tag == 50972) System.out.println("!!!!!!!!!!" + field.count + " " + field.offset); if (field.tag == 254 && field.offset == 0) { mainImage = true;//from w ww . j a v a 2 s .c o m } if (field.tag == 330) { subIFDsPointer = field.offset; subIFDs = field.count; subIFDsPointerLength = field.getTypeLength(); } // System.out.println(field.getTag() + " " + field.getType() + " " + field.getCount() + " " + field.getValue() + " " + field.getPointer()); if (field.tag == 257 || field.tag == 256) imageLocationFields.add(field); //Image if (field.tag == 273 || field.tag == 278 || field.tag == 279) imageLocationFields.add(field); //Stripe if (field.tag == 322 || field.tag == 323 || field.tag == 324 || field.tag == 325) imageLocationFields.add(field); //Tile } nextIFD = readEndianValue(in, 4, cursor.getEndian()); if (mainImage) return getPointers(imageLocationFields, cursor.getFile(), cursor.getEndian()); if (subIFDs == 1) { cursor.setPointer(subIFDsPointer); byte[] hash = readSubIFDirectory(cursor, in); if (hash != null) return hash; } else if (subIFDs > 1) { for (int j = 0; j < subIFDs; j++) { in.reset(); // in = new BufferedInputStream(new FileInputStream(cursor.getFile().toString())); if (!skipBytes(in, subIFDsPointer)) return null; if (!skipBytes(in, j * subIFDsPointerLength)) return null; cursor.setPointer(readEndianValue(in, subIFDsPointerLength, cursor.getEndian())); byte[] hash = readSubIFDirectory(cursor, in); if (hash != null) return hash; } } cursor.setPointer(nextIFD); return readIFDirectory(cursor, in); }
From source file:com.ckfinder.connector.utils.ImageUtils.java
/** * Uploads image and if the image size is larger than maximum allowed it * resizes the image./* w w w.j a v a 2s. co m*/ * * @param stream input stream. * @param file file name * @param fileName name of file * @param conf connector configuration * @throws IOException when error occurs. */ public static void createTmpThumb(final InputStream stream, final File file, final String fileName, final IConfiguration conf) throws IOException { BufferedInputStream bufferedIS = new BufferedInputStream(stream); bufferedIS.mark(Integer.MAX_VALUE); BufferedImage image = ImageIO.read(bufferedIS); if (image == null) { throw new IOException("Wrong file"); } Dimension dimension = createThumbDimension(image, conf.getImgWidth(), conf.getImgHeight()); if (image.getHeight() == dimension.height && image.getWidth() == dimension.width) { bufferedIS.reset(); writeUntouchedImage(bufferedIS, file); } else { resizeImage(image, dimension.width, dimension.height, conf.getImgQuality(), file); } stream.close(); }
From source file:com.linkedin.pinot.core.startree.StarTreeSerDe.java
/** * Utility method to StarTree version./*from www.j av a2 s .c om*/ * Presence of {@ref #MAGIC_MARKER} indicates on-heap format, while its * absence indicates on-heap format. * * @param bufferedInputStream * @return * @throws IOException */ public static StarTreeFormatVersion getStarTreeVersion(BufferedInputStream bufferedInputStream) throws IOException { byte[] magicBytes = new byte[MAGIC_MARKER_SIZE_IN_BYTES]; bufferedInputStream.mark(MAGIC_MARKER_SIZE_IN_BYTES); bufferedInputStream.read(magicBytes, 0, MAGIC_MARKER_SIZE_IN_BYTES); bufferedInputStream.reset(); LBufferAPI lBuffer = new LBuffer(MAGIC_MARKER_SIZE_IN_BYTES); lBuffer.readFrom(magicBytes, 0); long magicMarker = lBuffer.getLong(0); if (magicMarker == MAGIC_MARKER) { return StarTreeFormatVersion.OFF_HEAP; } else { return StarTreeFormatVersion.ON_HEAP; } }
From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java
/** * Uploads image and if the image size is larger than maximum allowed it resizes the image. * * @param stream input stream./*from www . ja v a 2s . c o m*/ * @param file file name * @param fileName name of file * @param conf connector configuration * @throws IOException when error occurs. */ public static void createTmpThumb(final InputStream stream, final File file, final String fileName, final IConfiguration conf) throws IOException { BufferedInputStream bufferedIS = new BufferedInputStream(stream); bufferedIS.mark(Integer.MAX_VALUE); BufferedImage image = ImageIO.read(bufferedIS); if (image == null) { throw new IOException("Wrong file"); } Dimension dimension = createThumbDimension(image, conf.getImgWidth(), conf.getImgHeight()); if (dimension.width == 0 || dimension.height == 0 || (image.getHeight() == dimension.height && image.getWidth() == dimension.width)) { bufferedIS.reset(); writeUntouchedImage(bufferedIS, file); } else { resizeImage(image, dimension.width, dimension.height, conf.getImgQuality(), file); } stream.close(); }