List of usage examples for java.io BufferedInputStream reset
public synchronized void reset() throws IOException
reset
method of InputStream
. From source file:com.baofeng.game.sdk.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {//from w w w . j a v a 2 s .co 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" + 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 {/* w w w . ja v a2 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.baofeng.game.sdk.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {/*from ww w . j a v a 2 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:com.pipi.studio.dev.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {/*from www . j av a 2 s . c om*/ if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); for (RequestParameter p : parameter) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(Utils.encode(p.getName())); bulider.append("="); bulider.append(Utils.encode(p.getValue())); } url += "?" + bulider.toString(); } LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url); request = new HttpGet(url); /*if(Constants.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) { 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(); // ByteArrayOutputStream content = new ByteArrayOutputStream(); // response.getEntity().writeTo(content); // ret = new String(content.toByteArray()).trim(); // content.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("ERROR.HTTP.001", exception.getMessage()); } LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " finished !"); } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, Constants.ERROR_MESSAGE); ret = ErrorUtil.errorJson("ERROR.HTTP.002", exception.getMessage()); LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " onFail " + e.getMessage()); } catch (org.apache.http.conn.ConnectTimeoutException e) { RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE); ret = ErrorUtil.errorJson("ERROR.HTTP.003", exception.getMessage()); LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " onFail " + e.getMessage()); } catch (java.net.SocketTimeoutException e) { RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE); ret = ErrorUtil.errorJson("ERROR.HTTP.004", exception.getMessage()); LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " onFail " + e.getMessage()); } catch (UnsupportedEncodingException e) { RequestException exception = new RequestException(RequestException.UNSUPPORTED_ENCODEING_EXCEPTION, "?"); ret = ErrorUtil.errorJson("ERROR.HTTP.005", exception.getMessage()); LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " UnsupportedEncodingException " + e.getMessage()); } catch (org.apache.http.conn.HttpHostConnectException e) { RequestException exception = new RequestException(RequestException.CONNECT_EXCEPTION, Constants.ERROR_MESSAGE); ret = ErrorUtil.errorJson("ERROR.HTTP.006", exception.getMessage()); LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " HttpHostConnectException " + e.getMessage()); } catch (ClientProtocolException e) { RequestException exception = new RequestException(RequestException.CLIENT_PROTOL_EXCEPTION, "??"); ret = ErrorUtil.errorJson("ERROR.HTTP.007", exception.getMessage()); e.printStackTrace(); LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " ClientProtocolException " + e.getMessage()); } catch (IOException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??"); ret = ErrorUtil.errorJson("ERROR.HTTP.008", exception.getMessage()); e.printStackTrace(); LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet request to url :" + url + " IOException " + e.getMessage()); } finally { if (!Constants.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; LogUtil.d("result", ret); msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } //request.// if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.dta.DTAFileReaderSpi.java
@Override public boolean canDecodeInput(Object source) throws IOException { if (!(source instanceof BufferedInputStream)) { return false; }/*from w ww .ja v a 2 s . c om*/ if (source == null) { throw new IllegalArgumentException("stream == null!"); } BufferedInputStream stream = (BufferedInputStream) source; dbgLog.fine("applying the dta test\n"); byte[] b = new byte[DTA_HEADER_SIZE]; if (stream.markSupported()) { stream.mark(0); } int nbytes = stream.read(b, 0, DTA_HEADER_SIZE); if (nbytes == 0) { throw new IOException(); } if (stream.markSupported()) { stream.reset(); } dbgLog.info("hex dump: 1st 4bytes =>" + new String(Hex.encodeHex(b)) + "<-"); if (b[2] != 1) { dbgLog.fine("3rd byte is not 1: given file is not stata-dta type"); return false; } else if ((b[1] != 1) && (b[1] != 2)) { dbgLog.fine("2nd byte is neither 0 nor 1: this file is not stata-dta type"); return false; } else if (!DTAFileReaderSpi.stataReleaseNumber.containsKey(b[0])) { dbgLog.fine("1st byte (" + b[0] + ") is not within the ingestable range [rel. 3-10]:" + "this file is NOT stata-dta type"); return false; } else { dbgLog.fine("this file is stata-dta type: " + DTAFileReaderSpi.stataReleaseNumber.get(b[0]) + "(No in byte=" + b[0] + ")"); return true; } }
From source file:org.craftercms.studio.impl.deployment.PreviewDeployer.java
protected void writeFile(final String path, final InputStream content) throws IOException { BufferedInputStream contentStream = new BufferedInputStream(content); StringBuilder sbSavePath = new StringBuilder(previewStoreRootPath); sbSavePath.append(File.separator); sbSavePath.append(path);//from w w w . j ava2 s.c om String savePath = sbSavePath.toString(); savePath = savePath.replaceAll(File.separator + "+", File.separator); File file = new File(savePath); OutputStream outputStream = null; try { contentStream.mark(0); contentStream.reset(); // create new file if doesn't exist if (!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } outputStream = new FileOutputStream(file); IOUtils.copy(contentStream, outputStream); outputStream.flush(); } catch (FileNotFoundException e) { if (log.isErrorEnabled()) { log.error("Error: not able to open output stream for file " + path); } throw e; } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Error: not able to write file " + path); } throw e; } finally { IOUtils.closeQuietly(outputStream); } }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.dta.DTAFileReaderSpi.java
@Override public boolean canDecodeInput(Object source) throws IOException { if (!(source instanceof BufferedInputStream)) { return false; }// w ww. ja va2s . c o m if (source == null) { throw new IllegalArgumentException("stream == null!"); } BufferedInputStream stream = (BufferedInputStream) source; dbgLog.fine("applying the dta test\n"); byte[] b = new byte[DTA_HEADER_SIZE]; if (stream.markSupported()) { stream.mark(0); } int nbytes = stream.read(b, 0, DTA_HEADER_SIZE); if (nbytes == 0) { throw new IOException(); } //printHexDump(b, "hex dump of the byte-array"); if (stream.markSupported()) { stream.reset(); } dbgLog.info("hex dump: 1st 4bytes =>" + new String(Hex.encodeHex(b)) + "<-"); if (b[2] != 1) { dbgLog.fine("3rd byte is not 1: given file is not stata-dta type"); return false; } else if ((b[1] != 1) && (b[1] != 2)) { dbgLog.fine("2nd byte is neither 0 nor 1: this file is not stata-dta type"); return false; } else if (!DTAFileReaderSpi.stataReleaseNumber.containsKey(b[0])) { dbgLog.fine("1st byte (" + b[0] + ") is not within the ingestable range [rel. 3-10]:" + "this file is NOT stata-dta type"); return false; } else { dbgLog.fine("this file is stata-dta type: " + DTAFileReaderSpi.stataReleaseNumber.get(b[0]) + "(No in byte=" + b[0] + ")"); return true; } }
From source file:com.google.acre.script.NHttpAsyncUrlfetch.java
private Scriptable callback_result(long start_time, URL url, HttpResponse res, boolean system, boolean log_to_user, String response_encoding) { BrowserCompatSpecFactory bcsf = new BrowserCompatSpecFactory(); CookieSpec cspec = bcsf.newInstance(null); String protocol = url.getProtocol(); boolean issecure = ("https".equals(protocol)); int port = url.getPort(); if (port == -1) port = 80;// w w w . j a v a2 s . c o m CookieOrigin origin = new CookieOrigin(url.getHost(), port, url.getPath(), issecure); Object body = ""; int status = res.getStatusLine().getStatusCode(); Context ctx = Context.getCurrentContext(); Scriptable out = ctx.newObject(_scope); Scriptable headers = ctx.newObject(_scope); Scriptable cookies = ctx.newObject(_scope); out.put("status", out, status); out.put("headers", out, headers); out.put("cookies", out, cookies); Header content_type_header = null; StringBuilder response_header_log = new StringBuilder(); for (Header h : res.getAllHeaders()) { if (h.getName().equalsIgnoreCase("set-cookie")) { String set_cookie = h.getValue(); Matcher m = Pattern.compile("\\s*(([^,]|(,\\s*\\d))+)").matcher(set_cookie); while (m.find()) { Header ch = new BasicHeader("Set-Cookie", set_cookie.substring(m.start(), m.end())); try { List<Cookie> pcookies = cspec.parse(ch, origin); for (Cookie c : pcookies) { cookies.put(c.getName(), cookies, new AcreCookie(c).toJsObject(_scope)); } } catch (MalformedCookieException e) { throw new RuntimeException(e); } } } else if (h.getName().equalsIgnoreCase("content-type")) { content_type_header = h; } response_header_log.append(h.getName() + ": " + h.getValue() + "\r\n"); headers.put(h.getName(), headers, h.getValue()); } String charset = null; if (content_type_header != null) { HeaderElement values[] = content_type_header.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } if (charset == null) charset = response_encoding; // read body HttpEntity ent = res.getEntity(); try { if (ent != null) { InputStream res_stream = ent.getContent(); Header cenc = ent.getContentEncoding(); if (cenc != null && res_stream != null) { HeaderElement[] codecs = cenc.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { res_stream = new GZIPInputStream(res_stream); } } } long first_byte_time = 0; long end_time = 0; if (content_type_header != null && (content_type_header.getValue().startsWith("image/") || content_type_header.getValue().startsWith("application/octet-stream") || content_type_header.getValue().startsWith("multipart/form-data"))) { // HttpClient's InputStream doesn't support mark/reset, so // wrap it with one that does. BufferedInputStream bufis = new BufferedInputStream(res_stream); bufis.mark(2); bufis.read(); first_byte_time = System.currentTimeMillis(); bufis.reset(); byte[] data = IOUtils.toByteArray(bufis); end_time = System.currentTimeMillis(); body = new JSBinary(); ((JSBinary) body).set_data(data); try { if (res_stream != null) res_stream.close(); } catch (IOException e) { // ignore } } else if (res_stream == null || charset == null) { first_byte_time = end_time = System.currentTimeMillis(); body = ""; } else { StringWriter writer = new StringWriter(); Reader reader = new InputStreamReader(res_stream, charset); int i = reader.read(); first_byte_time = System.currentTimeMillis(); writer.write(i); IOUtils.copy(reader, writer); end_time = System.currentTimeMillis(); body = writer.toString(); try { reader.close(); writer.close(); } catch (IOException e) { // ignore } } long reading_time = end_time - first_byte_time; long waiting_time = first_byte_time - start_time; String httprephdr = response_header_log.toString(); // XXX need to log start-time of request _logger.syslog4j("DEBUG", "urlfetch.response.async", "URL", url.toString(), "Status", Integer.toString(status), "Headers", httprephdr, "Reading time", reading_time, "Waiting time", waiting_time); if (system && log_to_user) { _response.userlog4j("DEBUG", "urlfetch.response.async", "URL", url.toString(), "Status", Integer.toString(status), "Headers", httprephdr); } // XXX seems like AcreResponse should be able to use // the statistics object to generate x-metaweb-cost // given a bit of extra information Statistics.instance().collectUrlfetchTime(start_time, first_byte_time, end_time); _costCollector.collect((system) ? "asuc" : "auuc").collect((system) ? "asuw" : "auuw", waiting_time); } } catch (IOException e) { throw new RuntimeException(e); } out.put("body", out, body); return out; }
From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.dta.DTAFileReaderSpi.java
@Override public boolean canDecodeInput(BufferedInputStream stream) throws IOException { if (stream == null) { throw new IllegalArgumentException("stream == null!"); }//from w ww .ja va 2 s. c om dbgLog.fine("applying the dta test\n"); byte[] b = new byte[DTA_HEADER_SIZE]; if (stream.markSupported()) { stream.mark(0); } int nbytes = stream.read(b, 0, DTA_HEADER_SIZE); if (nbytes == 0) { throw new IOException(); } if (stream.markSupported()) { stream.reset(); } dbgLog.info("hex dump: 1st 4bytes =>" + new String(Hex.encodeHex(b)) + "<-"); if (b[2] != 1) { dbgLog.fine("3rd byte is not 1: given file is not stata-dta type"); return false; } else if ((b[1] != 1) && (b[1] != 2)) { dbgLog.fine("2nd byte is neither 0 nor 1: this file is not stata-dta type"); return false; } else if (!DTAFileReaderSpi.stataReleaseNumber.containsKey(b[0])) { dbgLog.fine("1st byte (" + b[0] + ") is not within the ingestable range [rel. 3-10]:" + "this file is NOT stata-dta type"); return false; } else { dbgLog.fine("this file is stata-dta type: " + DTAFileReaderSpi.stataReleaseNumber.get(b[0]) + "(No in HEX=" + b[0] + ")"); return true; } }
From source file:org.gtdfree.model.GTDDataXMLTools.java
static public DataHeader load(GTDModel model, InputStream in) throws XMLStreamException, IOException { model.setSuspendedForMultipleChanges(true); model.getDataRepository().suspend(true); XMLStreamReader r;/* ww w. java 2 s.c o m*/ try { // buffer size is same as default in 1.6, we explicitly request it so, not to brake if defaut changes. BufferedInputStream bin = new BufferedInputStream(in, 8192); bin.mark(8191); Reader rr = new InputStreamReader(bin); CharBuffer b = CharBuffer.allocate(96); rr.read(b); b.position(0); //System.out.println(b); Pattern pattern = Pattern.compile("<\\?.*?encoding\\s*?=.*?\\?>", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$ Matcher matcher = pattern.matcher(b); // reset back to start of file bin.reset(); // we check if encoding is defined in xml, by the book encoding on r should be null if not defined in xml, // but in reality it can be arbitrary if not defined in xml. So we have to check ourselves. if (matcher.find()) { //System.out.println(matcher); // if defined, then XML parser will pick it up and use it r = XMLInputFactory.newInstance().createXMLStreamReader(bin); Logger.getLogger(GTDDataXMLTools.class).info("XML declared encoding: " + r.getEncoding() //$NON-NLS-1$ + ", system default encoding: " + Charset.defaultCharset()); //$NON-NLS-1$ } else { //System.out.println(matcher); // if not defined, then we assume it is generated by gtd-free version 0.4 or some local editor, // so we assume system default encoding. r = XMLInputFactory.newInstance().createXMLStreamReader(new InputStreamReader(bin)); Logger.getLogger(GTDDataXMLTools.class) .info("XML assumed system default encoding: " + Charset.defaultCharset()); //$NON-NLS-1$ } r.nextTag(); if ("gtd-data".equals(r.getLocalName())) { //$NON-NLS-1$ DataHeader dh = new DataHeader(null, r.getAttributeValue(null, "version"), //$NON-NLS-1$ r.getAttributeValue(null, "modified")); //$NON-NLS-1$ if (dh.version != null) { if (dh.version.equals("2.0")) { //$NON-NLS-1$ r.nextTag(); _load_2_0(model, r); return dh; } } String s = r.getAttributeValue(null, "lastActionID"); //$NON-NLS-1$ if (s != null) { try { model.setLastActionID(Integer.parseInt(s)); } catch (Exception e) { Logger.getLogger(GTDDataXMLTools.class).debug("Internal error.", e); //$NON-NLS-1$ } } if (dh.version != null) { if (dh.version.equals("2.1")) { //$NON-NLS-1$ r.nextTag(); _load_2_1(model, r); return dh; } if (dh.version.startsWith("2.2")) { //$NON-NLS-1$ r.nextTag(); _load_2_2(model, r); return dh; } } throw new IOException("XML gtd-free data with version number " + dh.version //$NON-NLS-1$ + " can not be imported. Data version is newer then supported versions. Update your GTD-Free application to latest version."); //$NON-NLS-1$ } _load_1_0(model, r); return null; } catch (XMLStreamException e) { if (e.getNestedException() != null) { Logger.getLogger(GTDDataXMLTools.class).debug("Parse error.", e.getNestedException()); //$NON-NLS-1$ } else { Logger.getLogger(GTDDataXMLTools.class).debug("Parse error.", e); //$NON-NLS-1$ } throw e; } catch (IOException e) { throw e; } finally { model.setSuspendedForMultipleChanges(false); model.getDataRepository().suspend(false); } }