List of usage examples for java.io ByteArrayOutputStream write
public synchronized void write(byte b[], int off, int len)
From source file:Main.java
public static byte[] compress(byte[] data, int level) throws IOException { if (data == null || data.length == 0) { return data; }/*w w w .jav a 2 s. c om*/ ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length); Deflater deflater = new Deflater(); deflater.setLevel(level); deflater.setInput(data); deflater.finish(); byte[] buf = new byte[BUFFER_SIZE]; while (!deflater.finished()) { int count = deflater.deflate(buf); bout.write(buf, 0, count); } deflater.end(); bout.close(); return bout.toByteArray(); }
From source file:Main.java
public static String decompress(String hexString) throws UnsupportedEncodingException { final byte[] byteArray = new byte[hexString.length() / 2]; int k = 0;/*from w w w. j av a 2 s . c o m*/ for (int i = 0; i < byteArray.length; i++) { byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); byteArray[i] = (byte) (high << 4 | low); k += 2; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(byteArray); try { GZIPInputStream gunzip = new GZIPInputStream(in); byte[] buffer = new byte[256]; int n; while ((n = gunzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } } catch (IOException e) { e.printStackTrace(); } return out.toString(); }
From source file:com.iaraby.utility.HttpUtility.java
/** * Read the Http response bytes and return the String value * String value could be row data or josn or xml data * /*from w ww . j a v a 2s. c o m*/ * @param HttpResponse * @return String value * @throws IllegalStateException * @throws IOException */ public static String readHttpResponse(HttpResponse response) throws IllegalStateException, IOException { HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); ByteArrayOutputStream content = new ByteArrayOutputStream(); int readBytes = 0; byte[] sBuffer = new byte[512]; while ((readBytes = inputStream.read(sBuffer)) != -1) { content.write(sBuffer, 0, readBytes); } inputStream.close(); return (new String(content.toByteArray())); }
From source file:com.enonic.esl.io.FileUtil.java
/** * Get bytes from an input stream./* w w w . j a va 2 s . co m*/ * * @param inputStream the input stream * @param sizeHint a hint of the stream's size * @return a byte array containing the stream's contents * @throws IOException */ public static byte[] getBytesFromStream(InputStream inputStream, int sizeHint) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(sizeHint); int numRead = 0; byte[] buffer = new byte[16 * 1024]; while ((numRead = inputStream.read(buffer)) >= 0) { baos.write(buffer, 0, numRead); } return baos.toByteArray(); }
From source file:Main.java
public static byte[] decompress(byte[] bytes) throws IOException { GZIPInputStream gzip = null;//from w ww.j a va 2s .c o m ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(bytes)); int len = 0; byte data[] = new byte[BUFFER_SIZE]; while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) { baos.write(data, 0, len); } gzip.close(); baos.flush(); return baos.toByteArray(); } finally { if (gzip != null) { gzip.close(); } if (baos != null) { baos.close(); } } }
From source file:Main.java
public static byte[] readFile(File file) throws IOException { if (!file.exists()) { throw new IOException("not crete file=" + file.getAbsolutePath()); }/*w w w. j a v a 2 s .c o m*/ FileInputStream fileInputStream = null; ByteArrayOutputStream byteArrayOutputStream = null; try { fileInputStream = new FileInputStream(file); byteArrayOutputStream = new ByteArrayOutputStream(64); int length = 0; byte[] buffer = new byte[1024]; while ((length = fileInputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, length); } return byteArrayOutputStream.toByteArray(); } finally { if (fileInputStream != null) { fileInputStream.close(); } if (byteArrayOutputStream != null) { byteArrayOutputStream.close(); } } }
From source file:c3.ops.priam.utils.SystemUtils.java
public static String getDataFromUrl(String url) { try {/*from w w w . ja v a 2s . c o m*/ HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setConnectTimeout(1000); conn.setReadTimeout(1000); conn.setRequestMethod("GET"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Unable to get data for URL " + url); } byte[] b = new byte[2048]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataInputStream d = new DataInputStream((FilterInputStream) conn.getContent()); int c = 0; while ((c = d.read(b, 0, b.length)) != -1) bos.write(b, 0, c); String return_ = new String(bos.toByteArray(), Charsets.UTF_8); logger.info("Calling URL API: {} returns: {}", url, return_); conn.disconnect(); return return_; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:Main.java
private static String consume(URLConnection connection) throws IOException { String encoding = getEncoding(connection); ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = connection.getInputStream(); try {/*from w w w . j av a 2s. co m*/ in = connection.getInputStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } } finally { try { in.close(); } catch (IOException ioe) { // continue } } try { return new String(out.toByteArray(), encoding); } catch (UnsupportedEncodingException uee) { try { return new String(out.toByteArray(), "UTF-8"); } catch (UnsupportedEncodingException uee2) { // can't happen throw new IllegalStateException(uee2); } } }
From source file:com.easycode.common.HttpUtil.java
public static byte[] httpPost(String url, List<FormItem> blist) throws Exception { byte[] ret = null; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8")); if (blist != null) { for (FormItem f : blist) { reqEntity.addPart(f.getName(), f.getCtx()); }/*from w w w . ja v a 2 s . c o m*/ } httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { InputStream tis = resEntity.getContent(); java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len = 0; while ((len = tis.read(bytes)) > 0) { out.write(bytes, 0, len); } ret = out.toByteArray(); } EntityUtils.consume(resEntity); try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) { } return ret; }
From source file:de.l3s.boilerpipe.sax.HTMLFetcher.java
public static HTMLDocument fetchHelper(final URL url) throws IOException { final URLConnection conn = url.openConnection(); //conn.setRequestProperty("User-Agent", //"Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36"); //conn.setRequestProperty("Cookie","wapparam=web2wap; vt=4"); final String ct = conn.getContentType(); if (ct == null || !(ct.equals("text/html") || ct.startsWith("text/html;"))) { //throw new IOException("Unsupported content type: "+ct+ url); System.err.println("WARN: unsupported Content-type: " + ct + url); }//from www. jav a 2 s. co m Charset cs = Charset.forName("UTF8"); if (ct != null) { Matcher m = PAT_CHARSET_REX.matcher(ct); if (m.find()) { final String charset = m.group(1); try { cs = Charset.forName(charset); } catch (UnsupportedCharsetException e) { // keep default } } } InputStream in = conn.getInputStream(); final String encoding = conn.getContentEncoding(); if (encoding != null) { if ("gzip".equalsIgnoreCase(encoding)) { in = new GZIPInputStream(in); } else { System.err.println("WARN: unsupported Content-Encoding: " + encoding); } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; int r; while ((r = in.read(buf)) != -1) { bos.write(buf, 0, r); } in.close(); final byte[] data = bos.toByteArray(); return new HTMLDocument(data, cs); }