List of usage examples for java.io InputStreamReader read
public int read(java.nio.CharBuffer target) throws IOException
From source file:com.hunantv.fw.utils.StreamUtils.java
/** * Copy the contents of the given InputStream into a String. * Leaves the stream open when done./* w w w. j a v a 2 s .c o m*/ * @param in the InputStream to copy from * @return the String that has been copied to * @throws IOException in case of I/O errors */ public static String copyToString(InputStream in, Charset charset) throws IOException { StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = reader.read(buffer)) != -1) { out.append(buffer, 0, bytesRead); } return out.toString(); }
From source file:org.crazydog.util.spring.StreamUtils.java
/** * Copy the contents of the given InputStream into a String. * Leaves the stream open when done./*from w ww . jav a2s . c o m*/ * @param in the InputStream to copy from * @return the String that has been copied to * @throws IOException in case of I/O errors */ public static String copyToString(InputStream in, Charset charset) throws IOException { org.springframework.util.Assert.notNull(in, "No InputStream specified"); StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = reader.read(buffer)) != -1) { out.append(buffer, 0, bytesRead); } return out.toString(); }
From source file:org.globusonline.transfer.DelegateProxyActivation.java
public static String readEntireStream(InputStreamReader in) throws IOException { StringBuilder contents = new StringBuilder(); char[] buffer = new char[4096]; int read = 0; do {// ww w. java 2s . c o m contents.append(buffer, 0, read); read = in.read(buffer); } while (read >= 0); return contents.toString(); }
From source file:Main.java
/** * Using a Reader and a Writer, returns a String from an InputStream. * * Method based on Hypersonic Code/* w ww . ja v a 2 s . c om*/ * * @param x InputStream to read from * @throws IOException * @return a Java string */ public static String inputStreamToString(InputStream x, String encoding) throws IOException { InputStreamReader in = new InputStreamReader(x, encoding); StringWriter writer = new StringWriter(); int blocksize = 8 * 1024; char[] buffer = new char[blocksize]; for (;;) { int read = in.read(buffer); if (read == -1) { break; } writer.write(buffer, 0, read); } writer.close(); return writer.toString(); }
From source file:org.ingini.mongodb.jongo.example.util.CollectionManager.java
private static void fill(DBCollection collection, String collectionContentFilePath) { StringBuilder stringBuilder = new StringBuilder(); try {/*from w ww . ja va 2 s. c o m*/ InputStreamReader inputStreamReader = new InputStreamReader( // CollectionManager.class.getClassLoader().getResourceAsStream(collectionContentFilePath), "UTF-8"); CharBuffer buf = CharBuffer.allocate(BUFFER_SIZE); for (int read = inputStreamReader.read(buf); read != EOF; read = inputStreamReader.read(buf)) { buf.flip(); stringBuilder.append(buf, START, read); } } catch (IOException e) { System.out.println("Unable to read input stream due to an exception! Exception: " + ExceptionUtils.getStackTrace(e)); throw new IllegalStateException(e); } BasicDBList parse = (BasicDBList) JSON.parse(stringBuilder.toString(), new MongoIdTransformerJSONCallback()); collection.insert(parse.toArray(new DBObject[parse.size()])); }
From source file:org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData.java
private static String toString(final InputStream input, final String encoding) throws IOException { final int DEFAULT_BUFFER_SIZE = 1024 * 4; StringWriter sw = new StringWriter(); InputStreamReader in = new InputStreamReader(input, encoding); char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int n = 0;/* w ww .ja v a 2s. co m*/ while (-1 != (n = in.read(buffer))) { sw.write(buffer, 0, n); } return sw.toString(); }
From source file:com.sun.socialsite.config.RuntimeConfig.java
/** * Get the runtime configuration definitions XML file as a string. * * This is basically a convenience method for accessing this file. * The file itself contains meta-data about what configuration * properties we change at runtime via the UI and how to setup * the display for editing those properties. *///from w w w.jav a 2 s.co m public static String getRuntimeConfigDefsAsString() { log.debug("Trying to load runtime config defs file"); try { InputStream in = Config.class.getResourceAsStream(DEFINITIONS_FILE); InputStreamReader reader = new InputStreamReader(in); StringWriter configString = new StringWriter(); char[] buf = new char[8196]; int length = 0; while ((length = reader.read(buf)) > 0) { configString.write(buf, 0, length); } reader.close(); return configString.toString(); } catch (Exception e) { log.error("Error loading runtime config defs file", e); } return ""; }
From source file:com.xerox.amazonws.ec2.EC2Utils.java
/** * This method makes a best effort to fetch a piece of instance metadata. * * @param key the name of the metadata to fetch * @return value of the metadata item/*from w w w . j a va 2 s . com*/ */ public static String getInstanceUserdata() throws IOException { int retries = 0; while (true) { try { URL url = new URL("http://169.254.169.254/latest/user-data/"); InputStreamReader rdr = new InputStreamReader(url.openStream()); StringWriter wtr = new StringWriter(); char[] buf = new char[1024]; int bytes; while ((bytes = rdr.read(buf)) > -1) { if (bytes > 0) { wtr.write(buf, 0, bytes); } } rdr.close(); return wtr.toString(); } catch (IOException ex) { if (retries == 5) { logger.debug("Problem getting user data, retries exhausted..."); return null; } else { logger.debug("Problem getting user data, retrying..."); try { Thread.sleep((int) Math.pow(2.0, retries) * 1000); } catch (InterruptedException e) { } } } } }
From source file:Main.java
public static String readStreamToString(InputStream inputStream) throws IOException { BufferedInputStream bufferedInputStream = null; InputStreamReader reader = null; try {//from w w w. j a v a 2s . c o m bufferedInputStream = new BufferedInputStream(inputStream); reader = new InputStreamReader(bufferedInputStream); StringBuilder stringBuilder = new StringBuilder(); final int bufferSize = 1024 * 2; char[] buffer = new char[bufferSize]; int n = 0; while ((n = reader.read(buffer)) != -1) { stringBuilder.append(buffer, 0, n); } return stringBuilder.toString(); } finally { closeQuietly(bufferedInputStream); closeQuietly(reader); } }
From source file:org.apache.roller.planet.config.PlanetRuntimeConfig.java
/** * Get the runtime configuration definitions XML file as a string. * * This is basically a convenience method for accessing this file. * The file itself contains meta-data about what configuration * properties we change at runtime via the UI and how to setup * the display for editing those properties. */// w ww. ja v a 2 s. co m public static String getRuntimeConfigDefsAsString() { log.debug("Trying to load runtime config defs file"); try { InputStreamReader reader = new InputStreamReader( PlanetConfig.class.getResourceAsStream(runtime_config)); StringWriter configString = new StringWriter(); char[] buf = new char[8196]; int length = 0; while ((length = reader.read(buf)) > 0) configString.write(buf, 0, length); reader.close(); return configString.toString(); } catch (Exception e) { log.error("Error loading runtime config defs file", e); } return ""; }