List of usage examples for java.io Reader close
public abstract void close() throws IOException;
From source file:com.prowidesoftware.swift.utils.Lib.java
/** * Read the content of the given reader into a string. * * @param reader the contents to read//from w w w.ja v a 2 s .com * @return the read content * @throws IOException * @since 7.7 */ public static String readReader(final Reader reader) throws IOException { if (reader == null) { return null; } final StringBuilder out = new StringBuilder(); try { int c = 0; while ((c = reader.read()) != -1) { out.append((char) c); } } finally { reader.close(); } return out.toString(); }
From source file:Main.java
private static CharSequence consume(URLConnection connection, int maxChars) throws IOException { String encoding = getEncoding(connection); StringBuilder out = new StringBuilder(); Reader in = null; try {//from ww w . ja va 2 s . co m in = new InputStreamReader(connection.getInputStream(), encoding); char[] buffer = new char[1024]; int charsRead; while (out.length() < maxChars && (charsRead = in.read(buffer)) > 0) { out.append(buffer, 0, charsRead); } } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { // continue } catch (NullPointerException ioe) { // continue } } } return out; }
From source file:Base64.java
private static char[] readChars(File file) { CharArrayWriter caw = new CharArrayWriter(); try {//ww w .j av a2 s . c om Reader fr = new FileReader(file); Reader in = new BufferedReader(fr); int count = 0; char[] buf = new char[16384]; while ((count = in.read(buf)) != -1) { if (count > 0) caw.write(buf, 0, count); } in.close(); } catch (Exception e) { e.printStackTrace(); } return caw.toCharArray(); }
From source file:com.moss.posixfifosockets.PosixFifoSocket.java
public static PosixFifoSocket newClientConnection(PosixFifoSocketAddress address, int timeoutMillis) throws IOException { final Log log = LogFactory.getLog(PosixFifoSocket.class); if (!address.controlPipe().exists()) { throw new IOException("There is no server at " + address); }/* w w w . j av a 2 s . c om*/ File in; File out; File control; long id; do { id = r.nextLong(); in = new File(address.socketsDir(), id + ".fifo.out"); out = new File(address.socketsDir(), id + ".fifo.in"); control = new File(address.socketsDir(), id + ".fifo.control"); } while (out.exists() || in.exists()); createFifo(in); createFifo(control); final String registrationString = "{" + id + "}"; if (log.isDebugEnabled()) log.debug("Sending registration " + registrationString); Writer w = new FileWriter(address.controlPipe()); w.write(registrationString); w.flush(); if (log.isDebugEnabled()) log.debug("Sent Registration " + registrationString); PosixFifoSocket socket = new PosixFifoSocket(id, in, out); Reader r = new FileReader(control); StringBuilder text = new StringBuilder(); for (int c = r.read(); c != -1 && c != '\n'; c = r.read()) { // READ UNTIL THE FIRST LINE BREAK text.append((char) c); } r.close(); if (!control.delete()) { throw new RuntimeException("Could not delete file:" + control.getAbsolutePath()); } if (!text.toString().equals("OK")) { throw new RuntimeException("Connection error: received \"" + text + "\""); } return socket; }
From source file:com.vuze.android.remote.rpc.RestJsonClient.java
private static void closeOnNewThread(final Reader reader) { Thread thread = new Thread(new Runnable() { @Override/* w w w . j av a 2s.c o m*/ public void run() { try { reader.close(); } catch (Throwable ignore) { } } }, "closeInputStream"); thread.setDaemon(true); thread.start(); }
From source file:com.comcast.cats.service.util.HttpClientUtil.java
/** * Helper method to clean up resources. This is important as the number of * open files/streams are limited in an operating system. * /*from w w w. j a va 2s .c o m*/ * @param bufferedReader * @param inputStreamReader * @param responseStream * @param httpMethod * @param client */ private static synchronized void cleanUp(Reader inputStreamReader, InputStream responseStream, HttpMethod httpMethod) { if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e) { logger.warn("Exception caught trying to close inputStreamReader", e); } } if (responseStream != null) { try { responseStream.close(); } catch (IOException e) { logger.warn("Exception caught trying to close responseStream", e); } } if (httpMethod != null) { httpMethod.releaseConnection(); } }
From source file:com.openedit.util.FileUtils.java
/** * @param inIn/*www . ja v a2s . co m*/ */ public static void safeClose(Reader inIn) { if (inIn != null) { try { inIn.close(); } catch (IOException ex) { log.error(ex); } } }
From source file:it.greenvulcano.gvesb.utils.ResultSetUtils.java
/** * Returns all values from the ResultSet as an XML. * For instance, if the ResultSet has 3 values, the returned XML will have following fields: * <RowSet> * <data> * <row> * <col>value1</col> * <col>value2</col> * <col>value3</col> * </row> * <row> * <col>value4</col> * <col>value5</col> * <col>value6</col> * </row> * ../*from ww w . ja va 2 s. c o m*/ * <row> * <col>valuex</col> * <col>valuey</col> * <col>valuez</col> * </row> * </data> * </RowSet> * @param rs * @return * @throws Exception */ public static Document getResultSetAsDOM(ResultSet rs) throws Exception { XMLUtils xml = XMLUtils.getParserInstance(); try { Document doc = xml.newDocument("RowSet"); Element docRoot = doc.getDocumentElement(); if (rs != null) { try { ResultSetMetaData metadata = rs.getMetaData(); Element data = null; Element row = null; Element col = null; Text text = null; String textVal = null; while (rs.next()) { boolean restartResultset = false; for (int j = 1; j <= metadata.getColumnCount() && !restartResultset; j++) { col = xml.createElement(doc, "col"); restartResultset = false; switch (metadata.getColumnType(j)) { case Types.CLOB: { Clob clob = rs.getClob(j); if (clob != null) { Reader is = clob.getCharacterStream(); StringWriter strW = new StringWriter(); IOUtils.copy(is, strW); is.close(); textVal = strW.toString(); } else { textVal = ""; } } break; case Types.BLOB: { Blob blob = rs.getBlob(j); if (blob != null) { InputStream is = blob.getBinaryStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); is.close(); try { byte[] buffer = Arrays.copyOf(baos.toByteArray(), (int) blob.length()); textVal = new String(Base64.getEncoder().encode(buffer)); } catch (SQLFeatureNotSupportedException exc) { textVal = new String(Base64.getEncoder().encode(baos.toByteArray())); } } else { textVal = ""; } } break; case -10: { // OracleTypes.CURSOR Object obj = rs.getObject(j); if (obj instanceof ResultSet) { rs = (ResultSet) obj; metadata = rs.getMetaData(); } restartResultset = true; } break; default: { textVal = rs.getString(j); if (textVal == null) { textVal = ""; } } } if (restartResultset) { continue; } if (row == null || j == 1) { row = xml.createElement(doc, "row"); } if (textVal != null) { text = doc.createTextNode(textVal); col.appendChild(text); } row.appendChild(col); } if (row != null) { if (data == null) { data = xml.createElement(doc, "data"); } data.appendChild(row); } } if (data != null) { docRoot.appendChild(data); } } finally { if (rs != null) { try { rs.close(); } catch (Exception exc) { // do nothing } rs = null; } } } return doc; } finally { XMLUtils.releaseParserInstance(xml); } }
From source file:Main.java
/** * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when * finished./* www . j a va2 s.com*/ * * @param input the content to write to the writer; may be null * @param writer the writer to which the content is to be written * @throws IOException * @throws IllegalArgumentException if the writer is null */ public static void write(Reader input, Writer writer) throws IOException { boolean error = false; try { if (input != null) { char[] buffer = new char[1024]; try { int numRead = 0; while ((numRead = input.read(buffer)) > -1) { writer.write(buffer, 0, numRead); } } finally { input.close(); } } } catch (IOException e) { error = true; // this error should be thrown, even if there is an error flushing/closing writer throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an error flushing/closing writer throw e; } finally { try { writer.flush(); } catch (IOException e) { if (!error) throw e; } finally { try { writer.close(); } catch (IOException e) { if (!error) throw e; } } } }
From source file:com.microsoft.tfs.client.eclipse.tpignore.TPIgnoreDocument.java
/** * Reads a .tpignore file from the given {@link IFile} using the * {@link IFile} encoding. The file's newline conventions are detected so * the document can be saved preserving them. * * @param file// w ww . j av a 2s. com * the {@link IFile} to read from (must not be <code>null</code>) * @return a new {@link TPIgnoreDocument} * @throws IOException * if an error occurred reading from the file * @throws CoreException * if an error occurred reading from the file */ public static TPIgnoreDocument read(final IFile file) throws IOException, CoreException { Check.notNull(file, "file"); //$NON-NLS-1$ if (file.exists() == false) { return new TPIgnoreDocument(NewlineUtils.PLATFORM_NEWLINE, null); } /* * Open a reader to detect the newline convention with. */ String newline = null; Reader reader = null; try { reader = new InputStreamReader(file.getContents(), file.getCharset()); newline = NewlineUtils.detectNewlineConvention(reader); } finally { if (reader != null) { try { reader.close(); } catch (final IOException e) { // ignore } reader = null; } } if (newline == null) { newline = NewlineUtils.PLATFORM_NEWLINE; } final TPIgnoreDocument ret = new TPIgnoreDocument(newline, file.getCharset()); BufferedReader br = null; try { reader = new InputStreamReader(file.getContents(), file.getCharset()); br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null) { ret.addLine(new Line(line)); } } finally { if (br != null) { try { br.close(); } catch (final IOException e) { // ignore } } } return ret; }