List of usage examples for java.io Reader close
public abstract void close() throws IOException;
From source file:com.raphfrk.craftproxyclient.net.auth.AuthManager.java
private static JSONObject readAccessToken() { Reader reader = null; try {// ww w. java2s. co m reader = new BufferedReader( new InputStreamReader(new FileInputStream(tokenFilename), StandardCharsets.UTF_8)); JSONParser parser = new JSONParser(); return (JSONObject) parser.parse(reader); } catch (IOException | ParseException e) { } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } return null; }
From source file:Main.java
/** * Read and return the entire contents of the supplied {@link Reader}. This method always closes the reader when finished * reading./*from ww w. j a v a 2s. c om*/ * * @param reader the reader of the contents; may be null * @return the contents, or an empty string if the supplied reader is null * @throws IOException if there is an error reading the content */ public static String read(Reader reader) throws IOException { if (reader == null) return ""; StringBuilder sb = new StringBuilder(); boolean error = false; try { int numRead = 0; char[] buffer = new char[1024]; while ((numRead = reader.read(buffer)) > -1) { sb.append(buffer, 0, numRead); } } catch (IOException e) { error = true; // this error should be thrown, even if there is an error closing reader throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an error closing reader throw e; } finally { try { reader.close(); } catch (IOException e) { if (!error) throw e; } } return sb.toString(); }
From source file:net.duckling.ddl.util.FileUtil.java
/** * Makes a new temporary file and writes content into it. The temporary * file is created using <code>File.createTempFile()</code>, and the usual * semantics apply. The files are not deleted automatically in exit. * * @param content Initial content of the temporary file. * @param encoding Encoding to use./*from w w w. j a va2 s. c o m*/ * @return The handle to the new temporary file * @throws IOException If the content creation failed. * @see java.io.File#createTempFile(String,String,File) */ public static File newTmpFile(String content, String encoding) throws IOException { Writer out = null; Reader in = null; File f = null; try { f = File.createTempFile("jspwiki", null); in = new StringReader(content); out = new OutputStreamWriter(new FileOutputStream(f), encoding); copyContents(in, out); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } return f; }
From source file:net.ftb.util.OSUtils.java
public static StyleSheet makeStyleSheet(String name) { try {//from w w w .j av a 2s . c om StyleSheet sheet = new StyleSheet(); Reader reader = new InputStreamReader(System.class.getResourceAsStream("/css/" + name + ".css")); sheet.loadRules(reader, null); reader.close(); return sheet; } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:de.schildbach.wallet.litecoin.ExchangeRatesProvider.java
private static Double getLycBtcRate() { try {/*from www . java 2 s.c om*/ final URL URL = new URL("http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=177"); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); final JSONObject o = head.getJSONObject("return").getJSONObject("markets").getJSONObject("LYC"); final Double rate = o.getDouble("lasttradeprice"); if (rate != null) return rate; } finally { if (reader != null) reader.close(); } } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:com.microsoft.tfs.jni.helpers.FileCopyHelper.java
/** * Copies a text file and any extended attributes, converting charsets along * the way. If any provided charsets are <code>null</code>, the default * charset is assumed.//ww w . j av a 2 s . c om * * This method should not be used for binary files, instead * {@link FileCopyHelper#copy(String, String)} should be used. * * @param source * The path to the source file * @param sourceCharset * The charset of the source file (may be <code>null</code>) * @param destination * The path to the target * @param destinationCharset * The charset of the target (may be <code>null</code>) * @throws FileNotFoundException * If the source does not exist, or the target's parent folder does * not exist * @throws IOException * If there was an IOException reading the source or writing the * target */ public static void copyText(final String source, Charset sourceCharset, final String destination, Charset destinationCharset) throws FileNotFoundException, IOException, MalformedInputException, UnmappableCharacterException { Check.notNull(source, "source"); //$NON-NLS-1$ Check.notNull(destination, "destination"); //$NON-NLS-1$ if (sourceCharset == null) { sourceCharset = Charset.defaultCharset(); } if (destinationCharset == null) { destinationCharset = Charset.defaultCharset(); } Reader in = null; Writer out = null; try { final CharsetDecoder decoder = sourceCharset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); in = new BufferedReader(new InputStreamReader(new FileInputStream(source), decoder)); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), destinationCharset)); copy(in, out); } finally { if (in != null) { try { in.close(); } catch (final IOException e) { log.warn(MessageFormat.format("Could not close {0} for reading: {1}", source, e.getMessage())); //$NON-NLS-1$ } } if (out != null) { try { out.close(); } catch (final IOException e) { log.warn(MessageFormat.format("Could not close {0} for writing: {1}", destination, //$NON-NLS-1$ e.getMessage())); } } } copyAttributes(source, destination); }
From source file:org.coding.git.api.CodingNetConnection.java
@NotNull private static JsonElement parseResponse(@NotNull InputStream codingResponse) throws IOException { Reader reader = new InputStreamReader(codingResponse, CharsetToolkit.UTF8_CHARSET); try {//from www . j av a2s. c o m return new JsonParser().parse(reader); } catch (JsonParseException jse) { throw new CodingNetJsonException("Couldn't parse Coding response", jse); } finally { reader.close(); } }
From source file:net.ben.subsonic.androidapp.util.Util.java
public static void close(Reader reader) { try {/*from ww w .j av a 2 s. co m*/ if (reader != null) { reader.close(); } } catch (Throwable x) { // Ignored } }
From source file:com.sfxie.extension.spring4.properties.PropertiesLoaderUtils.java
/** * Actually load properties from the given EncodedResource into the given Properties instance. * @param props the Properties instance to load into * @param resource the resource to load from * @param persister the PropertiesPersister to use * @throws IOException in case of I/O errors *///w w w . j a v a 2s . c o m static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister) throws IOException { InputStream stream = null; Reader reader = null; try { String filename = resource.getResource().getFilename(); if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { stream = resource.getInputStream(); persister.loadFromXml(props, stream); } else if (resource.requiresReader()) { reader = resource.getReader(); persister.load(props, reader); } else { stream = resource.getInputStream(); persister.load(props, stream); } } finally { if (stream != null) { stream.close(); } if (reader != null) { reader.close(); } } }
From source file:com.fhc25.percepcion.osiris.mapviewer.common.restutils.RestClient.java
private static String convertGZIPStreamToString(GZIPInputStream is) { StringBuilder sb = new StringBuilder(); String line = null;/*from w ww .ja v a2s.co m*/ int lineCount = 0; try { Reader decoder = new InputStreamReader(is, "UTF-8"); BufferedReader reader = new BufferedReader(decoder); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); lineCount++; } if (lineCount == 1) { sb.deleteCharAt(sb.length() - 1); } is.close(); decoder.close(); reader.close(); } catch (IOException e) { } return sb.toString(); }