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 stream into a string. * * @param stream the contents to read//from w w w .j av a 2 s.c o m * @param enconding optional encoding to use, if null "UTF-8" is used as default * @return the read content * @throws IOException * @since 7.7 */ public static String readStream(final InputStream stream, final String enconding) throws IOException { if (stream == null) { return null; } final StringBuilder out = new StringBuilder(); final String enc = enconding != null ? enconding : "UTF-8"; final Reader in = new InputStreamReader(stream, enc); try { int c = 0; while ((c = in.read()) != -1) { out.append((char) c); } } finally { in.close(); } return out.toString(); }
From source file:Main.java
public static String descomprimeComGZIP(byte[] bytes, String encoding) { String decompressedString = ""; try {//from w w w . j a v a 2s. c o m ByteArrayInputStream bais = new ByteArrayInputStream(bytes); GZIPInputStream gzip = new GZIPInputStream(bais); Reader reader = new InputStreamReader(gzip, encoding); StringBuffer sbuf = new StringBuffer(); char[] buffer = new char[32 * 1024]; int nread; while ((nread = reader.read(buffer)) >= 0) { sbuf.append(buffer, 0, nread); } decompressedString = sbuf.toString(); reader.close(); } catch (Exception e) { e.printStackTrace(); } return decompressedString; }
From source file:com.nebhale.jsonpath.testutils.JsonUtils.java
private static String readFile(String file) throws IOException { StringBuilder sb = new StringBuilder(); Reader in = null; try {// ww w. j ava 2 s. c o m in = new FileReader(file); char[] buffer = new char[8192]; int length; while ((length = in.read(buffer)) != -1) { sb.append(buffer, 0, length); } } finally { if (in != null) { in.close(); } } return sb.toString(); }
From source file:ca.simplegames.micro.utils.IO.java
/** * Close the given stream if the stream is not null. * * @param s The stream/* w ww . java 2s .co m*/ */ public static void close(Reader s) { if (s != null) { try { s.close(); } catch (Exception e) { log.error("Error closing reader: " + e.getMessage()); } } }
From source file:Main.java
static String readFully(Reader reader) throws IOException { try {//from w w w. ja v a 2 s .c om StringWriter writer = new StringWriter(); char[] buffer = new char[1024]; int count; while ((count = reader.read(buffer)) != -1) { writer.write(buffer, 0, count); } return writer.toString(); } finally { reader.close(); } }
From source file:Main.java
public static <T> T xml2Obj(String xmlString, Class<T> clazz) throws RuntimeException { T c = null;/*from w w w. j av a2 s. c om*/ Reader reader = null; try { reader = new StringReader(xmlString); Unmarshaller unMarshaller = JAXBContext.newInstance(clazz).createUnmarshaller(); c = (T) unMarshaller.unmarshal(reader); } catch (JAXBException ex) { throw new RuntimeException(ex); } finally { try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } return c; }
From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java
public static void close(Reader reader) { try {// w w w . j a v a2 s .co m if (reader != null) reader.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
/** * Descomprime uma string utilizando o GZIP * // w ww .j a v a 2s .co m * @param str * @param encoding * @return */ public static String gzipDecompress(byte[] bytes, String encoding) { String decompressedString = ""; try { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); GZIPInputStream gzip = new GZIPInputStream(bais); Reader reader = new InputStreamReader(gzip, encoding); StringBuffer sbuf = new StringBuffer(); char[] buffer = new char[32 * 1024]; int nread; while ((nread = reader.read(buffer)) >= 0) { sbuf.append(buffer, 0, nread); } decompressedString = sbuf.toString(); reader.close(); } catch (Exception e) { e.printStackTrace(); } return decompressedString; }
From source file:Main.java
/** * Descomprime uma string utilizando o GZIP * // www .j av a 2 s. c o m * @param str * @param encoding * @return */ public static String gzipDecompressString(String str, String encoding) { String decompressedString = ""; try { byte[] bytes = Base64.decodeBase64(str.getBytes(encoding)); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); GZIPInputStream gzip = new GZIPInputStream(bais); Reader reader = new InputStreamReader(gzip, encoding); StringBuffer sbuf = new StringBuffer(); char[] buffer = new char[32 * 1024]; int nread; while ((nread = reader.read(buffer)) >= 0) { sbuf.append(buffer, 0, nread); } decompressedString = sbuf.toString(); reader.close(); } catch (Exception e) { e.printStackTrace(); } return decompressedString; }
From source file:Main.java
public static String request(String url, Map<String, String> cookies, Map<String, String> parameters) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"); if (cookies != null && !cookies.isEmpty()) { StringBuilder cookieHeader = new StringBuilder(); for (String cookie : cookies.values()) { if (cookieHeader.length() > 0) { cookieHeader.append(";"); }/* w w w . j a va 2 s . c o m*/ cookieHeader.append(cookie); } connection.setRequestProperty("Cookie", cookieHeader.toString()); } connection.setDoInput(true); if (parameters != null && !parameters.isEmpty()) { connection.setDoOutput(true); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); osw.write(parametersToWWWFormURLEncoded(parameters)); osw.flush(); osw.close(); } if (cookies != null) { for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) { if (headerEntry != null && headerEntry.getKey() != null && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) { for (String header : headerEntry.getValue()) { for (HttpCookie httpCookie : HttpCookie.parse(header)) { cookies.put(httpCookie.getName(), httpCookie.toString()); } } } } } Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringWriter w = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while ((n = r.read(buffer)) != -1) { w.write(buffer, 0, n); } r.close(); return w.toString(); }