List of usage examples for java.io DataInputStream close
public void close() throws IOException
From source file:org.csploit.android.core.System.java
private static void preloadVendors() { if (mVendors == null) { try {//from w w w.j a v a 2 s. c om mVendors = new HashMap<>(); @SuppressWarnings("ConstantConditions") FileInputStream fstream = new FileInputStream( mContext.getFilesDir().getAbsolutePath() + "/tools/nmap/nmap-mac-prefixes"); DataInputStream in = new DataInputStream(fstream); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!line.startsWith("#") && !line.isEmpty()) { String[] tokens = line.split(" ", 2); if (tokens.length == 2) mVendors.put(NetworkHelper.getOUICode(tokens[0]), tokens[1]); } } in.close(); } catch (Exception e) { errorLogging(e); } } }
From source file:cd.education.data.collector.android.tasks.FormLoaderTask.java
/** * Read serialized {@link FormDef} from file and recreate as object. * * @param formDef/*ww w . j a va 2 s . c o m*/ * serialized FormDef file * @return {@link FormDef} object */ public FormDef deserializeFormDef(File formDef) { // TODO: any way to remove reliance on jrsp? FileInputStream fis = null; FormDef fd = null; try { // create new form def fd = new FormDef(); fis = new FileInputStream(formDef); DataInputStream dis = new DataInputStream(fis); // read serialized formdef into new formdef fd.readExternal(dis, ExtUtil.defaultPrototypes()); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); fd = null; } catch (IOException e) { e.printStackTrace(); fd = null; } catch (DeserializationException e) { e.printStackTrace(); fd = null; } catch (Exception e) { e.printStackTrace(); fd = null; } return fd; }
From source file:ClassPath.java
/** * @param name// w w w.ja v a 2 s. c om * fully qualified file name, e.g. java/lang/String * @param suffix * file name ends with suffix, e.g. .java * @return byte array for file on class path */ public byte[] getBytes(String name, String suffix) throws IOException { DataInputStream dis = null; try { InputStream is = getInputStream(name, suffix); if (is == null) { throw new IOException("Couldn't find: " + name + suffix); } dis = new DataInputStream(is); byte[] bytes = new byte[is.available()]; dis.readFully(bytes); return bytes; } finally { if (dis != null) { dis.close(); } } }
From source file:org.mobisocial.corral.CorralS3Connector.java
Uri downloadAndDecrypt(String ticket, String datestr, String objName, File cachefile, String mykey, CorralDownloadFuture future, DownloadProgressCallback callback) throws IOException, GeneralSecurityException { Log.d(TAG, "-----DOWNLOAD+DECRYPT START-----" + (String.valueOf(System.currentTimeMillis()))); Log.d(TAG, SERVER_URL + objName); Log.d(TAG, "Authorization: AWS " + ticket); Log.d(TAG, "Date: " + datestr); HttpClient http = new DefaultHttpClient(); HttpGet get = new HttpGet(SERVER_URL + objName); get.addHeader("Authorization", "AWS " + ticket); get.addHeader("Date", datestr); HttpResponse response = http.execute(get); DataInputStream is = new DataInputStream(response.getEntity().getContent()); long contentLength = response.getEntity().getContentLength(); if (!cachefile.exists()) { File tmpFile = new File(cachefile.getAbsoluteFile() + ".tmp"); tmpFile.getParentFile().mkdirs(); try {/*w ww. j av a 2 s.c om*/ CryptUtil cu = new CryptUtil(mykey); cu.InitCiphers(); FileOutputStream fos = new FileOutputStream(tmpFile); cu.decrypt(is, fos, contentLength, callback); try { is.close(); } catch (IOException e) { } try { fos.close(); } catch (IOException e) { } tmpFile.renameTo(cachefile); } catch (IOException e) { if (tmpFile.exists()) { tmpFile.delete(); } throw e; } catch (GeneralSecurityException e) { throw e; } } return cachefile.exists() ? Uri.fromFile(cachefile) : null; }
From source file:com.github.ambry.commons.BlobIdTest.java
/** * Gets the version number from a blobId string. * @param blobId The blobId string to get version number. * @return Version number/*from www . j a v a2 s. c o m*/ * @throws Exception Any unexpected exception. */ private short getVersionFromBlobString(String blobId) throws Exception { DataInputStream dis = new DataInputStream( new ByteBufferInputStream(ByteBuffer.wrap(Base64.decodeBase64(blobId)))); try { return dis.readShort(); } finally { dis.close(); } }
From source file:com.hadoopvietnam.cache.memcached.MemcachedCache.java
/** * Convert the memcached object into a List<Long>. * * @param inBytesOfLongs the byte[] to convert * @return the byte[] as List<Long>, null if not valid or empty bytes * @throws IOException thrown if any errors *///w ww . j a v a 2 s . co m protected ArrayList<Long> getListFromBytes(final Object inBytesOfLongs) throws IOException { if (inBytesOfLongs == null) { return null; } ArrayList<Long> toReturn = new ArrayList<Long>(); ByteArrayInputStream bytes = new ByteArrayInputStream((byte[]) inBytesOfLongs); DataInputStream input = new DataInputStream(bytes); try { while (input.available() > 0) { toReturn.add(input.readLong()); } } finally { input.close(); } return toReturn; }
From source file:com.jk.framework.util.FakeRunnable.java
/** * Read stream.//w w w . ja va 2s.c o m * * @param inStream * InputStream * @return String * @throws IOException * Signals that an I/O exception has occurred. */ public static byte[] readStream(final InputStream inStream) throws IOException { DataInputStream in = null; try { in = new DataInputStream(inStream); final int size = in.available(); final byte arr[] = new byte[size]; in.readFully(arr); return arr; } finally { if (in != null) { in.close(); } } }
From source file:com.mulesoft.example.fileconsumer.FileConsumerTestCase.java
@Test public void fileConsumer() throws Exception { MuleClient client = muleContext.getClient(); String sourceFileName = "sourceTestFile.csv"; URL url = IOUtils.getResourceAsUrl(sourceFileName, getClass()); FileUtils.copyFile(new File(url.toURI()), new File(properties.getProperty("FileConsumer.sourceFolder") + "/" + sourceFileName)); FileInputStream file = new FileInputStream(new File(url.toURI())); DataInputStream is = new DataInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line;/*from w w w .j a v a 2 s. co m*/ try { while ((line = br.readLine()) != null) { MuleMessage result = client.request("vm://" + properties.getProperty("FileConsumer.stockDataTopic"), RECEIVE_TIMEOUT); assertNotNull(result); assertFalse(result.getPayload() instanceof NullPayload); String[] split = line.split(","); String payload = result.getPayloadAsString(); for (String item : split) { assertTrue(payload.contains(item)); } } } finally { is.close(); } }
From source file:com.mingsoft.weixin.util.UploadDownUtils.java
/** * ? ? //w w w .j a va 2 s. c o m * @param access_token ?? * @param msgType image?voice?videothumb * @param localFile * @return ? */ public String uploadMedia(String msgType, String localFile, HttpServletRequest request) { String media_id = null; String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + getAccessToken() + "&type=" + msgType; String local_url = this.getRealPath(request, localFile); // String local_url = localFile; try { File file = new File(local_url); if (!file.exists() || !file.isFile()) { log.error("==" + local_url); return null; } URL urlObj = new URL(url); HttpURLConnection con = (HttpURLConnection) urlObj.openConnection(); con.setRequestMethod("POST"); // Post????get? con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); // post?? // ? con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Charset", "UTF-8"); // String BOUNDARY = "----------" + System.currentTimeMillis(); con.setRequestProperty("content-type", "multipart/form-data; boundary=" + BOUNDARY); // StringBuilder sb = new StringBuilder(); sb.append("--"); // ////////? sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] head = sb.toString().getBytes("utf-8"); // ? OutputStream out = new DataOutputStream(con.getOutputStream()); out.write(head); // DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); // byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// ?? out.write(foot); out.flush(); out.close(); /** * ????,????? */ // con.getResponseCode(); try { // BufferedReader???URL? StringBuffer buffer = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) { // System.out.println(line); buffer.append(line); } String respStr = buffer.toString(); log.debug("==respStr==" + respStr); try { JSONObject dataJson = JSONObject.parseObject(respStr); media_id = dataJson.getString("media_id"); } catch (Exception e) { log.error("==respStr==" + respStr, e); try { JSONObject dataJson = JSONObject.parseObject(respStr); return dataJson.getString("errcode"); } catch (Exception e1) { } } } catch (Exception e) { log.error("??POST?" + e); } } catch (Exception e) { log.error("?!=" + local_url); log.error("?!", e); } finally { } return media_id; }
From source file:com.eviware.soapui.impl.wsdl.panels.teststeps.amf.SoapUIAMFConnection.java
/** * Processes the HTTP response body./*from ww w. j a v a 2 s . c o m*/ */ protected Object processHttpResponseBody(InputStream inputStream) throws ClassNotFoundException, IOException, ClientStatusException, ServerStatusException { DataInputStream din = new DataInputStream(inputStream); ActionMessage message = new ActionMessage(); actionContext.setRequestMessage(message); MessageDeserializer deserializer = new AmfMessageDeserializer(); deserializer.initialize(serializationContext, din, null/* trace */); deserializer.readMessage(message, actionContext); din.close(); context.setProperty(AMFResponse.AMF_RESPONSE_ACTION_MESSAGE, message); return processAmfPacket(message); }