List of usage examples for java.io PushbackInputStream PushbackInputStream
public PushbackInputStream(InputStream in, int size)
PushbackInputStream
with a pushback buffer of the specified size
, and saves its argument, the input stream in
, for later use. From source file:com.qihang.winter.poi.excel.imports.ExcelImportServer.java
/** * Excel field Integer,Long,Double,Date,String,Boolean * * @param inputstream/*from w w w . j av a2s . com*/ * @param pojoClass * @param params * @return * @throws Exception */ public com.qihang.winter.poi.excel.entity.result.ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, com.qihang.winter.poi.excel.entity.ImportParams params) throws Exception { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Excel import start ,class is {}", pojoClass); } List<T> result = new ArrayList<T>(); Workbook book = null; boolean isXSSFWorkbook = true; if (!(inputstream.markSupported())) { inputstream = new PushbackInputStream(inputstream, 8); } if (POIFSFileSystem.hasPOIFSHeader(inputstream)) { book = new HSSFWorkbook(inputstream); isXSSFWorkbook = false; } else if (POIXMLDocument.hasOOXMLHeader(inputstream)) { book = new XSSFWorkbook(OPCPackage.open(inputstream)); } createErrorCellStyle(book); Map<String, PictureData> pictures; for (int i = 0; i < params.getSheetNum(); i++) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(" start to read excel by is ,startTime is {}", new Date().getTime()); } if (isXSSFWorkbook) { pictures = com.qihang.winter.poi.util.PoiPublicUtil .getSheetPictrues07((XSSFSheet) book.getSheetAt(i), (XSSFWorkbook) book); } else { pictures = com.qihang.winter.poi.util.PoiPublicUtil .getSheetPictrues03((HSSFSheet) book.getSheetAt(i), (HSSFWorkbook) book); } if (LOGGER.isDebugEnabled()) { LOGGER.debug(" end to read excel by is ,endTime is {}", new Date().getTime()); } result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); if (LOGGER.isDebugEnabled()) { LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime()); } } String excelName = ""; if (params.isNeedSave()) { excelName = saveThisExcel(params, pojoClass, isXSSFWorkbook, book); } return new com.qihang.winter.poi.excel.entity.result.ExcelImportResult(result, verfiyFail, book, excelName); }
From source file:org.methodize.nntprss.feed.Channel.java
/** * Retrieves the latest RSS doc from the remote site *//*from ww w . j a v a2 s . c o m*/ public synchronized void poll() { // Use method-level variable // Guard against change in history mid-poll polling = true; // boolean keepHistory = historical; long keepExpiration = expiration; lastPolled = new Date(); int statusCode = -1; HttpMethod method = null; String urlString = url.toString(); try { HttpClient httpClient = getHttpClient(); channelManager.configureHttpClient(httpClient); HttpResult result = null; try { connected = true; boolean redirected = false; int count = 0; do { URL currentUrl = new URL(urlString); method = new GetMethod(urlString); method.setRequestHeader("User-agent", AppConstants.getUserAgent()); method.setRequestHeader("Accept-Encoding", "gzip"); method.setFollowRedirects(false); method.setDoAuthentication(true); // ETag if (lastETag != null) { method.setRequestHeader("If-None-Match", lastETag); } // Last Modified if (lastModified != 0) { final String NAME = "If-Modified-Since"; //defend against such fun like net.freeroller.rickard got If-Modified-Since "Thu, 24 Aug 2028 12:29:54 GMT" if (lastModified < System.currentTimeMillis()) { final String DATE = httpDate.format(new Date(lastModified)); method.setRequestHeader(NAME, DATE); log.debug("channel " + this.name + " using " + NAME + " " + DATE); //ALEK } } method.setFollowRedirects(false); method.setDoAuthentication(true); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(currentUrl.getHost(), currentUrl.getPort(), currentUrl.getProtocol()); result = executeHttpRequest(httpClient, hostConfig, method); statusCode = result.getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { redirected = true; // Resolve against current URI - may be a relative URI try { urlString = new java.net.URI(urlString).resolve(result.getLocation()).toString(); } catch (URISyntaxException use) { // Fall back to just using location from result urlString = result.getLocation(); } if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY && channelManager.isObserveHttp301()) { try { url = new URL(urlString); if (log.isInfoEnabled()) { log.info("Channel = " + this.name + ", updated URL from HTTP Permanent Redirect"); } } catch (MalformedURLException mue) { // Ignore URL permanent redirect for now... } } } else { redirected = false; } // method.getResponseBody(); // method.releaseConnection(); count++; } while (count < 5 && redirected); } catch (HttpRecoverableException hre) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Temporary Http Problem - " + hre.getMessage()); } status = STATUS_CONNECTION_TIMEOUT; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (ConnectException ce) { // @TODO Might also be a connection refused - not only a timeout... if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Connection Timeout, skipping - " + ce.getMessage()); } status = STATUS_CONNECTION_TIMEOUT; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (UnknownHostException ue) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Unknown Host Exception, skipping"); } status = STATUS_UNKNOWN_HOST; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (NoRouteToHostException re) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - No Route To Host Exception, skipping"); } status = STATUS_NO_ROUTE_TO_HOST; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } catch (SocketException se) { // e.g. Network is unreachable if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - Socket Exception, skipping"); } status = STATUS_SOCKET_EXCEPTION; statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } // Only process if ok - if not ok (e.g. not modified), don't do anything if (connected && statusCode == HttpStatus.SC_OK) { PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(result.getResponse()), PUSHBACK_BUFFER_SIZE); skipBOM(pbis); BufferedInputStream bis = new BufferedInputStream(pbis); DocumentBuilder db = AppConstants.newDocumentBuilder(); try { Document rssDoc = null; if (!parseAtAllCost) { try { rssDoc = db.parse(bis); } catch (InternalError ie) { // Crimson library throws InternalErrors if (log.isDebugEnabled()) { log.debug("InternalError thrown by Crimson", ie); } throw new SAXException("InternalError thrown by Crimson: " + ie.getMessage()); } } else { // Parse-at-all-costs selected // Read in document to local array - may need to parse twice ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int bytesRead = bis.read(buf); while (bytesRead > -1) { if (bytesRead > 0) { bos.write(buf, 0, bytesRead); } bytesRead = bis.read(buf); } bos.flush(); bos.close(); byte[] rssDocBytes = bos.toByteArray(); try { // Try the XML document parser first - just in case // the doc is well-formed rssDoc = db.parse(new ByteArrayInputStream(rssDocBytes)); } catch (SAXParseException spe) { if (log.isDebugEnabled()) { log.debug("XML parse failed, trying tidy"); } // Fallback to parse-at-all-costs parser rssDoc = LooseParser.parse(new ByteArrayInputStream(rssDocBytes)); } } processChannelDocument(expiration, rssDoc); // Update last modified / etag from headers // lastETag = httpCon.getHeaderField("ETag"); // lastModified = httpCon.getHeaderFieldDate("Last-Modified", 0); Header hdrETag = method.getResponseHeader("ETag"); lastETag = hdrETag != null ? hdrETag.getValue() : null; Header hdrLastModified = method.getResponseHeader("Last-Modified"); lastModified = hdrLastModified != null ? parseHttpDate(hdrLastModified.getValue()) : 0; log.debug("channel " + this.name + " parsed Last-Modifed " + hdrLastModified + " to " + (lastModified != 0 ? "" + (new Date(lastModified)) : "" + lastModified)); //ALEK status = STATUS_OK; } catch (SAXParseException spe) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Error parsing RSS document - check feed"); } status = STATUS_INVALID_CONTENT; } bis.close(); // end if response code == HTTP_OK } else if (connected && statusCode == HttpStatus.SC_NOT_MODIFIED) { if (log.isDebugEnabled()) { log.debug("Channel=" + name + " - HTTP_NOT_MODIFIED, skipping"); } status = STATUS_OK; } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Proxy authentication required"); } status = STATUS_PROXY_AUTHENTICATION_REQUIRED; } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Authentication required"); } status = STATUS_USER_AUTHENTICATION_REQUIRED; } // Update channel in database... channelDAO.updateChannel(this); } catch (FileNotFoundException fnfe) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - File not found returned by web server - check feed"); } status = STATUS_NOT_FOUND; } catch (Exception e) { if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - Exception while polling channel", e); } } catch (NoClassDefFoundError ncdf) { // Throw if SSL / redirection to HTTPS if (log.isEnabledFor(Priority.WARN)) { log.warn("Channel=" + name + " - NoClassDefFound", ncdf); } } finally { connected = false; polling = false; } }
From source file:org.jeecgframework.poi.excel.imports.ExcelImportServer.java
/** * Excel field Integer,Long,Double,Date,String,Boolean * //from w ww. j ava 2 s . c o m * @param inputstream * @param pojoClass * @param params * @return * @throws Exception */ public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, ImportParams params) throws Exception { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Excel import start ,class is {}", pojoClass); } List<T> result = new ArrayList<T>(); Workbook book = null; boolean isXSSFWorkbook = true; if (!(inputstream.markSupported())) { inputstream = new PushbackInputStream(inputstream, 8); } if (POIFSFileSystem.hasPOIFSHeader(inputstream)) { book = new HSSFWorkbook(inputstream); isXSSFWorkbook = false; } else if (POIXMLDocument.hasOOXMLHeader(inputstream)) { book = new XSSFWorkbook(OPCPackage.open(inputstream)); } createErrorCellStyle(book); Map<String, PictureData> pictures; for (int i = 0; i < params.getSheetNum(); i++) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(" start to read excel by is ,startTime is {}", new Date().getTime()); } if (isXSSFWorkbook) { pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), (XSSFWorkbook) book); } else { pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), (HSSFWorkbook) book); } if (LOGGER.isDebugEnabled()) { LOGGER.debug(" end to read excel by is ,endTime is {}", new Date().getTime()); } result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); if (LOGGER.isDebugEnabled()) { LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime()); } } if (params.isNeedSave()) { saveThisExcel(params, pojoClass, isXSSFWorkbook, book); } return new ExcelImportResult(result, verfiyFail, book); }
From source file:cn.bzvs.excel.imports.ExcelImportServer.java
/** * Excel field Integer,Long,Double,Date,String,Boolean * //from www . j a v a 2s. com * @param inputstream * @param pojoClass * @param params * @return * @throws Exception */ public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, ImportParams params) throws Exception { List<T> result = new ArrayList<T>(); Workbook book = null; boolean isXSSFWorkbook = true; if (!(inputstream.markSupported())) { inputstream = new PushbackInputStream(inputstream, 8); } if (POIFSFileSystem.hasPOIFSHeader(inputstream)) { book = new HSSFWorkbook(inputstream); isXSSFWorkbook = false; } else if (POIXMLDocument.hasOOXMLHeader(inputstream)) { book = new XSSFWorkbook(OPCPackage.open(inputstream)); } createErrorCellStyle(book); Map<String, PictureData> pictures; for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex() + params.getSheetNum(); i++) { if (isXSSFWorkbook) { pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), (XSSFWorkbook) book); } else { pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), (HSSFWorkbook) book); } result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); } if (params.isNeedSave()) { saveThisExcel(params, pojoClass, isXSSFWorkbook, book); } return new ExcelImportResult(result, verfiyFail, book); }
From source file:org.deegree.framework.xml.XMLFragment.java
/** * Initializes the <code>XMLFragment</code> with the content from the given <code>InputStream</code>. Sets the * SystemId, too.//from w ww . j ava 2 s. com * * @param istream * @param systemId * cannot be null. This string should represent a URL that is related to the passed istream. If this URL * is not available or unknown, the string should contain the value of XMLFragment.DEFAULT_URL * @throws SAXException * @throws IOException * @throws XMLException * @throws NullPointerException */ public void load(InputStream istream, String systemId) throws SAXException, IOException, XMLException { PushbackInputStream pbis = new PushbackInputStream(istream, 1024); String encoding = readEncoding(pbis); if (LOG.isDebug()) { LOG.logDebug("Reading XMLFragment " + systemId + " with encoding ", encoding); } InputStreamReader isr = new InputStreamReader(pbis, encoding); load(isr, systemId); }
From source file:com.day.cq.wcm.foundation.impl.Rewriter.java
/** * Feed HTML into received over a {@link java.net.URLConnection} to an * HTML parser.// w w w . j a va 2 s . c o m * @param in input stream * @param contentType preferred content type * @param response servlet response * @throws java.io.IOException if an I/O error occurs */ private void rewriteHtml(InputStream in, String contentType, HttpServletResponse response) throws IOException { // Determine encoding if not specified String encoding = "8859_1"; int charsetIndex = contentType.indexOf("charset="); if (charsetIndex != -1) { encoding = contentType.substring(charsetIndex + "charset=".length()).trim(); } else { byte[] buf = new byte[2048]; int len = fillBuffer(in, buf); String scanned = EncodingScanner.scan(buf, 0, len); if (scanned != null) { encoding = scanned; contentType += "; charset=" + encoding; } PushbackInputStream pb = new PushbackInputStream(in, buf.length); pb.unread(buf, 0, len); in = pb; } // Set appropriate content type and get writer response.setContentType(contentType); PrintWriter writer = response.getWriter(); setWriter(writer); // Define tags that should be inspected HashSet<String> inclusionSet = new HashSet<String>(); inclusionSet.add("A"); inclusionSet.add("AREA"); inclusionSet.add("BASE"); inclusionSet.add("FORM"); inclusionSet.add("FRAME"); inclusionSet.add("IFRAME"); inclusionSet.add("IMG"); inclusionSet.add("INPUT"); inclusionSet.add("LINK"); inclusionSet.add("SCRIPT"); inclusionSet.add("TABLE"); inclusionSet.add("TD"); inclusionSet.add("TR"); inclusionSet.add("NOREWRITE"); HtmlParser parser = new HtmlParser(); parser.setTagInclusionSet(inclusionSet); parser.setDocumentHandler(this); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(in, encoding)); char buf[] = new char[2048]; int len; while ((len = reader.read(buf)) != -1) { parser.update(buf, 0, len); } parser.finished(); } finally { if (reader != null) { reader.close(); } } }
From source file:jef.tools.XMLUtils.java
/** * XML/*from w ww. j a va2 s.c o m*/ * * @param in * ? * @param charSet * ? * @param ignorComment * * @return Document. DOM * @throws SAXException * ? * @throws IOException * */ public static Document loadDocument(InputStream in, String charSet, boolean ignorComments, boolean namespaceAware) throws SAXException, IOException { DocumentBuilder db = REUSABLE_BUILDER.get().getDocumentBuilder(ignorComments, namespaceAware); InputSource is = null; // ????charset if (charSet == null) {// ?200??? byte[] buf = new byte[200]; PushbackInputStream pin = new PushbackInputStream(in, 200); in = pin; int len = pin.read(buf); if (len > 0) { pin.unread(buf, 0, len); charSet = getCharsetInXml(buf, len); } } if (charSet != null) { is = new InputSource(new XmlFixedReader(new InputStreamReader(in, charSet))); is.setEncoding(charSet); } else { // ? Reader reader = new InputStreamReader(in, "UTF-8");// XML???Reader??Reader?XML? is = new InputSource(new XmlFixedReader(reader)); } Document doc = db.parse(is); doc.setXmlStandalone(true);// True???standalone="no" return doc; }
From source file:org.apache.tika.parser.rtf.TextExtractor.java
public void extract(InputStream in) throws IOException, SAXException, TikaException { // in = new FilterInputStream(in) { // public int read() throws IOException { // int r = super.read(); // System.out.write(r); // System.out.flush(); // return r; // } // public int read(byte b[], int off, int len) throws IOException { // int r = super.read(b, off, len); // System.out.write(b, off, r); // System.out.flush(); // return r; // } // };/* ww w. j a va 2s.com*/ extract(new PushbackInputStream(in, 2)); }
From source file:org.methodize.nntprss.feed.Channel.java
/** * Simple channel validation - ensures URL * is valid, XML document is returned, and * document has an rss root element with a * version, or rdf root element, // www . jav a2 s .c o m */ public static boolean isValid(URL url) throws HttpUserException { boolean valid = false; try { // System.setProperty("networkaddress.cache.ttl", "0"); HttpClient client = new HttpClient(); ChannelManager.getChannelManager().configureHttpClient(client); if (url.getUserInfo() != null) { client.getState().setCredentials(null, null, new UsernamePasswordCredentials(URLDecoder.decode(url.getUserInfo()))); } String urlString = url.toString(); HttpMethod method = null; int count = 0; HttpResult result = null; int statusCode = HttpStatus.SC_OK; boolean redirected = false; do { method = new GetMethod(urlString); method.setRequestHeader("User-agent", AppConstants.getUserAgent()); method.setRequestHeader("Accept-Encoding", "gzip"); method.setFollowRedirects(false); method.setDoAuthentication(true); HostConfiguration hostConfiguration = client.getHostConfiguration(); URI hostURI = new URI(urlString); hostConfiguration.setHost(hostURI); result = executeHttpRequest(client, hostConfiguration, method); statusCode = result.getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { redirected = true; // Resolve against current URI - may be a relative URI try { urlString = new java.net.URI(urlString).resolve(result.getLocation()).toString(); } catch (URISyntaxException use) { // Fall back to just using location from result urlString = result.getLocation(); } } else { redirected = false; } // method.getResponseBody(); // method.releaseConnection(); count++; } while (count < 5 && redirected); // Only process if ok - if not ok (e.g. not modified), don't do anything if (statusCode == HttpStatus.SC_OK) { PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(result.getResponse()), PUSHBACK_BUFFER_SIZE); skipBOM(pbis); BufferedInputStream bis = new BufferedInputStream(pbis); DocumentBuilder db = AppConstants.newDocumentBuilder(); Document rssDoc = db.parse(bis); Element rootElm = rssDoc.getDocumentElement(); for (int i = 0; i < parsers.length; i++) { if (parsers[i].isParsable(rootElm)) { valid = true; break; } } } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { throw new HttpUserException(statusCode); } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { throw new HttpUserException(statusCode); } } catch (URIException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return valid; }
From source file:org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptInterpreter.java
protected Script compileScript(Context cx, Scriptable scope, Source src) throws Exception { PushbackInputStream is = new PushbackInputStream(src.getInputStream(), ENCODING_BUF_SIZE); try {// w w w . j a v a 2s . c o m String encoding = findEncoding(is); Reader reader = encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding); reader = new BufferedReader(reader); Script compiledScript = cx.compileReader(scope, reader, src.getURI(), 1, null); return compiledScript; } finally { is.close(); } }