List of usage examples for java.io DataInputStream close
public void close() throws IOException
From source file:com.android.internal.location.GpsXtraDownloader.java
protected static byte[] doDownload(String url, boolean isProxySet, String proxyHost, int proxyPort) { AndroidHttpClient client = null;//from ww w. j a va 2s .co m try { client = AndroidHttpClient.newInstance("Android"); HttpUriRequest req = new HttpGet(url); if (isProxySet) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); ConnRouteParams.setDefaultProxy(req.getParams(), proxy); } req.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); req.addHeader("x-wap-profile", "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#"); HttpResponse response = client.execute(req); StatusLine status = response.getStatusLine(); if (status.getStatusCode() != 200) { // HTTP 200 is success. Log.d(TAG, "HTTP error: " + status.getReasonPhrase()); return null; } HttpEntity entity = response.getEntity(); byte[] body = null; if (entity != null) { try { if (entity.getContentLength() > 0) { body = new byte[(int) entity.getContentLength()]; DataInputStream dis = new DataInputStream(entity.getContent()); try { dis.readFully(body); } finally { try { dis.close(); } catch (IOException e) { Log.e(TAG, "Unexpected IOException.", e); } } } } finally { if (entity != null) { entity.consumeContent(); } } } return body; } catch (Exception e) { Log.d(TAG, "error " + e); } finally { if (client != null) { client.close(); } } return null; }
From source file:com.polivoto.networking.ServicioDeIPExterna.java
public static String obtenerIPExterna() { String ip = null;/*from ww w .j av a 2 s . c om*/ try { HttpURLConnection con = (HttpURLConnection) new URL(GET_EXTERNAL_HOST).openConnection(); DataInputStream entrada = new DataInputStream(con.getInputStream()); int length; byte[] chunk = new byte[64]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((length = entrada.read(chunk)) != -1) baos.write(chunk, 0, length); ip = baos.toString(); baos.close(); entrada.close(); con.disconnect(); } catch (IOException e) { e.printStackTrace(); } System.out.println("IP exterior: " + ip); return ip; }
From source file:com.maydesk.base.util.PDUtil.java
public static byte[] readBytesFromFile(File file) throws IOException { byte[] bytesOfFile = new byte[(int) file.length()]; DataInputStream dis = new DataInputStream(new FileInputStream(file)); dis.readFully(bytesOfFile);/* ww w .j av a 2 s . c om*/ dis.close(); return bytesOfFile; }
From source file:com.android.server.location.GpsXtraDownloader.java
protected static byte[] doDownload(String url, boolean isProxySet, String proxyHost, int proxyPort) { if (DEBUG)// w w w .ja va2 s.c o m Log.d(TAG, "Downloading XTRA data from " + url); AndroidHttpClient client = null; try { client = AndroidHttpClient.newInstance("Android"); HttpUriRequest req = new HttpGet(url); if (isProxySet) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); ConnRouteParams.setDefaultProxy(req.getParams(), proxy); } req.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); req.addHeader("x-wap-profile", "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#"); HttpResponse response = client.execute(req); StatusLine status = response.getStatusLine(); if (status.getStatusCode() != 200) { // HTTP 200 is success. if (DEBUG) Log.d(TAG, "HTTP error: " + status.getReasonPhrase()); return null; } HttpEntity entity = response.getEntity(); byte[] body = null; if (entity != null) { try { if (entity.getContentLength() > 0) { body = new byte[(int) entity.getContentLength()]; DataInputStream dis = new DataInputStream(entity.getContent()); try { dis.readFully(body); } finally { try { dis.close(); } catch (IOException e) { Log.e(TAG, "Unexpected IOException.", e); } } } } finally { if (entity != null) { entity.consumeContent(); } } } return body; } catch (Exception e) { if (DEBUG) Log.d(TAG, "error " + e); } finally { if (client != null) { client.close(); } } return null; }
From source file:com.polivoto.networking.ServicioDeIPExterna.java
public static String obtenerIPServidorRemoto() { String ip = null;/*from w w w . j a va2s .c om*/ try { HttpURLConnection con = (HttpURLConnection) new URL(REMOTE_HOST).openConnection(); DataInputStream entrada = new DataInputStream(con.getInputStream()); int length; byte[] chunk = new byte[64]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((length = entrada.read(chunk)) != -1) baos.write(chunk, 0, length); JSONObject json = new JSONObject(baos.toString()); baos.close(); entrada.close(); con.disconnect(); ip = json.getString("content"); } catch (JSONException | IOException e) { e.printStackTrace(); } System.out.println("IP servidor remoto: " + ip); return ip; }
From source file:genepi.db.h2.H2Connector.java
public static String readFileAsString(InputStream is) throws java.io.IOException, URISyntaxException { DataInputStream in = new DataInputStream(is); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine;// w w w .j a v a 2s .c o m StringBuilder builder = new StringBuilder(); while ((strLine = br.readLine()) != null) { builder.append("\n"); builder.append(strLine); } in.close(); return builder.toString(); }
From source file:com.android.bandwidthtest.util.BandwidthTestUtil.java
/** * Parses the first line in a file if exists. * * @param file {@link File} the input/* w ww.j av a 2s .co m*/ * @return the integer value of the first line of the file. */ public static int parseIntValueFromFile(File file) { int value = 0; if (file.exists()) { try { FileInputStream fstream = new FileInputStream(file); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine = br.readLine(); if (strLine != null) { value = Integer.parseInt(strLine); } // Close the input stream in.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } return value; }
From source file:Main.java
public static String readFromInternalStorage(Context context, String fileName) { File file = new File(context.getFilesDir(), fileName); try {/*from ww w . j a v a2s. co m*/ FileInputStream fis = new FileInputStream(file); DataInputStream in = new DataInputStream(fis); BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = br.readLine()) != null) { stringBuilder.append(line); } String json = stringBuilder.toString(); br.close(); in.close(); fis.close(); return json; } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } return null; }
From source file:com.solace.samples.cloudfoundry.securesession.controller.SolaceController.java
/** * This utility function installs a certificate into the JRE's trusted * store. Normally you would not do this, but this is provided to * demonstrate how to use TLS, and have the client validate a self-signed * server certificate./*from w ww . ja v a2 s .c o m*/ * * @throws Exception */ private static void importCertificate() throws Exception { File file = new File(CERTIFICATE_FILE_NAME); logger.info("Loading certificate from " + file.getAbsolutePath()); // This loads the KeyStore from the default location // (i.e. default for a Clound Foundry app) using the default password. FileInputStream is = new FileInputStream(TRUST_STORE); char[] password = TRUST_STORE_PASSWORD.toCharArray(); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(is, password); is.close(); // Create an ByteArrayInputStream stream from the FileInputStream fis = new FileInputStream(CERTIFICATE_FILE_NAME); DataInputStream dis = new DataInputStream(fis); byte[] bytes = new byte[dis.available()]; dis.readFully(bytes); dis.close(); ByteArrayInputStream certstream = new ByteArrayInputStream(bytes); // This takes that Byte Array and creates a certificate out of it. CertificateFactory cf = CertificateFactory.getInstance("X.509"); Certificate certs = cf.generateCertificate(certstream); // Finally, store the new certificate in the keystore. keystore.setCertificateEntry(CERTIFICATE_ALIAS, certs); // Save the new keystore contents FileOutputStream out = new FileOutputStream(TRUST_STORE); keystore.store(out, password); out.close(); }
From source file:JALPTest.java
/** * Creates a Producer using the given command line params. * * @param xml the ApplicationMetadataXML * @param socketPath a String which is the path to the socket * @param privateKeyPath a String which is the path to the private key in DER format * @param publicKeyPath a String which is the path to the public key in DER format * @param certPath a String which is the path to the certificate * @param hasDigest a Boolean, true to set a digest method in the producer * @return the created Producer/*from ww w. ja v a 2s. c om*/ * @throws Exception */ private static Producer createProducer(ApplicationMetadataXML xml, String socketPath, String privateKeyPath, String publicKeyPath, String certPath, Boolean hasDigest) throws Exception { Producer producer = new Producer(xml); producer.setSocketFile(socketPath); if (privateKeyPath != null && !"".equals(privateKeyPath)) { File privateKeyFile = new File(privateKeyPath); DataInputStream privateDis = new DataInputStream(new FileInputStream(privateKeyFile)); byte[] privateKeyBytes = new byte[(int) privateKeyFile.length()]; privateDis.readFully(privateKeyBytes); privateDis.close(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); KeySpec privateKs = new PKCS8EncodedKeySpec(privateKeyBytes); PrivateKey privateKey = keyFactory.generatePrivate(privateKs); File publicKeyFile = new File(publicKeyPath); DataInputStream publicDis = new DataInputStream(new FileInputStream(publicKeyFile)); byte[] publicKeyBytes = new byte[(int) publicKeyFile.length()]; publicDis.readFully(publicKeyBytes); publicDis.close(); KeySpec publicKs = new X509EncodedKeySpec(publicKeyBytes); PublicKey publicKey = keyFactory.generatePublic(publicKs); producer.setPrivateKey(privateKey); producer.setPublicKey(publicKey); } if (certPath != null && !"".equals(certPath)) { InputStream inputStream = new FileInputStream(certPath); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream); inputStream.close(); producer.setCertificate(cert); } if (hasDigest) { producer.setDigestMethod(DMType.SHA256); } return producer; }