List of usage examples for java.io InputStreamReader close
public void close() throws IOException
From source file:com.norconex.collector.http.robot.impl.DefaultRobotsTxtProvider.java
@Override public synchronized RobotsTxt getRobotsTxt(DefaultHttpClient httpClient, String url) { String baseURL = getBaseURL(url); RobotsTxt robotsTxt = robotsTxtCache.get(baseURL); if (robotsTxt != null) { return robotsTxt; }/*from w w w.ja va2 s . com*/ String userAgent = ((String) httpClient.getParams().getParameter(CoreProtocolPNames.USER_AGENT)) .toLowerCase(); String robotsURL = baseURL + "/robots.txt"; HttpGet method = new HttpGet(robotsURL); List<String> sitemapLocations = new ArrayList<String>(); List<IURLFilter> filters = new ArrayList<IURLFilter>(); MutableFloat crawlDelay = new MutableFloat(RobotsTxt.UNSPECIFIED_CRAWL_DELAY); try { HttpResponse response = httpClient.execute(method); InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); BufferedReader br = new BufferedReader(isr); boolean agentAlreadyMatched = false; boolean doneWithAgent = false; String line; while ((line = br.readLine()) != null) { String key = line.replaceFirst("(.*?)(:.*)", "$1").trim(); String value = line.replaceFirst("(.*?:)(.*)", "$2").trim(); if ("sitemap".equalsIgnoreCase(key)) { sitemapLocations.add(value); } if (!doneWithAgent) { if ("user-agent".equalsIgnoreCase(key)) { if (matchesUserAgent(userAgent, value)) { agentAlreadyMatched = true; } else if (agentAlreadyMatched) { doneWithAgent = true; } } if (agentAlreadyMatched) { parseAgentLines(baseURL, filters, crawlDelay, key, value); } } } isr.close(); } catch (Exception e) { LOG.warn("Not able to obtain robots.txt at: " + robotsURL, e); } robotsTxt = new RobotsTxt(filters.toArray(new IURLFilter[] {}), crawlDelay.floatValue()); robotsTxtCache.put(baseURL, robotsTxt); return robotsTxt; }
From source file:org.ballerinalang.test.context.ServerInstance.java
/** * Reading output from input stream./*from w w w. j a va 2 s.c o m*/ * * @param inputStream input steam of a process * @return the output string generated by java process */ private String readProcessInputStream(InputStream inputStream) { InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; StringBuilder stringBuilder = new StringBuilder(); try { inputStreamReader = new InputStreamReader(inputStream, Charset.defaultCharset()); bufferedReader = new BufferedReader(inputStreamReader); int x; while ((x = bufferedReader.read()) != -1) { stringBuilder.append((char) x); } } catch (Exception ex) { log.error("Error reading process id", ex); } finally { if (inputStreamReader != null) { try { inputStream.close(); inputStreamReader.close(); } catch (IOException e) { log.error("Error occurred while closing stream: " + e.getMessage(), e); } } if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { log.error("Error occurred while closing stream: " + e.getMessage(), e); } } } return stringBuilder.toString(); }
From source file:org.botlibre.util.Utils.java
/** * Get the contents of the stream to a .self file and parse it. *//*www . j a v a 2 s .c o m*/ public static String loadTextFile(InputStream stream, String encoding, int maxSize) { if (encoding.trim().isEmpty()) { encoding = "UTF-8"; } // FEFF because this is the Unicode char represented by the UTF-8 byte order mark (EF BB BF). String UTF8_BOM = "\uFEFF"; StringWriter writer = new StringWriter(); InputStreamReader reader = null; try { reader = new InputStreamReader(stream, encoding); int size = 0; int next = reader.read(); boolean first = true; while (next >= 0) { if (first && next == UTF8_BOM.charAt(0)) { // skip } else { writer.write(next); } next = reader.read(); if (size > maxSize) { throw new BotException( "File size limit exceeded: " + size + " > " + maxSize + " token: " + next); } size++; } } catch (IOException exception) { throw new BotException("IO Error: " + exception.getMessage(), exception); } finally { if (reader != null) { try { reader.close(); } catch (IOException ignore) { } } if (stream != null) { try { stream.close(); } catch (IOException ignore) { } } } return writer.toString(); }
From source file:be.docarch.odt2braille.PEF.java
private int countPages(File brailleFile, Volume volume) throws IOException { int pageCount = 0; FileInputStream fileInputStream = new FileInputStream(brailleFile); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); String brfInput = IOUtils.toString(inputStreamReader); try {/* ww w .j a v a 2 s. co m*/ Matcher matcher = Pattern.compile("(\f|\uE000)").matcher(brfInput); pageCount = 1; while (matcher.find()) { char ch = brfInput.charAt(matcher.start()); if (ch == '\f') { pageCount++; } else { if (volume.getTableOfContent()) { pageCount--; } break; } } } finally { if (inputStreamReader != null) { inputStreamReader.close(); fileInputStream.close(); } } return pageCount; }
From source file:org.yamj.api.common.http.AbstractPoolingHttpClient.java
protected DigestedResponse readContent(final HttpResponse response, final Charset charset) throws IOException { StringWriter content = new StringWriter(SW_BUFFER_10K); InputStream is = response.getEntity().getContent(); InputStreamReader isr = null; BufferedReader br = null;/* w ww . ja va 2s . c o m*/ final DigestedResponse digestedResponse = new DigestedResponse(); digestedResponse.setStatusCode(response.getStatusLine().getStatusCode()); try { if (charset == null) { isr = new InputStreamReader(is, Charset.defaultCharset()); } else { isr = new InputStreamReader(is, charset); } br = new BufferedReader(isr); String line = br.readLine(); while (line != null) { content.write(line); line = br.readLine(); } content.flush(); digestedResponse.setContent(content.toString()); return digestedResponse; } finally { if (br != null) { try { br.close(); } catch (IOException ex) { LOG.trace("Failed to close BufferedReader", ex); } } if (isr != null) { try { isr.close(); } catch (IOException ex) { LOG.trace("Failed to close InputStreamReader", ex); } } try { content.close(); } catch (IOException ex) { LOG.trace("Failed to close StringWriter", ex); } try { is.close(); } catch (IOException ex) { LOG.trace("Failed to close InputStream", ex); } } }
From source file:com.lenovo.tensorhusky.common.utils.LinuxResourceCalculatorPlugin.java
/** * Read /proc/cpuinfo, parse and calculate CPU information *//* w w w.j a va 2 s .c om*/ private void readProcCpuInfoFile() { // This directory needs to be read only once if (readCpuInfoFile) { return; } // Read "/proc/cpuinfo" file BufferedReader in = null; InputStreamReader fReader = null; try { fReader = new InputStreamReader(new FileInputStream(procfsCpuFile), Charset.forName("UTF-8")); in = new BufferedReader(fReader); } catch (FileNotFoundException f) { // shouldn't happen.... return; } Matcher mat = null; try { numProcessors = 0; String str = in.readLine(); while (str != null) { mat = PROCESSOR_FORMAT.matcher(str); if (mat.find()) { numProcessors++; } mat = FREQUENCY_FORMAT.matcher(str); if (mat.find()) { cpuFrequency = (long) (Double.parseDouble(mat.group(1)) * 1000); // kHz } str = in.readLine(); } } catch (IOException io) { LOG.warn("Error reading the stream " + io); } finally { // Close the streams try { fReader.close(); try { in.close(); } catch (IOException i) { LOG.warn("Error closing the stream " + in); } } catch (IOException i) { LOG.warn("Error closing the stream " + fReader); } } readCpuInfoFile = true; }
From source file:com.marklogic.contentpump.DelimitedTextInputFormat.java
public List<InputSplit> getSplits(JobContext job) throws IOException { boolean delimSplit = isSplitInput(job.getConfiguration()); //if delimSplit is true, size of each split is determined by //Math.max(minSize, Math.min(maxSize, blockSize)) in FileInputFormat List<InputSplit> splits = super.getSplits(job); if (!delimSplit) { return splits; }/* w w w . j a va2 s . c o m*/ if (splits.size() >= SPLIT_COUNT_LIMIT) { //if #splits > 1 million, there is enough parallelism //therefore no point to split LOG.warn("Exceeding SPLIT_COUNT_LIMIT, input_split is off:" + SPLIT_COUNT_LIMIT); DefaultStringifier.store(job.getConfiguration(), false, ConfigConstants.CONF_SPLIT_INPUT); return splits; } // add header info into splits List<InputSplit> populatedSplits = new ArrayList<InputSplit>(); LOG.info(splits.size() + " DelimitedSplits generated"); Configuration conf = job.getConfiguration(); char delimiter = 0; ArrayList<Text> hlist = new ArrayList<Text>(); for (InputSplit file : splits) { FileSplit fsplit = ((FileSplit) file); Path path = fsplit.getPath(); FileSystem fs = path.getFileSystem(conf); if (fsplit.getStart() == 0) { // parse the inSplit, get the header FSDataInputStream fileIn = fs.open(path); String delimStr = conf.get(ConfigConstants.CONF_DELIMITER, ConfigConstants.DEFAULT_DELIMITER); if (delimStr.length() == 1) { delimiter = delimStr.charAt(0); } else { LOG.error("Incorrect delimitor: " + delimiter + ". Expects single character."); } String encoding = conf.get(MarkLogicConstants.OUTPUT_CONTENT_ENCODING, MarkLogicConstants.DEFAULT_OUTPUT_CONTENT_ENCODING); InputStreamReader instream = new InputStreamReader(fileIn, encoding); CSVParser parser = new CSVParser(instream, CSVParserFormatter.getFormat(delimiter, DelimitedTextReader.encapsulator, true, true)); Iterator<CSVRecord> it = parser.iterator(); String[] header = null; if (it.hasNext()) { CSVRecord record = (CSVRecord) it.next(); Iterator<String> recordIterator = record.iterator(); int recordSize = record.size(); header = new String[recordSize]; for (int i = 0; i < recordSize; i++) { if (recordIterator.hasNext()) { header[i] = (String) recordIterator.next(); } else { throw new IOException("Record size doesn't match the real size"); } } EncodingUtil.handleBOMUTF8(header, 0); hlist.clear(); for (String s : header) { hlist.add(new Text(s)); } } instream.close(); } DelimitedSplit ds = new DelimitedSplit(new TextArrayWritable(hlist.toArray(new Text[hlist.size()])), path, fsplit.getStart(), fsplit.getLength(), fsplit.getLocations()); populatedSplits.add(ds); } return populatedSplits; }
From source file:org.coltram.nsd.bonjour.LocalExposedBonjourService.java
public void run() { try {// w w w. jav a2 s. co m log.fine( "starting coltram bonjour service on port " + serverSocket.getLocalPort() + " id:" + serviceId); while (!threadStopper) { Socket socket = serverSocket.accept(); log.fine("socket accepted on " + socket.getPort()); InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String message; while (true) { message = bufferedReader.readLine(); log.fine("BSS receive on port " + socket.getPort() + " :" + message); if (message == null) { break; } //log.info("relaying bonjour message to:" + serviceId + " -- " + message); log.fine("receiveBonjourMsg " + message); JSONObject msg = new JSONObject(message); String purpose = msg.getString("purpose"); if ("subscribe".equals(purpose)) { subscribe(msg); } else if ("unsubscribe".equals(purpose)) { unsubscribe(msg); } else { notifyListeners(message); } } bufferedReader.close(); inputStreamReader.close(); socket.close(); } log.finer("bonjour service actually stopped"); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.opendatakit.utilities.ODKFileUtils.java
/** * TODO this is almost identical to writeConfiguredOdkAppVersion * * @param appName the app name * @param odkAppVersionFile the file that contains the installed app version * @param apkVersion the version to match against * @return whether the passed apk version matches the version in the file *//*from w w w . j a va 2 s .c o m*/ private static boolean checkOdkAppVersion(String appName, String odkAppVersionFile, String apkVersion) { File versionFile = new File(getDataFolder(appName), odkAppVersionFile); if (!versionFile.exists()) { return false; } String versionLine = null; FileInputStream fs = null; InputStreamReader r = null; BufferedReader br = null; try { fs = new FileInputStream(versionFile); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { r = new InputStreamReader(fs, StandardCharsets.UTF_8); } else { //noinspection deprecation r = new InputStreamReader(fs, Charsets.UTF_8); } br = new BufferedReader(r); versionLine = br.readLine(); } catch (IOException e) { WebLogger.getLogger(appName).printStackTrace(e); return false; } finally { if (br != null) { try { br.close(); } catch (IOException e) { WebLogger.getLogger(appName).printStackTrace(e); } } if (r != null) { try { r.close(); } catch (IOException e) { WebLogger.getLogger(appName).printStackTrace(e); } } try { if (fs != null) { fs.close(); } } catch (IOException e) { WebLogger.getLogger(appName).printStackTrace(e); } } String[] versionRange = versionLine.split(";"); for (String version : versionRange) { if (version.trim().equals(apkVersion)) { return true; } } return false; }