List of usage examples for java.io BufferedInputStream mark
public synchronized void mark(int readlimit)
mark
method of InputStream
. From source file:org.gradle.api.plugins.buildcomparison.outcome.internal.archive.entry.FileToArchiveEntrySetTransformer.java
private ImmutableSet<ArchiveEntry> walk(InputStream archiveInputStream, ImmutableSet.Builder<ArchiveEntry> allEntries, ImmutableList<String> parentPaths) { ImmutableSet.Builder<ArchiveEntry> entries = ImmutableSet.builder(); ZipInputStream zipStream = new ZipInputStream(archiveInputStream); try {/*from www . ja v a 2 s . c o m*/ ZipEntry entry = zipStream.getNextEntry(); while (entry != null) { ArchiveEntry.Builder builder = new ArchiveEntry.Builder(); builder.setParentPaths(parentPaths); builder.setPath(entry.getName()); builder.setCrc(entry.getCrc()); builder.setDirectory(entry.isDirectory()); builder.setSize(entry.getSize()); if (!builder.isDirectory() && (zipStream.available() == 1)) { boolean zipEntry; final BufferedInputStream bis = new BufferedInputStream(zipStream) { @Override public void close() throws IOException { } }; bis.mark(Integer.MAX_VALUE); zipEntry = new ZipInputStream(bis).getNextEntry() != null; bis.reset(); if (zipEntry) { ImmutableList<String> nextParentPaths = ImmutableList.<String>builder().addAll(parentPaths) .add(entry.getName()).build(); ImmutableSet<ArchiveEntry> subEntries = walk(bis, allEntries, nextParentPaths); builder.setSubEntries(subEntries); } } ArchiveEntry archiveEntry = builder.build(); entries.add(archiveEntry); allEntries.add(archiveEntry); zipStream.closeEntry(); entry = zipStream.getNextEntry(); } } catch (IOException e) { throw new UncheckedIOException(e); } finally { IOUtils.closeQuietly(zipStream); } return entries.build(); }
From source file:com.iloomo.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {//from www .j a v a 2 s . c o m for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); Set<String> keys = parameter.keySet(); for (String key : keys) { list.add(new BasicNameValuePair(key, String.valueOf(parameter.get(key)))); } ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { // LogUtil.d("HttpTask", " use GZIPInputStream "); is = new GZIPInputStream(bis); } else { // LogUtil.d("HttpTask", // " not use GZIPInputStream"); is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("999", exception.getMessage()); } break; } catch (Exception e) { if (i == HttpConstant.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("999", exception.getMessage()); } else { Log.d("connection url", "" + i); continue; } } } } catch (IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, HttpConstant.ERROR_MESSAGE); ret = ErrorUtil.errorJson("999", exception.getMessage()); } finally { if (!HttpConstant.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } } super.run(); }
From source file:com.iloomo.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {/* w w w.j a v a 2 s .c o m*/ if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); Set<String> keys = parameter.keySet(); for (String key : keys) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(EncodeUtils.encode(key)); bulider.append("="); bulider.append(EncodeUtils.encode(String.valueOf(parameter.get(key)))); } url += "?" + bulider.toString(); Log.i("request", url); } for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (HttpConstant.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); // ByteArrayOutputStream content = new // ByteArrayOutputStream(); // response.getEntity().writeTo(content); // ret = new String(content.toByteArray()).trim(); // content.close(); } else { ret = ErrorUtil.errorJson("" + statusCode, "??,??" + statusCode); continue; } break; } catch (Exception e) { if (i == HttpConstant.CONNECTION_COUNT - 1) { ret = ErrorUtil.errorJson("999", ""); } else { Log.d("connection url", "" + i); continue; } } } } catch (IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, HttpConstant.ERROR_MESSAGE); ret = ErrorUtil.errorJson("999", exception.getMessage()); } finally { if (!HttpConstant.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } } super.run(); }
From source file:com.changxiang.game.sdk.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {/*from w w w. j a v a 2 s . c o m*/ if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); for (RequestParameter p : parameter) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(Util.encode(p.getName())); bulider.append("="); bulider.append(Util.encode(p.getValue())); } // url += "?" + bulider.toString(); url += "?" + bulider.toString() + "&sign=" + MD5Util.MD5(bulider.toString() + "1234" + CXGameConfig.SERVERKEY); System.out.println("===" + url); } LogUtil.d("AsyncHttpGet : ", url); for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (CXGameConfig.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); LogUtil.d("connection url", "" + i); continue; } break; } catch (Exception e) { if (i == CXGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, CXGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!CXGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:com.baofeng.game.sdk.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {/*from w w w . j a va 2s . c om*/ if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); for (RequestParameter p : parameter) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(Util.encode(p.getName())); bulider.append("="); bulider.append(Util.encode(p.getValue())); } // url += "?" + bulider.toString(); url += "?" + bulider.toString() + "&sign=" + MD5Util.MD5(bulider.toString() + "1234" + BFGameConfig.SERVERKEY); // System.out.println("@@@"+bulider.toString()); } LogUtil.d("AsyncHttpGet : ", url); for (int i = 0; i < BFGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (BFGameConfig.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); LogUtil.d("connection url", "" + i); continue; } break; } catch (Exception e) { if (i == BFGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, BFGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!BFGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:com.changxiang.game.sdk.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {/*from w w w.jav a 2 s . c o m*/ for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); for (RequestParameter p : parameter) { list.add(new BasicNameValuePair(Util.encode(p.getName()), Util.encode(p.getValue()))); LogUtil.d("AsyncHttpPost Param ", p.getName() + " , " + p.getValue()); } StringBuffer sb = new StringBuffer(); for (int j = 0; j < list.size(); j++) { sb.append(list.get(j)); if (!(j == list.size() - 1)) { sb.append("&"); } } BasicNameValuePair bn = new BasicNameValuePair("sign", MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY)); System.out.println( "POST_SIGN:" + sb + "&sign=" + MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY)); list.add(bn); ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } System.out.println("====" + url); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { // HttpManager.saveCookies(response); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } break; } catch (Exception e) { if (i == CXGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, CXGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!CXGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:com.slytechs.capture.StreamFactory.java
public <T extends InputCapture<? extends FilePacket>> T newInput(final Class<T> t, final File file, Filter<ProtocolFilterTarget> filter) throws IOException { final BufferedInputStream b = new BufferedInputStream(new FileInputStream(file)); b.mark(1024); // Buffer first 1K of stream so we can rewind /*/*from w w w. j a v a 2s. c om*/ * Check the stream, without decompression first */ if (formatType(Channels.newChannel(b)) != null) { b.close(); /* * This is a plain uncompressed file, open up a FileChannel. It will be * much faster */ return newInput(t, new RandomAccessFile(file, "rw").getChannel(), filter); } /* * Try with gunziped stream, second */ b.reset(); // Rewind if (formatType(Channels.newChannel(new GZIPInputStream(b))) != null) { b.close(); /* * Now reopen the same file, but this time without the buffered * inputstream in the middle. Try to make things as efficient as possible. * TODO: implement much faster channel based GZIP decompression algorithm */ return newInput(t, Channels.newChannel(new GZIPInputStream(new FileInputStream(file))), filter); } throw new IllegalArgumentException( "File is not any compressed or decompressed known format [" + file.getName() + "]"); }
From source file:com.slytechs.capture.StreamFactory.java
public InputCapture<? extends CapturePacket> newInput(final File file, final Filter<ProtocolFilterTarget> filter) throws IOException { final BufferedInputStream b = new BufferedInputStream(new FileInputStream(file)); b.mark(1024); // Buffer first 1K of stream so we can rewind /*/* w w w .java 2 s . co m*/ * Check the stream, without decompression first */ if (formatType(Channels.newChannel(b)) != null) { b.close(); /* * This is a plain uncompressed file, open up a FileChannel. It will be * much faster */ return newInput(new RandomAccessFile(file, "rw").getChannel(), filter); } /* * Try with gunziped stream, second */ b.reset(); // Rewind if (formatType(Channels.newChannel(new GZIPInputStream(b))) != null) { b.close(); /* * Now reopen the same file, but this time without the buffered * inputstream in the middle. Try to make things as efficient as possible. * TODO: implement much faster channel based GZIP decompression algorithm */ return newInput(Channels.newChannel(new GZIPInputStream(new FileInputStream(file))), filter); } b.close(); return factoryForOther.getFactory().newInput(new RandomAccessFile(file, "r").getChannel(), filter); }
From source file:com.baofeng.game.sdk.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {//w w w .ja va2 s. c o m for (int i = 0; i < BFGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); for (RequestParameter p : parameter) { list.add(new BasicNameValuePair(Util.encode(p.getName()), Util.encode(p.getValue()))); LogUtil.d("AsyncHttpPost Param ", p.getName() + " , " + p.getValue()); } StringBuffer sb = new StringBuffer(); for (int j = 0; j < list.size(); j++) { sb.append(list.get(j)); if (!(j == list.size() - 1)) { sb.append("&"); } } BasicNameValuePair bn = new BasicNameValuePair("sign", MD5Util.MD5(sb + "1234" + BFGameConfig.SERVERKEY)); // System.out.println("@@@" + sb + "1234" // + BFGameConfig.SERVERKEY); list.add(bn); ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } LogUtil.d("AsyncHttpPost : ", url); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { // HttpManager.saveCookies(response); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } break; } catch (Exception e) { if (i == BFGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, BFGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!BFGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:edu.harvard.iq.dataverse.ingest.metadataextraction.impl.plugins.fits.FITSFileMetadataExtractorSpi.java
@Override public boolean canDecodeInput(BufferedInputStream stream) throws IOException { if (stream == null) { throw new IllegalArgumentException("stream == null!"); }/*w w w .java 2s .c om*/ byte[] b = new byte[FITS_HEADER_SIZE]; if (stream.markSupported()) { stream.mark(0); } int nbytes = stream.read(b, 0, FITS_HEADER_SIZE); if (nbytes == 0) { throw new IOException(); } //printHexDump(b, "hex dump of the byte-array"); dbgLog.info("hex dump of the 1st " + FITS_HEADER_SIZE + " bytes:" + (new String(Hex.encodeHex(b))).toUpperCase()); if (stream.markSupported()) { stream.reset(); } boolean DEBUG = false; String hdr4fits = new String(b); if (hdr4fits.equals(FITS_FILE_SIGNATURE)) { dbgLog.fine("this is a fits file"); return true; } else { dbgLog.fine("this is NOT a fits file"); return false; } }