List of usage examples for java.io InputStreamReader read
public int read(char cbuf[], int offset, int length) throws IOException
From source file:net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.DataTypeBigDecimal.java
/** * Read a file and construct a valid object from its contents. * Errors are returned by throwing an IOException containing the * cause of the problem as its message.//w ww.j a v a2 s . c om * <P> * DataType is responsible for validating that the imported * data can be converted to an object, and then must return * a text string that can be used in the Popup window text area. * This object-to-text conversion is the same as is done by * the DataType object internally in the getJTextArea() method. * * <P> * File is assumed to be and ASCII string of digits * representing a value of this data type. */ public String importObject(FileInputStream inStream) throws IOException { InputStreamReader inReader = new InputStreamReader(inStream); int fileSize = inStream.available(); char charBuf[] = new char[fileSize]; int count = inReader.read(charBuf, 0, fileSize); if (count != fileSize) throw new IOException("Could read only " + count + " chars from a total file size of " + fileSize + ". Import failed."); // convert file text into a string // Special case: some systems tack a newline at the end of // the text read. Assume that if last char is a newline that // we want everything else in the line. String fileText; if (charBuf[count - 1] == KeyEvent.VK_ENTER) fileText = new String(charBuf, 0, count - 1); else fileText = new String(charBuf); // test that the string is valid by converting it into an // object of this data type StringBuffer messageBuffer = new StringBuffer(); validateAndConvertInPopup(fileText, null, messageBuffer); if (messageBuffer.length() > 0) { ; // convert number conversion issue into IO issue for consistancy throw new IOException( "Text does not represent data of type " + getClassName() + ". Text was:\n" + fileText); } // return the text from the file since it does // represent a valid value of this data type return fileText; }
From source file:org.codehaus.mojo.tomcat.TomcatManager.java
/** * Gets the data from the specified input stream as a string using the specified charset. * /*ww w . j av a 2 s . co m*/ * @param in the input stream to read from * @param charset the charset to use when constructing the string * @return a string representation of the data read from the input stream * @throws IOException if an i/o error occurs */ private String toString(InputStream in, String charset) throws IOException { InputStreamReader reader = new InputStreamReader(in, charset); StringBuffer buffer = new StringBuffer(); char[] chars = new char[1024]; int n; while ((n = reader.read(chars, 0, chars.length)) != -1) { buffer.append(chars, 0, n); } return buffer.toString(); }
From source file:org.apache.tapestry.engine.DefaultTemplateSource.java
/** * Reads a Stream into memory as an array of characters. * **//*from ww w .j av a 2 s . co m*/ private char[] readTemplateStream(InputStream stream, String encoding) throws IOException { char[] charBuffer = new char[BUFFER_SIZE]; StringBuffer buffer = new StringBuffer(); InputStreamReader reader; if (encoding != null) reader = new InputStreamReader(new BufferedInputStream(stream), encoding); else reader = new InputStreamReader(new BufferedInputStream(stream)); try { while (true) { int charsRead = reader.read(charBuffer, 0, BUFFER_SIZE); if (charsRead <= 0) break; buffer.append(charBuffer, 0, charsRead); } } finally { reader.close(); } // OK, now reuse the charBuffer variable to // produce the final result. int length = buffer.length(); charBuffer = new char[length]; // Copy the character out of the StringBuffer and into the // array. buffer.getChars(0, length, charBuffer, 0); return charBuffer; }
From source file:at.pardus.android.browser.PardusMessageChecker.java
/** * Tries to get the msgFrame via an HTTP GET request, parses it for new * messages/logs and displays the result. * /*from w w w . j av a 2 s . c o m*/ * Does not need to be run on the UI thread. */ public void check() { if (universe == null) { return; } if (PardusConstants.DEBUG) { Log.v(this.getClass().getSimpleName(), "Checking for new messages/logs"); } InputStreamReader reader = null; try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity == null) { Log.w(this.getClass().getSimpleName(), "Could not check for messages (error in response)"); return; } reader = new InputStreamReader(entity.getContent()); StringBuilder sb = new StringBuilder(); char[] buffer = new char[2048]; int i = 0; while ((i = reader.read(buffer, 0, buffer.length)) >= 0) { if (i > 0) { sb.append(buffer, 0, i); } } String responseStr = sb.toString(); parseResponse(responseStr); } catch (ClientProtocolException e) { Log.e(this.getClass().getSimpleName(), "Could not check for messages (error in protocol)", e); } catch (IOException e) { Log.w(this.getClass().getSimpleName(), "Could not check for messages (error reading response)", e); } catch (Exception e) { Log.e(this.getClass().getSimpleName(), "Could not check for messages (unknown error)", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } }
From source file:sos.Sos.java
/** Getter for post content * /*from w ww. j a v a 2s . c om*/ * @param request * @return the post content */ private String getPostContent(HttpServletRequest request, SnannySostServerConfig snannySostServerConfig) { try { // Don't use request.getReader() , fix charset with InputStreamReader ServletInputStream servletInputStream = request.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(request.getInputStream(), snannySostServerConfig.getCharset()); StringBuilder buffer = new StringBuilder(); char[] buf = new char[4 * 1024]; // 4Kchar buffer int len; while ((len = inputStreamReader.read(buf, 0, buf.length)) != -1) { buffer.append(buf, 0, len); } return (buffer.toString()); } catch (IOException ex) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_IO_POST, Status.SERVICE_UNAVAILABLE); } }
From source file:com.xmlcalabash.library.ApacheHttpRequest.java
public void readBodyContentPart(TreeWriter tree, InputStream bodyStream, String contentType, String charset) throws SaxonApiException, IOException { if (xmlContentType(contentType)) { // Read it as XML SAXSource source = new SAXSource(new InputSource(bodyStream)); DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder(); tree.addSubtree(builder.build(source)); } else if (textContentType(contentType)) { // Read it as text InputStreamReader reader = new InputStreamReader(bodyStream, charset); char buf[] = new char[bufSize]; int len = reader.read(buf, 0, bufSize); while (len >= 0) { tree.addText(new String(buf, 0, len)); len = reader.read(buf, 0, bufSize); }//from w ww .j a v a2 s .c o m } else { // Read it as binary byte bytes[] = new byte[bufSize]; int pos = 0; int readLen = bufSize; int len = bodyStream.read(bytes, 0, bufSize); while (len >= 0) { pos += len; readLen -= len; if (readLen == 0) { System.err.println("Encoding " + readLen + " bytes (from " + pos + ")"); tree.addText(Base64.encodeBytes(bytes)); pos = 0; readLen = bufSize; } len = bodyStream.read(bytes, pos, readLen); } if (pos > 0) { byte lastBytes[] = new byte[pos]; System.arraycopy(bytes, 0, lastBytes, 0, pos); tree.addText(Base64.encodeBytes(lastBytes)); } tree.addText("\n"); // FIXME: should we be doing this? } }
From source file:org.adblockplus.android.AdblockPlus.java
@Override public void onCreate() { super.onCreate(); instance = this; // Check for crash report try {/*from w ww .j a va 2s. c o m*/ InputStreamReader reportFile = new InputStreamReader(openFileInput(CrashHandler.REPORT_FILE)); final char[] buffer = new char[0x1000]; StringBuilder out = new StringBuilder(); int read; do { read = reportFile.read(buffer, 0, buffer.length); if (read > 0) out.append(buffer, 0, read); } while (read >= 0); String report = out.toString(); if (!"".equals(report)) { final Intent intent = new Intent(this, CrashReportDialog.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("report", report); startActivity(intent); } } catch (FileNotFoundException e) { // ignore } catch (IOException e) { // TODO Auto-generated catch block Log.e(TAG, e.getMessage(), e); } // Set crash handler Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); // Initiate update check scheduleUpdater(0); }
From source file:ch.randelshofer.cubetwister.HTMLExporter.java
/** * Reads the content of the input stream into a list of token. * If a token start with "${", its a placeholder. * If a token doesn't start with "${" its a literal text. *//*from w w w. j a va 2 s . co m*/ private String[] toTokens(String filename, InputStream in) throws IOException { InputStreamReader reader = new InputStreamReader(in, "UTF8"); StringBuilder buf = new StringBuilder(); char[] cbuf = new char[256]; int len; while (-1 != (len = reader.read(cbuf, 0, cbuf.length))) { buf.append(cbuf, 0, len); } int p1 = 0; int p2 = 0; LinkedList<String> tokens = new LinkedList<String>(); while (true) { p1 = buf.indexOf("${", p2); if (p1 == -1) { break; } tokens.add(buf.substring(p2, p1)); p2 = buf.indexOf("}", p1 + 2); if (p2 == -1) { throw new IOException("Closing curly bracket missing in " + filename + " after position " + p1); } p2++; tokens.add(buf.substring(p1, p2)); } if (p2 != buf.length()) { tokens.add(buf.substring(p2)); } return tokens.toArray(new String[tokens.size()]); }
From source file:com.comcast.cqs.test.stress.CqsStressTester.java
public String send(String endpoint, String message) throws Exception { logger.debug("event=send_http_request endpoint=" + endpoint + "\" message=\"" + message + "\""); if ((message == null) || (endpoint == null)) { logger.debug("event=send_http_request error_code=MissingParameters endpoint=" + endpoint + "\" message=\"" + message + "\""); throw new Exception("Message and Endpoint must both be set"); }//from w w w .ja v a 2s .com HttpPost httpPost = new HttpPost(endpoint); StringEntity stringEntity = new StringEntity(message); httpPost.setEntity(stringEntity); HttpResponse response = httpClient.execute(httpPost); response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); InputStreamReader responseReader = new InputStreamReader(instream); StringBuffer responseB = new StringBuffer(); char[] arr = new char[1024]; int size = 0; while ((size = responseReader.read(arr, 0, arr.length)) != -1) { responseB.append(arr, 0, size); } instream.close(); return responseB.toString(); } logger.error("Could not get response entity"); System.exit(1); return null; }
From source file:com.xmlcalabash.library.HttpRequest.java
public void readBodyContentPart(TreeWriter tree, InputStream bodyStream, String contentType, String charset) throws SaxonApiException, IOException { if (xmlContentType(contentType)) { // Read it as XML tree.addSubtree(runtime.parse(new InputSource(bodyStream))); } else if (textContentType(contentType)) { // Read it as text InputStreamReader reader = new InputStreamReader(bodyStream, charset); char buf[] = new char[bufSize]; int len = reader.read(buf, 0, bufSize); while (len >= 0) { String s = new String(buf, 0, len); tree.addText(s);/*from w w w. j a v a 2 s .com*/ len = reader.read(buf, 0, bufSize); } } else if (jsonContentType(contentType)) { InputStreamReader reader = new InputStreamReader(bodyStream); JSONTokener jt = new JSONTokener(reader); XdmNode jsonDoc = JSONtoXML.convert(runtime.getProcessor(), jt, runtime.jsonFlavor()); tree.addSubtree(jsonDoc); } else { // Read it as binary byte bytes[] = new byte[bufSize]; int pos = 0; int readLen = bufSize; int len = bodyStream.read(bytes, 0, bufSize); while (len >= 0) { pos += len; readLen -= len; if (readLen == 0) { String encoded = Base64.encodeBytes(bytes); tree.addText(encoded); pos = 0; readLen = bufSize; } len = bodyStream.read(bytes, pos, readLen); } if (pos > 0) { byte lastBytes[] = new byte[pos]; System.arraycopy(bytes, 0, lastBytes, 0, pos); tree.addText(Base64.encodeBytes(lastBytes)); } tree.addText("\n"); // FIXME: should we be doing this? } }