/** * Gets an array of PIM items from an encoded input stream and list. * * @param is data input stream * @param enc character encoding of stream data * @param list populate from list * @return array of PIM items * @throws PIMException if any error reading the PIM data * @throws UnsupportedEncodingException if encoding is not supported */ private PIMItem[] fromSerialFormat(InputStream is, String enc, PIMList list) throws PIMException, UnsupportedEncodingException { if (enc == null) { enc = "UTF-8" /* NO I18N */; } InputStream in = new MarkableInputStream(is); in.mark(Integer.MAX_VALUE); try { for (int i = 0; i < formats.length; i++) { try { PIMItem[] items = formats[i].decode(in, enc, list); if (items == null) { throw new PIMException("Empty stream or insufficient data"); } return items; } catch (UnsupportedPIMFormatException e) { in.reset(); in.mark(Integer.MAX_VALUE); } } throw new PIMException("Format is not recognized"); } catch (UnsupportedEncodingException e) { throw e; } catch (IOException e) { throw new PIMException(e.getMessage()); } }
@Override public int getNextEncoded(InputStream is) throws IOException { is.mark(4); byte[] bytes = new byte[4]; int count = 0; while (count < 4) { int nextByte = is.read(); if (nextByte == -1) { return -1; } bytes[count++] = (byte) nextByte; if (checkRange(bytes, count)) { return toInt(bytes, 0, count); } } is.reset(); // invalid code, try to determine number of bytes to consume bytes = new byte[4]; count = 0; while (count < 4) { is.mark(1); int nextByte = is.read(); if (nextByte == -1) { return -1; } bytes[count++] = (byte) nextByte; if (!checkPrefix(bytes, count)) { // todo 1 this is not correct - must pad to next range size is.reset(); count--; return toInt(bytes, 0, count); } } return -1; }
public Object engineRead() throws StreamParsingException { try { if (sData != null) { if (sDataObjectCount != sData.size()) { return getCertificate(); } else { sData = null; sDataObjectCount = 0; return null; } } currentStream.mark(10); int tag = currentStream.read(); if (tag == -1) { return null; } if (tag != 0x30) // assume ascii PEM encoded. { currentStream.reset(); return readPEMCertificate(currentStream); } else { currentStream.reset(); return readDERCertificate(currentStream); } } catch (Exception e) { throw new StreamParsingException(e.toString(), e); } }
/** * Reads a signature if it applies. Currently BIRT can recognize the following signatures: * * <ul> * <li>SIGNATURE_UTF_8 * <li>SIGNATURE_UNICODE_BIG * <li>SIGNATURE_UNICODE_LITTLE * <li>SIGNATURE_UCS4_BIG_8 * <li>SIGNATURE_UCS4_LITTLE * </ul> * * @param inputStream the input stream of the unicode file * @return the encoding signature from the unicode file. * @throws IOException if errors occur when reading bytes from the input stream */ public static String checkUTFSignature(InputStream inputStream) throws IOException { inputStream.mark(1024); int byte1 = inputStream.read(); int byte2 = inputStream.read(); if (byte1 == 0xFE && byte2 == 0xFF) { return SIGNATURE_UNICODE_BIG; } else if (byte1 == 0xFF && byte2 == 0xFE) { return SIGNATURE_UNICODE_LITTLE; } int byte3 = inputStream.read(); // check for UTF-8 byte order mark if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF) { return SIGNATURE_UTF_8; } int byte4 = inputStream.read(); if (byte1 == 0x00 && byte2 == 0x00 && byte3 == 0xFE && byte4 == 0xFF) { return SIGNATURE_UCS4_BIG; } else if (byte1 == 0x00 && byte2 == 0x00 && byte3 == 0xFF && byte4 == 0xFE) { return SIGNATURE_UCS4_LITTLE; } inputStream.reset(); return null; }
@SmallTest public void testRun() throws Exception { MemoryFile file = new MemoryFile("MemoryFileTest", 1000000); byte[] buffer = new byte[testString.length]; // check low level accessors file.writeBytes(testString, 0, 2000, testString.length); file.readBytes(buffer, 2000, 0, testString.length); compareBuffers(testString, buffer, testString.length); // check streams buffer = new byte[testString.length]; OutputStream os = file.getOutputStream(); os.write(testString); InputStream is = file.getInputStream(); is.mark(testString.length); is.read(buffer); compareBuffers(testString, buffer, testString.length); // test mark/reset buffer = new byte[testString.length]; is.reset(); is.read(buffer); compareBuffers(testString, buffer, testString.length); file.close(); }
/** * Method to verify if receieved stream has content or its empty * * @param stream Stream to check * @return verified stream * @throws IOException */ private InputStream verifyContentReceieved(InputStream stream) throws IOException { if (stream == null) { // Check if its null logger.debug("Request stream received as null"); return null; } else if (stream.markSupported()) { // Check stream supports mark/reset // mark() and read the first byte just to check stream.mark(1); final int bytesRead = stream.read(new byte[1]); if (bytesRead != -1) { // stream not empty stream.reset(); // reset the stream as if untouched return stream; } else { // stream empty logger.debug("Request received with empty body"); return null; } } else { // Panic! this stream does not support mark/reset, try with PushbackInputStream as a last // resort int bytesRead; PushbackInputStream pbs = new PushbackInputStream(stream); if ((bytesRead = pbs.read()) != -1) { // Contents detected, unread and return pbs.unread(bytesRead); return pbs; } else { // Empty stream detected logger.debug("Request received with empty body!"); return null; } } }
/** * Finds a suitable analyzer class for the data in this stream * * @param in The stream containing the data to analyze * @return the analyzer factory to use * @throws java.io.IOException if an error occurs while reading data from the stream */ public static FileAnalyzerFactory find(InputStream in) throws IOException { in.mark(8); byte[] content = new byte[8]; int len = in.read(content); in.reset(); if (len < 8) { /* * Need at least 4 bytes to perform magic string matching. */ if (len < 4) { return null; } content = Arrays.copyOf(content, len); } FileAnalyzerFactory factory = find(content); if (factory != null) { return factory; } for (FileAnalyzerFactory.Matcher matcher : matchers) { FileAnalyzerFactory fac = matcher.isMagic(content, in); if (fac != null) { return fac; } } return null; }
private void loadProps(Properties props, File file) throws IOException { InputStream fin = null; try { fin = new BufferedInputStream(new FileInputStream(file), 1); fin.mark(1); int c = fin.read(); fin.reset(); if (c == '#') { // uncompressed if (_log.shouldLog(Log.INFO)) _log.info("Loading uncompressed profile data from " + file.getName()); DataHelper.loadProps(props, fin); } else { // compressed (or corrupt...) if (_log.shouldLog(Log.INFO)) _log.info("Loading compressed profile data from " + file.getName()); DataHelper.loadProps(props, new GZIPInputStream(fin)); } } finally { try { if (fin != null) fin.close(); } catch (IOException e) { } } }
protected static int[] readHeader(InputStream stream, boolean isBinary, boolean close) throws IOException { if (isBinary) { DataInputStream dis = null; try { dis = new DataInputStream(stream); for (int i = 0; i < LocalFeatureList.BINARY_HEADER.length; i++) dis.readByte(); // read the header final int nItems = dis.readInt(); final int veclen = dis.readInt(); return new int[] {nItems, veclen, 8 + LocalFeatureList.BINARY_HEADER.length}; } finally { if (close && dis != null) try { dis.close(); } catch (final IOException e) { } } } else { InputStreamReader fr = null; BufferedReader br = null; int nlines = 1; final boolean isBuffered = stream.markSupported(); try { if (isBuffered) stream.mark(1024); fr = new InputStreamReader(stream); br = new BufferedReader(fr); // read the header line final String head = br.readLine().trim(); if (isBuffered) stream.reset(); final String[] h = head.split(" "); final int nItems = Integer.decode(h[0]); int veclen = 0; if (h.length > 1) { veclen = Integer.decode(h[1]); } else { veclen = Integer.decode(br.readLine().trim()); nlines++; } return new int[] {nItems, veclen, nlines}; } finally { if (close && br != null) try { br.close(); } catch (final IOException e) { } if (close && fr != null) try { fr.close(); } catch (final IOException e) { } } } }
/** * Parses a property list from an InputStream. It can either be in XML or binary format. * * @param is The InputStream delivering the property list data * @return The root object of the property list * @throws Exception If an error occurred while parsing. */ public static NSObject parse(InputStream is) throws Exception { if (is.markSupported()) { is.mark(10); byte[] magic = new byte[8]; is.read(magic); is.reset(); String magic_string = new String(magic); if (magic_string.startsWith("bplist00")) { return BinaryPropertyListParser.parse(is); } else if (magic_string.startsWith("<?xml")) { return XMLPropertyListParser.parse(is); } else { throw new UnsupportedOperationException( "The given data is neither a binary nor a XML property list. ASCII property lists are not supported."); } } else { if (is.available() > Runtime.getRuntime().freeMemory()) { throw new Exception( "To little heap space available! Wanted to read " + is.available() + " bytes, but only " + Runtime.getRuntime().freeMemory() + " are available."); } byte[] buf = new byte[is.available()]; is.read(buf); is.close(); return parse(buf); } }
/** * Create an compressor input stream from an input stream, auto-detecting the compressor type from * the first few bytes of the stream. The InputStream must support marks, like * BufferedInputStream. * * @param inputStream the input stream * @return the compressor input stream * @throws CompressorException if the compressor name is not known * @throws IllegalArgumentException if the stream is null or does not support mark * @since Commons Compress 1.1 */ @Override public CompressorInputStream createCompressorInputStream(final InputStream inputStream) throws CompressorException { checkNotNull(inputStream, "inputStream"); checkArgument(inputStream.markSupported(), "Mark is not supported."); final byte[] signature = new byte[SIGNATURE_LENGTH]; inputStream.mark(signature.length); try { int signatureLength = inputStream.read(signature); inputStream.reset(); if (BZip2CompressorInputStream.matches(signature, signatureLength)) { return createBZip2CompressorInputStream(inputStream); } else if (GzipCompressorInputStream.matches(signature, signatureLength)) { return createGZipCompressorInputStream(inputStream); } else if (XZCompressorInputStream.matches(signature, signatureLength)) { return createXZCompressorInputStream(inputStream); } else if (Pack200CompressorInputStream.matches(signature, signatureLength)) { return createPack200CompressorInputStream(inputStream); } else if (transparentSignatureDetection) { return createForwardingCompressorInputStream(inputStream); } } catch (IOException e) { throw new CompressorException("Failed to detect Compressor from InputStream.", e); } throw new CompressorException("No Compressor found for the stream signature."); }
private ConstraintMappingsType getValidationConfig(InputStream in, Unmarshaller unmarshaller) { ConstraintMappingsType constraintMappings; try { // check whether mark is supported, if so we can reset the stream in order to allow reuse of // Configuration boolean markSupported = in.markSupported(); if (markSupported) { in.mark(Integer.MAX_VALUE); } StreamSource stream = new StreamSource(new CloseIgnoringInputStream(in)); JAXBElement<ConstraintMappingsType> root = unmarshaller.unmarshal(stream, ConstraintMappingsType.class); constraintMappings = root.getValue(); if (markSupported) { try { in.reset(); } catch (IOException e) { log.debug("Unable to reset input stream."); } } } catch (JAXBException e) { throw log.getErrorParsingMappingFileException(e); } return constraintMappings; }
/** * Determine if the input stream is an HTTP response. If it is then the first 7 characters will be * 'HTTP/1.' as in HTTP/1.1 or HTTP/1.0. * * @return true is this is an HTTP response or false otherwise. * @throws IOException if an error occurs while reading the stream. */ protected boolean isHTTP() throws IOException { int c1; int c2; int c3; int c4; int c5; int c6; int c7; is.mark(10); c1 = is.read(); c2 = is.read(); c3 = is.read(); c4 = is.read(); c5 = is.read(); c6 = is.read(); c7 = is.read(); is.reset(); if ((c1 == 'H') && (c2 == 'T') && (c3 == 'T') && (c4 == 'P') && (c5 == '/') && (c6 == '1') && (c7 == '.')) { return true; } else { return false; } }
/** * Convert input stream to a string * * @param is - input stream to convert * @param bRepeatable - should we retrieve the content in such a way that it can be repeated? If * true and the stream doesn't support repeating this operation that the content will not be * touched * @return String - string value corresponding to the content of the input stream * @throws IOException - an error has occurred */ public static String convertStreamToString(InputStream is, boolean bRepeatable) throws IOException { String strReturn = ""; final int iReadLimit = 64 * 1024; if (is != null) { if ((!bRepeatable) || (is.markSupported())) { try { if ((bRepeatable) && (is.markSupported())) { is.mark(iReadLimit); } strReturn = new java.util.Scanner(is).useDelimiter("\\A").next(); } catch (java.util.NoSuchElementException e) { strReturn = ""; } finally { if ((bRepeatable) && (is.markSupported())) { is.reset(); } } } else { strReturn = "<Cannot access input stream of the request in repeatable way>"; } } return strReturn; }
public Charset detect(InputStream input, Metadata metadata) throws IOException { if (input == null) { return null; } input.mark(LOOKAHEAD); try { UniversalEncodingListener listener = new UniversalEncodingListener(metadata); byte[] b = new byte[BUFSIZE]; int n = 0; int m = input.read(b); while (m != -1 && n < LOOKAHEAD && !listener.isDone()) { n += m; listener.handleData(b, 0, m); m = input.read(b, 0, Math.min(b.length, LOOKAHEAD - n)); } return listener.dataEnd(); } catch (LinkageError e) { return null; // juniversalchardet is not available } finally { input.reset(); } }
/** * Returns true if the connection is functional. This uses a shameful hack to peek a byte from the * socket. */ boolean isStale() throws IOException { if (!isEligibleForRecycling()) { return true; } InputStream in = getInputStream(); if (in.available() > 0) { return false; } Socket socket = getSocket(); int soTimeout = socket.getSoTimeout(); try { socket.setSoTimeout(1); in.mark(1); int byteRead = in.read(); if (byteRead != -1) { in.reset(); return false; } return true; // the socket is reporting all data read; it's stale } catch (SocketTimeoutException e) { return false; // the connection is not stale; hooray } catch (IOException e) { return true; // the connection is stale, the read or soTimeout failed. } finally { socket.setSoTimeout(soTimeout); } }
public void unpack(ISOComponent c, InputStream in) throws IOException, ISOException { if (!(c instanceof ISOField)) throw new ISOException(c.getClass().getName() + " is not an ISOField"); boolean endFound = false; if (in.markSupported()) { in.mark(getMaxPackedLength()); } ByteBuffer buf = ByteBuffer.allocate(getMaxPackedLength()); for (int i = 0; i < getMaxPackedLength() && in.available() > 0; i++) { byte dataByte = (byte) in.read(); if (dataByte == terminator) { endFound = true; break; } else { buf.put(dataByte); } } if (endFound) { byte[] data = byteBufferToBytes(buf); c.setValue(data); } else { if (in.markSupported()) { in.reset(); } throw new ISOException("Terminating Backslash does not exist"); } }
private static String determineEncoding(InputStream in) throws IOException { String encoding = "UTF-8"; in.mark(4); byte[] check = new byte[4]; int size = in.read(check); if (size == 2) { if (((check[0] & 0xFF) == 0x00 && (check[1] & 0xFF) != 0x00) || ((check[0] & 0xFF) == 0xFE && (check[1] & 0xFF) == 0xFF)) { encoding = "UTF-16BE"; } else if (((check[0] & 0xFF) != 0x00 && (check[1] & 0xFF) == 0x00) || ((check[0] & 0xFF) == 0xFF && (check[1] & 0xFF) == 0xFE)) { encoding = "UTF-16LE"; } } else if (size == 4) { if (((check[0] & 0xFF) == 0x00 && (check[1] & 0xFF) == 0x00)) { encoding = "UTF-32BE"; } else if (((check[2] & 0xFF) == 0x00 && (check[3] & 0xFF) == 0x00)) { encoding = "UTF-32LE"; } else if (((check[0] & 0xFF) == 0x00 && (check[1] & 0xFF) != 0x00) || ((check[0] & 0xFF) == 0xFE && (check[1] & 0xFF) == 0xFF)) { encoding = "UTF-16BE"; } else if (((check[0] & 0xFF) != 0x00 && (check[1] & 0xFF) == 0x00) || ((check[0] & 0xFF) == 0xFF && (check[1] & 0xFF) == 0xFE)) { encoding = "UTF-16LE"; } } in.reset(); return encoding; }
protected ImageContainer doInBackground(Void... params) { InputStream inputStreamWrapper = new BufferedInputStream(inputStream); try { inputStreamWrapper.mark(inputStreamWrapper.available()); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStreamWrapper, null, options); if (options.outHeight != -1 && options.outWidth != 1) { options.inSampleSize = ImageHandler.calculateInSampleSize(options, this.width, this.height); inputStreamWrapper.reset(); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeStream(inputStreamWrapper, null, options); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); return new ImageContainer(ImageStatusCode.FILE_ENCODED, bitmap, stream.toByteArray()); } else { return new ImageContainer(ImageStatusCode.NOT_AN_IMAGE, null, null); } } catch (IOException e) { e.printStackTrace(); return new ImageContainer(ImageStatusCode.FILE_NOT_FOUND, null, null); } }
public static File createTestFile(long largerThanMB) throws IOException { File outputFile = new File("target/test/csv/perftest.csv"); if (outputFile.exists()) { return outputFile; } 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; }
public static boolean isID3v2StartPosition(InputStream input) throws IOException { input.mark(3); try { return input.read() == 'I' && input.read() == 'D' && input.read() == '3'; } finally { input.reset(); } }
@Override public void mark(int readlimit) { tryEnsureOpen(); if (is != null) { is.mark(readlimit); } }
/** * Constructs a new repeatable cipher input stream using the specified InputStream as the source * data, and the CipherFactory for building Cipher objects. * * @param input The original, unencrypted data stream. This stream should be markable/resetable in * order for this class to work correctly. * @param cipherFactory The factory used for creating identical cipher objects when this stream is * reset and a new CipherInputStream is needed. */ public RepeatableCipherInputStream(InputStream input, CipherFactory cipherFactory) { super(createCipherInputStream(input, cipherFactory)); this.unencryptedDataStream = input; this.cipherFactory = cipherFactory; // Mark the beginning of the data stream so we can reset back to it unencryptedDataStream.mark(-1); }
public static boolean isCRAMFile(final InputStream stream) throws IOException { stream.mark(4); final int buffSize = CramHeader.MAGIC.length; final byte[] buffer = new byte[buffSize]; readBytes(stream, buffer, 0, buffSize); stream.reset(); return Arrays.equals(buffer, CramHeader.MAGIC); }
public X509Credential(InputStream certInputStream, InputStream keyInputStream) throws CredentialException { if (certInputStream.markSupported()) { certInputStream.mark(BUFFER_SIZE); } loadKey(keyInputStream); loadCertificate(certInputStream); validateCredential(); }
private String readLine(final byte[] hdrbuf) throws IOException { bin.mark(hdrbuf.length); final int cnt = bin.read(hdrbuf); int lf = 0; while (lf < cnt && hdrbuf[lf] != '\n') lf++; bin.reset(); bin.skip(lf); if (lf < cnt && hdrbuf[lf] == '\n') bin.skip(1); return RawParseUtils.decode(Constants.CHARSET, hdrbuf, 0, lf); }
@Override public Long get(InputStream is) throws IOException { is.mark(this.offset + SIZE); is.skip(this.offset); byte bytes[] = new byte[SIZE]; int count = is.read(bytes); is.reset(); if (count != 0) throw new EOFException(); BigInteger bigInt = new BigInteger(bytes); return bigInt.longValue(); }
/** Return the AudioFileFormat from the given InputStream. */ public AudioFileFormat getAudioFileFormat(InputStream inputStream) throws UnsupportedAudioFileException, IOException { if (TDebug.TraceAudioFileReader) TDebug.out("getAudioFileFormat(InputStream inputStream)"); try { if (!inputStream.markSupported()) inputStream = new BufferedInputStream(inputStream); inputStream.mark(MARK_LIMIT); return getAudioFileFormat(inputStream, AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED); } finally { inputStream.reset(); } }
public MediaType detect(InputStream input, Metadata metadata) throws IOException { // Check if we have access to the document if (input == null) { return MediaType.OCTET_STREAM; } // If this is a TikaInputStream wrapping an already // parsed NPOIFileSystem/DirectoryNode, just get the // names from the root: TikaInputStream tis = TikaInputStream.cast(input); Set<String> names = null; if (tis != null) { Object container = tis.getOpenContainer(); if (container instanceof NPOIFSFileSystem) { names = getTopLevelNames(((NPOIFSFileSystem) container).getRoot()); } else if (container instanceof DirectoryNode) { names = getTopLevelNames((DirectoryNode) container); } } if (names == null) { // Check if the document starts with the OLE header input.mark(8); try { if (input.read() != 0xd0 || input.read() != 0xcf || input.read() != 0x11 || input.read() != 0xe0 || input.read() != 0xa1 || input.read() != 0xb1 || input.read() != 0x1a || input.read() != 0xe1) { return MediaType.OCTET_STREAM; } } finally { input.reset(); } } // We can only detect the exact type when given a TikaInputStream if (names == null && tis != null) { // Look for known top level entry names to detect the document type names = getTopLevelNames(tis); } // Detect based on the names (as available) if (tis != null && tis.getOpenContainer() != null && tis.getOpenContainer() instanceof NPOIFSFileSystem) { return detect(names, ((NPOIFSFileSystem) tis.getOpenContainer()).getRoot()); } else { return detect(names, null); } }
@Override public void addFile( long companyId, long repositoryId, String fileName, boolean validateFileExtension, InputStream is) throws PortalException { if (is instanceof ByteArrayFileInputStream) { ByteArrayFileInputStream byteArrayFileInputStream = (ByteArrayFileInputStream) is; File file = byteArrayFileInputStream.getFile(); addFile(companyId, repositoryId, fileName, validateFileExtension, file); return; } validate(fileName, validateFileExtension); if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED || !AntivirusScannerUtil.isActive()) { store.addFile(companyId, repositoryId, fileName, is); } else { File tempFile = null; try { if (is.markSupported()) { is.mark(is.available() + 1); AntivirusScannerUtil.scan(is); is.reset(); store.addFile(companyId, repositoryId, fileName, is); } else { tempFile = FileUtil.createTempFile(); FileUtil.write(tempFile, is); AntivirusScannerUtil.scan(tempFile); store.addFile(companyId, repositoryId, fileName, tempFile); } } catch (IOException ioe) { throw new SystemException("Unable to scan file " + fileName, ioe); } finally { if (tempFile != null) { tempFile.delete(); } } } }