List of usage examples for java.io Reader close
public abstract void close() throws IOException;
From source file:it.cloudicaro.disit.kb.rdf.HttpUtil.java
public static String get(URL url, String accept, String user, String passwd) throws Exception { //System.out.println("GET "+url); HttpClient client = HttpClients.createDefault(); HttpGet request = new HttpGet(url.toURI()); if (accept != null) request.addHeader("Accept", accept); HttpClientContext context = HttpClientContext.create(); if (user != null && passwd != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd)); /*// Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth);*/ // Add AuthCache to the execution context context.setCredentialsProvider(credsProvider); }/*from ww w .j a v a 2s.com*/ HttpResponse response = client.execute(request, context); StatusLine s = response.getStatusLine(); int code = s.getStatusCode(); //System.out.println(code); if (code != 200) throw new Exception( "failed access to " + url.toString() + " code: " + code + " " + s.getReasonPhrase()); Reader reader = null; try { reader = new InputStreamReader(response.getEntity().getContent()); StringBuilder sb = new StringBuilder(); { int read; char[] cbuf = new char[1024]; while ((read = reader.read(cbuf)) != -1) { sb.append(cbuf, 0, read); } } return sb.toString(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.zotoh.core.io.StreamUte.java
/** * @param r//from w w w . j a va 2 s . c om * @return */ public static Reader close(Reader r) { try { if (r != null) r.close(); } catch (Throwable t) { } return null; }
From source file:edu.mayo.cts2.uriresolver.dao.DAOUtiltities.java
public static boolean importMySQLDataToH2Database(DataSource ds) { JdbcTemplate jdbcTemplateObject;//from w ww .j a va 2s . c om jdbcTemplateObject = new JdbcTemplate(ds); StringBuffer sqlBuffer = new StringBuffer(); BufferedReader bufferedReader = null; Reader reader = null; try { InputStream in = ResolveURI.class.getResourceAsStream("/uriresolver.sql"); reader = new InputStreamReader(in, "UTF-8"); bufferedReader = new BufferedReader(reader); while (bufferedReader.ready()) { String line = bufferedReader.readLine().trim(); if (isSQLCode(line)) { sqlBuffer.append(convertToH2(line)); } } bufferedReader.close(); reader.close(); } catch (IOException e) { logger.error("Error while importing data to in memory database: " + e.getMessage()); return false; } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (reader != null) { reader.close(); } } catch (IOException ex) { logger.error("Error while closing access to in memory database: " + ex.getMessage()); return true; } } jdbcTemplateObject.execute(sqlBuffer.toString()); return true; }
From source file:com.calebgomer.roadkill_reporter.AsyncReporter.java
private static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException { if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); }// ww w.j a v a2s .c o m InputStream instream = entity.getContent(); if (instream == null) { return ""; } if (entity.getContentLength() > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } String charset = getContentCharSet(entity); if (charset == null) { charset = HTTP.DEFAULT_CONTENT_CHARSET; } Reader reader = new InputStreamReader(instream, charset); StringBuilder buffer = new StringBuilder(); try { char[] tmp = new char[1024]; int l; while ((l = reader.read(tmp)) != -1) { buffer.append(tmp, 0, l); } } finally { reader.close(); } return buffer.toString(); }
From source file:com.github.magicsky.sya.checkers.TestSourceReader.java
public static int getLineNumber(int offset, Path fullPath) throws Exception { IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath); Reader reader = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset())); try {/*w w w . j a va2s. c o m*/ int line = 1; for (int i = 0; i < offset; i++) { int c = reader.read(); Assert.assertTrue(c >= 0); if (c == '\n') line++; } return line; } finally { reader.close(); } }
From source file:Main.java
/** * @param r Will be read and closed/*from w ww . ja va2s .com*/ * @return The contents of input */ public static String read(Reader r) { try { BufferedReader reader = r instanceof BufferedReader ? (BufferedReader) r : new BufferedReader(r); final int bufSize = 8192; // this is the default BufferredReader // buffer size StringBuilder sb = new StringBuilder(bufSize); char[] cbuf = new char[bufSize]; while (true) { int chars = reader.read(cbuf); if (chars == -1) break; sb.append(cbuf, 0, chars); } return sb.toString(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { r.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
From source file:org.crazydog.util.spring.FileCopyUtils.java
/** * Copy the contents of the given Reader to the given Writer. * Closes both when done./*from w w w . ja va 2 s .co m*/ * @param in the Reader to copy from * @param out the Writer to copy to * @return the number of characters copied * @throws IOException in case of I/O errors */ public static int copy(Reader in, Writer out) throws IOException { org.springframework.util.Assert.notNull(in, "No Reader specified"); org.springframework.util.Assert.notNull(out, "No Writer specified"); try { int byteCount = 0; char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); byteCount += bytesRead; } out.flush(); return byteCount; } finally { try { in.close(); } catch (IOException ex) { } try { out.close(); } catch (IOException ex) { } } }
From source file:com.mbeddr.pluginmanager.com.intellij.ide.plugins.RepositoryHelper.java
private static List<IdeaPluginDescriptor> parsePluginList(@NotNull Reader reader) throws IOException { try {/* w w w . j av a2 s . c o m*/ SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); RepositoryContentHandler handler = new RepositoryContentHandler(); parser.parse(new InputSource(reader), handler); return handler.getPluginsList(); } catch (ParserConfigurationException e) { throw new IOException(e); } catch (SAXException e) { throw new IOException(e); } finally { reader.close(); } }
From source file:com.jims.oauth2.common.utils.OAuthUtils.java
/** * Get the entity content as a String, using the provided default character set * if none is found in the entity./*w ww .j a v a2 s .co m*/ * If defaultCharset is null, the default "UTF-8" is used. * * @param is input stream to be saved as string * @param defaultCharset character set to be applied if none found in the entity * @return the entity content as a String * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE * @throws java.io.IOException if an error occurs reading the input stream */ public static String toString(final InputStream is, final String defaultCharset) throws IOException { if (is == null) { throw new IllegalArgumentException("InputStream may not be null"); } String charset = defaultCharset; if (charset == null) { charset = DEFAULT_CONTENT_CHARSET; } Reader reader = new InputStreamReader(is, charset); StringBuilder sb = new StringBuilder(); int l; try { char[] tmp = new char[4096]; while ((l = reader.read(tmp)) != -1) { sb.append(tmp, 0, l); } } finally { reader.close(); } return sb.toString(); }
From source file:com.github.sdbg.debug.core.internal.sourcemaps.SourceMap.java
public static SourceMap createFrom(IStorage storage) throws IOException, CoreException { Reader reader; if (storage instanceof IFile) { reader = new InputStreamReader(storage.getContents(), ((IFile) storage).getCharset()); } else {//from w w w. j av a2s.c o m reader = new InputStreamReader(storage.getContents()); } try { String contents = Streams.loadAndClose(reader); return createFrom(storage.getFullPath(), contents); } catch (JSONException e) { throw new IOException(e); } finally { reader.close(); } }