List of usage examples for java.io ByteArrayOutputStream toString
public synchronized String toString()
From source file:com.ocp.picasa.GDataClient.java
public static String inputStreamToString(InputStream stream) { if (stream != null) { try {/* w w w . j a v a 2 s .co m*/ ByteArrayOutputStream bytes = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = stream.read(buffer)) != -1) { bytes.write(buffer, 0, bytesRead); } return new String(bytes.toString()); } catch (IOException e) { // Fall through and return null. } } return null; }
From source file:com.halseyburgund.rwframework.core.RWHttpManager.java
public static String uploadFile(String page, Properties properties, String fileParam, String file, int timeOutSec) throws Exception { if (D) {// w w w . ja v a 2s. co m Log.d(TAG, "Starting upload of file: " + file, null); } // build GET-like page name that includes the RW operation Enumeration<Object> enumProps = properties.keys(); StringBuilder uriBuilder = new StringBuilder(page).append('?'); while (enumProps.hasMoreElements()) { String key = enumProps.nextElement().toString(); String value = properties.get(key).toString(); if ("operation".equals(key)) { uriBuilder.append(key); uriBuilder.append('='); uriBuilder.append(java.net.URLEncoder.encode(value)); break; } } if (D) { Log.d(TAG, "GET request: " + uriBuilder.toString(), null); } HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, timeOutSec * 1000); HttpConnectionParams.setSoTimeout(httpParams, timeOutSec * 1000); HttpClient httpClient = new DefaultHttpClient(httpParams); HttpPost request = new HttpPost(uriBuilder.toString()); RWMultipartEntity entity = new RWMultipartEntity(); Iterator<Map.Entry<Object, Object>> i = properties.entrySet().iterator(); while (i.hasNext()) { Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) i.next(); String key = (String) entry.getKey(); String val = (String) entry.getValue(); entity.addPart(key, val); if (D) { Log.d(TAG, "Added StringBody multipart for: '" + key + "' = '" + val + "'", null); } } File upload = new File(file); entity.addPart(fileParam, upload); if (D) { String msg = "Added FileBody multipart for: '" + fileParam + "' =" + " <'" + upload.getAbsolutePath() + ", " + "size: " + upload.length() + " bytes >'"; Log.d(TAG, msg, null); } request.setEntity(entity); if (D) { Log.d(TAG, "Sending HTTP request...", null); } HttpResponse response = httpClient.execute(request); int st = response.getStatusLine().getStatusCode(); if (st == HttpStatus.SC_OK) { StringBuffer sbResponse = new StringBuffer(); InputStream content = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { sbResponse.append(line); } content.close(); // this will also close the connection if (D) { Log.d(TAG, "Upload successful (HTTP code: " + st + ")", null); Log.d(TAG, "Server response: " + sbResponse.toString(), null); } return sbResponse.toString(); } else { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); entity.writeTo(ostream); Log.e(TAG, "Upload failed (http code: " + st + ")", null); Log.e(TAG, "Server response: " + ostream.toString(), null); throw new HttpException(String.valueOf(st)); } }
From source file:org.jboss.as.test.integration.management.http.HttpDeploymentUploadUnitTestCase.java
private static ModelNode validateStatus(final HttpResponse response) throws IOException { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final InputStream in = response.getEntity().getContent(); final byte[] buffer = new byte[64]; int len;/* w w w. j ava 2 s . c o m*/ while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } fail(out.toString()); } final HttpEntity entity = response.getEntity(); final ModelNode result = ModelNode.fromJSONStream(entity.getContent()); assertNotNull(result); assertTrue(Operations.isSuccessfulOutcome(result)); return result; }
From source file:hudson.remoting.Engine.java
/** * Read until '\n' and returns it as a string. *//*from ww w. jav a2s. co m*/ private static String readLine(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); while (true) { int ch = in.read(); if (ch < 0 || ch == '\n') return baos.toString().trim(); // trim off possible '\r' baos.write(ch); } }
From source file:com.jrummyapps.busybox.utils.Utils.java
/** * Read a file from /res/raw//from w w w .j av a 2 s . c o m * * @param id * The id from R.raw * @return The contents of the file or {@code null} if reading failed. */ public static String readRaw(int id) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = getContext().getResources().openRawResource(id); try { IOUtils.copy(inputStream, outputStream); } catch (IOException e) { return null; } finally { IoUtils.closeQuietly(outputStream); IoUtils.closeQuietly(inputStream); } return outputStream.toString(); }
From source file:com.meltmedia.cadmium.core.FileSystemManager.java
public static String getFileContents(InputStream in) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); streamCopy(in, out);/*from w w w . j ava 2 s . c o m*/ String contents = out.toString(); if (contents != null) { contents.trim(); } return contents; }
From source file:com.jrummyapps.busybox.utils.Utils.java
/** * Get a list of binaries in the assets directory * * @return a list of binaries from the assets in this APK file. *//*w w w .j av a 2s . c o m*/ public static ArrayList<BinaryInfo> getBinariesFromAssets() { ArrayList<BinaryInfo> binaries = new ArrayList<>(); try { ByteArrayOutputStream output = new ByteArrayOutputStream(); InputStream input = getContext().getResources().openRawResource(R.raw.binaries); byte[] buffer = new byte[4096]; int n; while ((n = input.read(buffer)) != -1) { output.write(buffer, 0, n); } input.close(); JSONArray jsonArray = new JSONArray(output.toString()); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("name"); String filename = jsonObject.getString("filename"); String abi = jsonObject.getString("abi"); String flavor = jsonObject.getString("flavor"); String url = jsonObject.getString("url"); String md5sum = jsonObject.getString("md5sum"); long size = jsonObject.getLong("size"); binaries.add(new BinaryInfo(name, filename, abi, flavor, url, md5sum, size)); } } catch (Exception e) { Crashlytics.logException(e); } return binaries; }
From source file:com.aptana.editor.css.validator.CSSValidator.java
/** * Gets the validation report from the validator. * // w w w .j av a2 s . c o m * @param source * the source text * @param path * the source path * @return the report */ private static String getReport(String source, URI path) { StyleSheetParser parser = new StyleSheetParser(); ApplContext ac = new ApplContext("en"); //$NON-NLS-1$ ac.setProfile(APTANA_PROFILE); try { parser.parseStyleElement(ac, new ByteArrayInputStream(source.getBytes(IOUtil.UTF_8)), null, null, path.toURL(), 0); } catch (MalformedURLException e) { IdeLog.logError(CSSPlugin.getDefault(), MessageFormat.format(Messages.CSSValidator_ERR_InvalidPath, path), e); } catch (UnsupportedEncodingException e) { IdeLog.logError(CSSPlugin.getDefault(), e); } StyleSheet stylesheet = parser.getStyleSheet(); stylesheet.findConflicts(ac); StyleReport report = StyleReportFactory.getStyleReport(ac, "Title", stylesheet, "soap12", 2); //$NON-NLS-1$ //$NON-NLS-2$ ByteArrayOutputStream out = new ByteArrayOutputStream(); report.print(new PrintWriter(out)); return out.toString().replaceAll("m:", ""); //$NON-NLS-1$ //$NON-NLS-2$ }
From source file:com.aptana.css.core.internal.build.CSSValidator.java
/** * Gets the validation report from the validator. * /* w w w. j a v a 2 s.c om*/ * @param source * the source text * @param path * the source path * @return the report */ private static String getReport(String source, URI path) { StyleSheetParser parser = new StyleSheetParser(); ApplContext ac = new ApplContext("en"); //$NON-NLS-1$ ac.setProfile(APTANA_PROFILE); try { parser.parseStyleElement(ac, new ByteArrayInputStream(source.getBytes(IOUtil.UTF_8)), null, null, path.toURL(), 0); } catch (MalformedURLException e) { IdeLog.logError(CSSCorePlugin.getDefault(), MessageFormat.format(Messages.CSSValidator_ERR_InvalidPath, path), e); } catch (UnsupportedEncodingException e) { IdeLog.logError(CSSCorePlugin.getDefault(), e); } StyleSheet stylesheet = parser.getStyleSheet(); stylesheet.findConflicts(ac); StyleReport report = StyleReportFactory.getStyleReport(ac, "Title", stylesheet, "soap12", 2); //$NON-NLS-1$ //$NON-NLS-2$ ByteArrayOutputStream out = new ByteArrayOutputStream(); report.print(new PrintWriter(out)); return out.toString().replaceAll("m:", ""); //$NON-NLS-1$ //$NON-NLS-2$ }
From source file:org.apache.jetspeed.util.Base64.java
public static String encodeAsString(byte[] plaindata) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream inStream = new ByteArrayOutputStream(); inStream.write(plaindata, 0, plaindata.length); // pad/* w ww.j av a2s . c o m*/ if ((plaindata.length % 3) == 1) { inStream.write(0); inStream.write(0); } else if ((plaindata.length % 3) == 2) { inStream.write(0); } inStream.writeTo(MimeUtility.encode(out, "base64")); return out.toString(); }