List of usage examples for java.io Reader close
public abstract void close() throws IOException;
From source file:edu.uci.ics.jung.io.PartitionDecorationReader.java
/** * Decorates vertices in the specified partition with typed count data. * The data must be contained in a text file in the following format: * // ww w .j a v a 2s. c o m * <pre> * vid_1 type_1 count_1 * vid_2 type_2 count_2 * ... * </pre> * * <p>where <code>count_i</code> (an integer value) represents * the number of elements of * type <code>type_i</code> possessed by the vertex with ID * <code>vid_i</code> (as defined by <code>BipartiteGraphReader.load()</code>) * for the <code>i</code>th line in the file.</p> * * <p>For example, the vertices might represent authors, the type might * represent a topic, and the count might represent the number of * papers that the specified author had written on that topic.<p> * * <p>If <code>normalize</code> is <code>true</code>, then the * count data will be scaled so that the counts for * each vertex will sum to 1. (In this case, each vertex must have * a positive total count value.) * * <p>The end of the file may be artificially set by putting the string * <code>end_of_file</code> on a line by itself.</p> * * @return the total number of types observed * @param bg the bipartite graph whose vertices are to be decorated * @param count_reader the reader containing the decoration data * @param partition the partition whose decorations are specified by this file * @param count_key the user key for the decorations * @param copyact the copy action for the decorations */ public static int loadCounts(Graph bg, Reader count_reader, Predicate partition, Object count_key, UserData.CopyAction copyact) { StringLabeller id_label = StringLabeller.getLabeller(bg, partition); Set types = new HashSet(); try { BufferedReader br = new BufferedReader(count_reader); while (br.ready()) { String curLine = br.readLine(); if (curLine == null || curLine.equals("end_of_file")) break; if (curLine.trim().length() == 0) continue; StringTokenizer st = new StringTokenizer(curLine); String entity_id = st.nextToken(); String type_id = st.nextToken(); Integer count = new Integer(st.nextToken()); types.add(type_id); Vertex v = id_label.getVertex(entity_id); if (v == null) throw new IllegalArgumentException("Unrecognized vertex " + entity_id); Map count_map = (Map) v.getUserDatum(count_key); if (count_map == null) { count_map = new HashMap(); v.addUserDatum(count_key, count_map, copyact); } count_map.put(type_id, count); } br.close(); count_reader.close(); } catch (IOException ioe) { throw new FatalException("Error in loading counts from " + count_reader); } return types.size(); }
From source file:org.ambientdynamix.update.contextplugin.NexusSource.java
/** * Extracts plug-ins from a Nexus repo using the (deprecated) date_index search method. * * @param repo RepositoryInfo configured with a NEXUS_INDEX_SOURCE type and associated url. * @return A List of DiscoveredContextPlugin(s) * @throws Exception/*from www.jav a2 s . com*/ * @See https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/rest.data_index.html */ private List<DiscoveredContextPlugin> getPlugsFromIndexSearch(RepositoryInfo repo, PLATFORM platform, VersionInfo platformVersion, VersionInfo frameworkVersion) throws Exception { Serializer serializer = new Persister(); String repoId = Utils.getArgumentValueFromUrl(repo.getUrl(), "repositoryId"); String xmlData = retrieve(repo.getUrl()); Reader reader = new StringReader(xmlData); List<DiscoveredContextPlugin> plugs = new ArrayList<>(); NexusSearchResultsBinder searchResults = serializer.read(NexusSearchResultsBinder.class, reader, false); if (searchResults != null && searchResults.totalCount > 0) { // Gather the metadata artifacts from the search results List<NexusArtifactBinder> metadata = new ArrayList<>(); for (NexusArtifactBinder art : searchResults.data) { if (repoId != null && art.repoId.equalsIgnoreCase(repoId)) { if (art.classifier != null && art.classifier.equalsIgnoreCase("metadata")) if (!metadata.contains(art)) metadata.add(art); else Log.w(TAG, "Already stored metadata for ID: " + art.getId()); } } // Next gather the plugin artifacts for each associated metadata artifact Map<NexusArtifactBinder, NexusArtifactBinder> metaMap = new HashMap<>(); for (NexusArtifactBinder meta : metadata) { for (NexusArtifactBinder art : searchResults.data) { if (repoId != null && art.repoId.equalsIgnoreCase(repoId)) { if (art.classifier != null && art.classifier.equalsIgnoreCase("plugin") && art.getId().equalsIgnoreCase(meta.getId()) && art.version.equalsIgnoreCase(meta.version)) { if (!metaMap.keySet().contains(meta)) metaMap.put(meta, art); else Log.w(TAG, "Already stored plugin for ID: " + meta.getId()); break; } } } } /* * Now create the plug-ins using the metadata and the download URIs from the artifacts */ for (NexusArtifactBinder metaBinder : metaMap.keySet()) { /* * Steps: 1. Download the metadata file 2. De-serialize it to create a template 3. Use the values from * the template to construct a ContextPlugin 4. Use the associated pluging uri to update the * ContextPlugin download link */ NexusArtifactBinder plugBinder = metaMap.get(metaBinder); if (plugBinder != null) { try { String metaXml = retrieve(metaBinder.resourceURI); Reader metaReader = new StringReader(metaXml); ContextPluginBinder metaResult = serializer.read(ContextPluginBinder.class, metaReader, false); metaReader.close(); if (metaResult.repoType.equals("nexus-rest")) { metaResult.installUrl = plugBinder.resourceURI; metaResult.updateUrl = plugBinder.artifactLink; } DiscoveredContextPlugin plug = metaResult.createDiscoveredPlugin(repo); if (UpdateUtils.checkCompatibility(plug.getContextPlugin(), platform, platformVersion, frameworkVersion)) { if (!plugs.contains(plug)) { plugs.add(plug); } else { Log.w(TAG, "Plugin was already added... skipping"); } } } catch (Exception e) { Log.w(TAG, "Problem parsing plugin metadata for: " + metaBinder.getId() + " | " + e.toString()); } } else { Log.w(TAG, "Could not find plugBinder for: " + metaBinder); } } } return plugs; }
From source file:com.neocotic.blingaer.link.LinkServiceImpl.java
@Override public String fetchTitle(String url) throws IllegalArgumentException { LOG.trace("Entered method - fetchTitle"); if (validateUrl(url).hasErrors()) { throw new IllegalArgumentException("Invalid URL"); }//from w w w . j av a 2s . c o m String content = null; Reader reader = null; try { reader = new InputStreamReader(new URL(url).openStream()); content = IOUtils.toString(reader); reader.close(); } catch (Exception e) { LOG.debug("Swallowed exception", e); } finally { IOUtils.closeQuietly(reader); } // Use URL if content could not be retrieved if (content == null) { return url; } // Attempts to extract charset declared in the HTML String charSet = null; Matcher matcher = Pattern.compile("/<meta[^>]*?charset=([^>]*?)\\/?>/is").matcher(content); while (matcher.find()) { charSet = StringUtils.trimToNull(matcher.group()); break; } // Attempts to extract title declared in the HTML String title = null; matcher = Pattern.compile("/<title>(.*?)<\\/title>/is").matcher(content); while (matcher.find()) { title = StringUtils.trimToNull(matcher.group()); break; } // If title wasn't found uses an error message (if applicable) or URL if (title == null) { title = (content.indexOf("Error") == 0) ? content : url; } // Attempts charset conversion to UTF-8 try { if (charSet == null) { title = new String(title.getBytes(), "UTF-8"); } else { title = new String(title.getBytes(charSet), "UTF-8"); } } catch (UnsupportedEncodingException e) { LOG.debug("Swallowed exception", e); } // Removes HTML entities title = StringEscapeUtils.unescapeHtml4(title); // Removes all tags title = title.replaceAll("<(.|\\n)*?>", StringUtils.EMPTY); LOG.trace("Exiting method - fetchTitle"); return title; }
From source file:co.cask.cdap.gateway.handlers.metrics.MetricsDiscoveryQueryTest.java
@Test public void testDiscoverMetrics() throws Exception { JsonArray expected = new JsonArray(); JsonArray readContexts = children(node("app", "WCount", children(//from w ww. j av a2 s . com node("flow", "WCounter", children(node("flowlet", "counter"), node("flowlet", "splitter"))), node("flow", "WordCounter", children(node("flowlet", "splitter"))), node("mapreduce", "ClassicWordCount", children(node("mapreduceTask", "mappers"), node("mapreduceTask", "reducers"))), node("procedure", "RCounts")))); JsonObject reads = new JsonObject(); reads.addProperty("metric", "reads"); reads.add("contexts", readContexts); expected.add(reads); HttpResponse response = doGet("/v2/metrics/available/apps/WCount"); Reader reader = new InputStreamReader(response.getEntity().getContent(), Charsets.UTF_8); try { Assert.assertEquals("did not return 200 status.", HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); JsonArray json = new Gson().fromJson(reader, JsonArray.class); Assert.assertEquals(expected, json); } finally { reader.close(); } }
From source file:ReaderUtil.java
/** * Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when done. *//*from w w w .j a v a2 s. c o m*/ public String readToString(Reader in) throws IOException { StringBuffer buf = null; try { buf = pool.borrowObject(); for (int c = in.read(); c != -1; c = in.read()) { buf.append((char) c); } return buf.toString(); } catch (IOException e) { throw e; } catch (Exception e) { throw new RuntimeException("Unable to borrow buffer from pool" + e.toString()); } finally { try { in.close(); } catch (Exception e) { // ignored } try { if (null != buf) { pool.returnObject(buf); } } catch (Exception e) { // ignored } } }
From source file:architecture.common.license.io.LicenseReader.java
private String decodeToXml(Reader in) throws IOException { StringBuffer text = new StringBuffer(); char buf[] = new char[1024]; int len;//from w w w . j av a 2 s . c o m while ((len = in.read(buf)) >= 0) { int j = 0; while (j < len) { char ch = buf[j]; if (Character.isLetter(ch) || Character.isDigit(ch) || ch == '+' || ch == '/' || ch == '=') text.append(ch); j++; } } in.close(); String xml = new String( Base64.decodeBase64(text.toString().getBytes(ApplicationConstants.DEFAULT_CHAR_ENCODING))); if (!assertionsDisabled && !text.toString().matches("^[^\\s]*$")) { throw new AssertionError(); } else { log.debug(xml); return xml; } }
From source file:com.rimbit.android_wallet.ExchangeRatesProvider.java
private static String getURLResult(URL url, final String userAgent) { HttpURLConnection connection = null; Reader reader = null; try {/*w w w. ja v a2 s . c om*/ connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS); connection.addRequestProperty("User-Agent", userAgent); connection.connect(); final int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream is = new BufferedInputStream(connection.getInputStream(), 1024); reader = new InputStreamReader(is, Constants.UTF_8); final StringBuilder content = new StringBuilder(); Io.copy(reader, content); return content.toString(); } else { log.warn("http status {} when fetching exchange rates from {}", responseCode, RIMBIT_EXPLORER_URL); } } catch (final Exception x) { log.warn("problem fetching exchange rates from " + RIMBIT_EXPLORER_URL, x); } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } if (connection != null) connection.disconnect(); } return null; }
From source file:co.cask.cdap.gateway.handlers.metrics.MetricsQueryTest.java
@Test public void testingInvalidUserServiceMetrics() throws Exception { MetricsCollector collector = collectionService.getCollector(MetricsScope.USER, "WordCount.s.InvalidService.CountRunnable", "0"); collector.increment("reads", 1); // Wait for collection to happen TimeUnit.SECONDS.sleep(2);//from ww w .j a v a 2 s . c o m String runnableRequest = "/user/apps/WordCount/service/InvalidService/runnables/CountRunnable/reads?aggregate=true"; HttpResponse response = doGet("/v2/metrics" + runnableRequest); Reader reader = new InputStreamReader(response.getEntity().getContent(), Charsets.UTF_8); try { Assert.assertEquals("GET " + runnableRequest + " did not return 404 status.", HttpStatus.SC_NOT_FOUND, response.getStatusLine().getStatusCode()); } finally { reader.close(); } }
From source file:fedora.server.storage.translation.AtomDOSerializer.java
/** * AUDIT datastream is rebuilt from the latest in-memory audit trail which * is a separate array list in the DigitalObject class. Audit trail * datastream re-created from audit records. There is only ONE version of * the audit trail datastream/*ww w. j a v a 2 s . c o m*/ * * @throws ObjectIntegrityException * @throws StreamIOException */ private void addAuditDatastream() throws ObjectIntegrityException, StreamIOException { if (m_obj.getAuditRecords().size() == 0) { return; } String dsId = m_pid.toURI() + "/AUDIT"; String dsvId = dsId + "/" + DateUtility.convertDateToString(m_obj.getCreateDate()); Entry dsEntry = m_feed.addEntry(); dsEntry.setId(dsId); dsEntry.setTitle("AUDIT"); dsEntry.setUpdated(m_obj.getCreateDate()); // create date? dsEntry.addCategory(MODEL.STATE.uri, "A", null); dsEntry.addCategory(MODEL.CONTROL_GROUP.uri, "X", null); dsEntry.addCategory(MODEL.VERSIONABLE.uri, "false", null); dsEntry.addLink(dsvId, Link.REL_ALTERNATE); Entry dsvEntry = m_feed.addEntry(); dsvEntry.setId(dsvId); dsvEntry.setTitle("AUDIT.0"); dsvEntry.setUpdated(m_obj.getCreateDate()); ThreadHelper.addInReplyTo(dsvEntry, m_pid.toURI() + "/AUDIT"); dsvEntry.addCategory(MODEL.FORMAT_URI.uri, AUDIT1_0.uri, null); dsvEntry.addCategory(MODEL.LABEL.uri, "Audit Trail for this object", null); if (m_format.equals(ATOM_ZIP1_1)) { String name = "AUDIT.0.xml"; try { m_zout.putNextEntry(new ZipEntry(name)); Reader r = new StringReader(DOTranslationUtility.getAuditTrail(m_obj)); IOUtils.copy(r, m_zout, m_encoding); m_zout.closeEntry(); r.close(); } catch (IOException e) { throw new StreamIOException(e.getMessage(), e); } IRI iri = new IRI(name); dsvEntry.setSummary("AUDIT.0"); dsvEntry.setContent(iri, "text/xml"); } else { dsvEntry.setContent(DOTranslationUtility.getAuditTrail(m_obj), "text/xml"); } }
From source file:org.ambientdynamix.update.contextplugin.NexusSource.java
/** * Extracts plug-ins from a Nexus repo using the Lucene search method. * * @param repo RepositoryInfo configured with a NEXUS_LUCENE_SOURCE type and associated url. * @return A List of DiscoveredContextPlugin(s) * @throws Exception//from w ww . j a v a 2s .c o m * @See https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/rest.lucene.search.html */ private List<DiscoveredContextPlugin> getPlugsFromLuceneSearch(RepositoryInfo repo, PLATFORM platform, VersionInfo platformVersion, VersionInfo frameworkVersion) throws Exception { Serializer serializer = new Persister(); String xmlData = retrieve(repo.getUrl()); Reader reader = new StringReader(xmlData); List<DiscoveredContextPlugin> plugs = new ArrayList<>(); NexusMavenLuceneResultsBinder searchResults = serializer.read(NexusMavenLuceneResultsBinder.class, reader, false); NexusNGRepositoryDetail repoDetails = searchResults.repoDetails.repoDetail; if (searchResults != null && searchResults.totalCount > 0) { // Grab Nexus' base url (may not be at {url}/nexus/) String nexusBaseUrl = repoDetails.repositoryURL.substring(0, repoDetails.repositoryURL.indexOf("/service/local/")); for (NexusNGArtifactBinder art : searchResults.data) { // Each art should be a plug-in, if it contains a plugin and metadata classifier for (NexusNgArtifactHitBinder hit : art.artifactHits) { DiscoveredContextPlugin plug = null; // Search for the metadata for (NexusArtifactLinkBinder link : hit.artifactLinks) { if (link.isMetadata()) { try { // Grab the metadata String metaXml = retrieve(makeRetrievalUrl(nexusBaseUrl, art, hit, link)); Reader metaReader = new StringReader(metaXml); ContextPluginBinder metaResult = serializer.read(ContextPluginBinder.class, metaReader, false); metaReader.close(); DiscoveredContextPlugin tmp = metaResult.createDiscoveredPlugin(repo); if (UpdateUtils.checkCompatibility(tmp.getContextPlugin(), platform, platformVersion, frameworkVersion)) { plug = tmp; break; } } catch (Exception e) { Log.w(TAG, "Problem creating plugin for " + art + ", exception was: " + e.toString()); } } } // If we created a plugin, create the download link to the JAR if (plug != null) { boolean found = false; for (NexusArtifactLinkBinder link : hit.artifactLinks) { if (link.isPlugin()) { plug.getContextPlugin() .setInstallUrl(makeRetrievalUrl(nexusBaseUrl, art, hit, link)); if (!plugs.contains(plug)) { plugs.add(plug); break; } else { Log.w(TAG, "Plugin was already added... skipping"); } } } if (!found) { Log.w(TAG, "Could not find JAR for " + art); } } } } } return plugs; }