List of usage examples for java.io InputStream mark
public synchronized void mark(int readlimit)
From source file:com.alibaba.simpleimage.util.ImageUtils.java
public static boolean isBMP(InputStream in) throws IOException { if (!in.markSupported()) { throw new IllegalArgumentException("Input stream must support mark"); }// w ww. j a v a 2 s.co m byte[] b = new byte[2]; try { in.mark(30); in.read(b); } finally { in.reset(); } return (b[0] == 0x42) && (b[1] == 0x4d); }
From source file:com.alibaba.simpleimage.util.ImageUtils.java
public static boolean isPNG(InputStream in) throws IOException { if (!in.markSupported()) { throw new IllegalArgumentException("Input stream must support mark"); }//from w w w . j a v a2 s .com byte[] b = new byte[8]; try { in.mark(30); in.read(b); } finally { in.reset(); } return (b[0] == (byte) 137 && b[1] == (byte) 80 && b[2] == (byte) 78 && b[3] == (byte) 71 && b[4] == (byte) 13 && b[5] == (byte) 10 && b[6] == (byte) 26 && b[7] == (byte) 10); }
From source file:com.alibaba.simpleimage.util.ImageUtils.java
public static boolean isGIF(InputStream in) throws IOException { if (!in.markSupported()) { throw new IllegalArgumentException("Input stream must support mark"); }//www .j a v a 2 s . c o m byte[] b = new byte[6]; try { in.mark(30); in.read(b); } finally { in.reset(); } return b[0] == 'G' && b[1] == 'I' && b[2] == 'F' && b[3] == '8' && (b[4] == '7' || b[4] == '9') && b[5] == 'a'; }
From source file:com.adguard.filter.proxy.ProtocolDetector.java
/** * Detects protocol of the input stream/*w w w . j av a 2 s . c om*/ * * @param inputStream Stream to check * @return Protocol detected * @throws IOException If something is wrong with the input stream * @throws IllegalArgumentException If stream does not support "mark" */ public static Protocol detect(InputStream inputStream) throws IOException { if (!inputStream.markSupported()) { throw new IllegalArgumentException("Stream must support mark for protocol detection!"); } byte[] packet = new byte[32]; inputStream.mark(packet.length); int read = inputStream.read(packet); inputStream.reset(); if (read < packet.length) { return Protocol.TOO_SMALL; } else if (ProtocolDetector.isHttpProtocol(packet)) { return Protocol.HTTP; } else if (ProtocolDetector.isTlsProtocol(packet)) { return Protocol.TLS; } else { return Protocol.OTHER; } }
From source file:com.norconex.commons.lang.io.IOUtil.java
/** * Gets and resets the specified number of bytes from the input stream. * @param is input stream/*w w w. ja va2s . c o m*/ * @param qty number of bytes to read * @return byte array of length matching the quantity requested * @throws IOException if {@link InputStream#markSupported()} returns false */ public static byte[] borrowBytes(InputStream is, int qty) throws IOException { if (is == null) { throw new IllegalArgumentException("Input stream cannot be null."); } else if (!is.markSupported()) { throw new IllegalArgumentException("Input stream must support mark."); } is.mark(qty); byte[] bytes = new byte[qty]; is.read(bytes); is.reset(); return bytes; }
From source file:com.adguard.filter.proxy.ProtocolDetector.java
/** * Peeks first N non-empty bytes from a peeking stream. * * @param inputStream Input stream/* www .j a v a 2 s .c o m*/ * @param length Length of the non-empty fragment we need * @return First non-empty bytes read or null if there is no fragment of the specified length */ private static byte[] peekFirstNotEmptyBytes(InputStream inputStream, int length) throws IOException { final int markLength = 32 * 1024; int bytesRead = 0; int bytesFound = 0; byte[] firstBytes = new byte[length]; byte[] buffer = new byte[512]; inputStream.mark(markLength); try { while (bytesRead < markLength) { int bytesMarked = IOUtils.read(inputStream, buffer); // if stream is finished and we still didn't read the array of length we need if (bytesMarked <= 0) return null; bytesRead += bytesMarked; for (int i = 0; i < bytesMarked; i++) { byte b = buffer[i]; boolean empty = IoUtils.isEmptyOrWhitespace(b) || IoUtils.isBomByte(b); // bytesFound > 0 means that we have already found first non-empty character // we also ignore zero byte to handle UTF-16 if ((!empty || bytesFound > 0) && b != 0) { firstBytes[bytesFound] = b; bytesFound++; } if (bytesFound == firstBytes.length) { // Found enough bytes return firstBytes; } } // End of stream and we have not found enough bytes if (bytesMarked < buffer.length) { return null; } } } finally { inputStream.reset(); } return null; }
From source file:Main.java
/** * Decodes from an input stream that optimally supports mark and reset * operations. If a maximum width and/or height are specified then the * passed stream must support mark and reset so that the bitmap can be down * sampled properly. If the width and/or height are specified and the input * stream does not support mark and reset, then an IllegalArgumentException * will be throw./* www. j av a 2 s . c o m*/ */ public static Bitmap decodeSampledBitmapFromStream(InputStream inputStream, int width, int height) { if ((width != 0 || height != 0) && !inputStream.markSupported()) { throw new IllegalArgumentException( "Bitmap decoding requires an input stream that supports " + "mark and reset"); } // Set a mark for reset. Since we have no idea of the size of this // image, just set the maximum value possible. inputStream.mark(Integer.MAX_VALUE); // First decode with inJustDecodeBounds=true to check dimensions. final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); // Reset the stream for the actual decoding phase. try { inputStream.reset(); } catch (IOException e) { Log.e(TAG, "Failed to reset input stream during bitmap decoding"); return null; } // If either width or height is passed in as 0, then use the actual // stored image dimension. if (width == 0) { width = options.outWidth; } if (height == 0) { height = options.outHeight; } // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, width, height); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(inputStream, null, options); }
From source file:org.apache.nifi.controller.TemplateManager.java
private static boolean isMoreData(final InputStream in) throws IOException { in.mark(1); final int nextByte = in.read(); if (nextByte == -1) { return false; }/*w w w . j a v a 2s. c o m*/ in.reset(); return true; }
From source file:org.apache.sling.distribution.packaging.impl.DistributionPackageUtils.java
public static void readInfo(InputStream inputStream, Map<String, Object> info) { try {//from w w w.jav a2 s . co m int size = META_START.getBytes("UTF-8").length; inputStream.mark(size); byte[] buffer = new byte[size]; int bytesRead = inputStream.read(buffer, 0, size); String s = new String(buffer, "UTF-8"); if (bytesRead > 0 && buffer[0] > 0 && META_START.equals(s)) { ObjectInputStream stream = getSafeObjectInputStream(inputStream); HashMap<String, Object> map = (HashMap<String, Object>) stream.readObject(); info.putAll(map); } else { inputStream.reset(); } } catch (IOException e) { log.error("Cannot read stream info", e); } catch (ClassNotFoundException e) { log.error("Cannot read stream info", e); } }
From source file:parquet.compat.test.Utils.java
public static File createTestFile(long largerThanMB) throws IOException { File outputFile = new File("target/test/csv/perftest.csv"); if (outputFile.exists()) { return outputFile; }/*from w w w. j a v a2s . co m*/ File toCopy = new File("../parquet-testdata/tpch/customer.csv"); FileUtils.copyFile(new File("../parquet-testdata/tpch/customer.schema"), new File("target/test/csv/perftest.schema")); OutputStream output = null; InputStream input = null; try { output = new BufferedOutputStream(new FileOutputStream(outputFile, true)); input = new BufferedInputStream(new FileInputStream(toCopy)); input.mark(Integer.MAX_VALUE); while (outputFile.length() <= largerThanMB * 1024 * 1024) { //appendFile(output, toCopy); IOUtils.copy(input, output); input.reset(); } } finally { closeQuietly(input); closeQuietly(output); } return outputFile; }