List of usage examples for java.io DataInputStream read
public final int read(byte b[], int off, int len) throws IOException
len
bytes of data from the contained input stream into an array of bytes. From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] b = new byte[1]; Properties systemSettings = System.getProperties(); systemSettings.put("http.proxyHost", "proxy.mydomain.local"); systemSettings.put("http.proxyPort", "80"); URL u = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); BASE64Encoder encoder = new BASE64Encoder(); String encodedUserPwd = encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes()); con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd); DataInputStream di = new DataInputStream(con.getInputStream()); while (-1 != di.read(b, 0, 1)) { System.out.print(new String(b)); }/*from w w w. ja va2 s . c o m*/ }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] b = new byte[1]; Properties systemSettings = System.getProperties(); systemSettings.put("http.proxyHost", "proxy.mydomain.local"); systemSettings.put("http.proxyPort", "80"); Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("mydomain\\username", "password".toCharArray()); }/*w w w . j a v a 2 s. c o m*/ }); URL u = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); DataInputStream di = new DataInputStream(con.getInputStream()); while (-1 != di.read(b, 0, 1)) { System.out.print(new String(b)); } }
From source file:Main.java
public static void main(String[] args) throws IOException { InputStream is = new FileInputStream("c:\\test.txt"); DataInputStream dis = new DataInputStream(is); int count = is.available(); byte[] bs = new byte[count]; dis.read(bs, 4, 3); for (byte b : bs) { char c = (char) b; if (b == 0) { c = '0'; }/*from ww w.ja va2 s .com*/ System.out.print(c); } }
From source file:Main.java
public static void main(String args[]) throws Exception { byte[] b = new byte[1]; URL url = new URL("http://www.server.com/a.gif"); URLConnection urlConnection = url.openConnection(); urlConnection.connect();/*from ww w . j av a 2 s .com*/ DataInputStream di = new DataInputStream(urlConnection.getInputStream()); FileOutputStream fo = new FileOutputStream("a.gif"); while (-1 != di.read(b, 0, 1)) fo.write(b, 0, 1); di.close(); fo.close(); }
From source file:com.weibo.wesync.client.NHttpClient2.java
public static void main(String[] args) throws Exception { RSAPublicKey publicKey = RSAEncrypt.loadPublicKey("D:\\weibo\\meyou_gw\\conf\\public.pem"); // //w ww . j a va 2s. c o m byte[] cipher = RSAEncrypt.encrypt(publicKey, password.getBytes()); password = RSAEncrypt.toHexString(cipher); // HTTP parameters for the client HttpParams params = new SyncBasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000) .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true); // Create HTTP protocol processing chain HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Use standard client-side protocol interceptors new RequestContent(), new RequestTargetHost(), new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() }); // Create client-side HTTP protocol handler HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor(); // Create client-side I/O event dispatch final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, params); // Create client-side I/O reactor IOReactorConfig config = new IOReactorConfig(); config.setIoThreadCount(1); final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(config); // Create HTTP connection pool BasicNIOConnPool pool = new BasicNIOConnPool(ioReactor, params); // Limit total number of connections to just two pool.setDefaultMaxPerRoute(2); pool.setMaxTotal(1); // Run the I/O reactor in a separate thread Thread t = new Thread(new Runnable() { public void run() { try { // Ready to go! ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } System.out.println("Shutdown"); } }); // Start the client thread t.start(); // Create HTTP requester // HttpAsyncRequester requester = new HttpAsyncRequester( // httpproc, new DefaultConnectionReuseStrategy(), params); // Execute HTTP GETs to the following hosts and HttpHost[] targets = new HttpHost[] { // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), // new HttpHost("123.125.106.28", 8093, "http"), new HttpHost("123.125.106.28", 8082, "http") }; final CountDownLatch latch = new CountDownLatch(targets.length); int callbackId = 0; for (int i = 0; i < 1; i++) { for (final HttpHost target : targets) { BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/wesync"); // String usrpwd = Base64.encodeBase64String((username + ":" + password).getBytes()); // request.setHeader("authorization", "Basic " + usrpwd); request.setHeader("uid", "2565640713"); Meyou.MeyouPacket packet = null; if (callbackId == 0) { packet = Meyou.MeyouPacket.newBuilder().setCallbackId(String.valueOf(callbackId++)) .setSort(MeyouSort.notice).build(); } else { packet = Meyou.MeyouPacket.newBuilder().setCallbackId(String.valueOf(callbackId++)) .setSort(MeyouSort.wesync).build(); } ByteArrayEntity entity = new ByteArrayEntity(packet.toByteArray()); request.setEntity(entity); // BasicHttpRequest request = new BasicHttpRequest("GET", "/test.html"); System.out.println("send ..."); HttpAsyncRequester requester = new HttpAsyncRequester(httpproc, new DefaultConnectionReuseStrategy(), params); requester.execute(new BasicAsyncRequestProducer(target, request), new BasicAsyncResponseConsumer(), pool, new BasicHttpContext(), // Handle HTTP response from a callback new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { StatusLine status = response.getStatusLine(); int code = status.getStatusCode(); if (code == 200) { try { latch.countDown(); DataInputStream in; in = new DataInputStream(response.getEntity().getContent()); int packetLength = in.readInt(); int start = 0; while (packetLength > 0) { ByteArrayOutputStream outstream = new ByteArrayOutputStream( packetLength); byte[] buffer = new byte[1024]; int len = 0; while (start < packetLength && (len = in.read(buffer, start, packetLength)) > 0) { outstream.write(buffer, 0, len); start += len; } Meyou.MeyouPacket packet0 = Meyou.MeyouPacket .parseFrom(outstream.toByteArray()); System.out.println(target + "->" + packet0); if ((len = in.read(buffer, start, 4)) > 0) { packetLength = Util.readPacketLength(buffer); } else { break; } } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { System.out.println("error code=" + code + "|" + status.getReasonPhrase()); } } public void failed(final Exception ex) { latch.countDown(); System.out.println(target + "->" + ex); } public void cancelled() { latch.countDown(); System.out.println(target + " cancelled"); } }); Thread.sleep((long) (Math.random() * 10000)); } } // latch.await(); // System.out.println("Shutting down I/O reactor"); // ioReactor.shutdown(); // System.out.println("Done"); }
From source file:Main.java
public static void openNamedFile(String filename) { try {/*w ww .j ava 2 s.co m*/ File f = new File(filename); // Log.e("kuinfa", "filename= " + filename); FileInputStream fis = new FileInputStream(f); long size = f.length(); name = f.getName(); patch = f.getParentFile().toString(); DataInputStream dis = new DataInputStream(fis); byte[] b = new byte[(int) size]; int length = dis.read(b, 0, (int) size); dis.close(); fis.close(); String ttt = new String(b, 0, length, "UTF-8"); try { ttt = new String(ttt.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { } } catch (FileNotFoundException e) { } catch (IOException e) { } }
From source file:Main.java
/** * Read a number of bytes or throw.//from w w w . j a v a 2 s . co m * * @param size * The number of bytes read * @return The array of bytes read. * @throws IOException */ public static byte[] readBytes(DataInputStream stream, int size) throws IOException { byte[] buf = new byte[size]; int toRead = size; int done = 0; while (toRead > 0) { int read = stream.read(buf, done, toRead); if (read == -1) { throw new IOException(); } done += read; toRead -= read; } return buf; }
From source file:Main.java
static int readFirmwareFirmware(File fp) { try {//from w ww .j av a 2 s. co m FileInputStream fis = new FileInputStream(fp); DataInputStream dis = new DataInputStream(fis); if (dis.skipBytes(2048) != 2048) { // Log.v("DistoX", "failed skip"); return 0; // skip 8 bootloader blocks } byte[] buf = new byte[64]; if (dis.read(buf, 0, 64) != 64) { // Log.v("DistoX", "failed read"); return 0; } for (int k = 0; k < 64; ++k) { // Log.v("DistoX", "byte " + k + " " + buf[k] + " sign " + signature[k] ); if (k == 6 || k == 7 || k == 16 || k == 17) continue; if (buf[k] != signature[k]) return 0; } if (buf[7] == (byte) 0xf8) { if (buf[6] == (byte) 0x34) { return 21; } else if (buf[6] == (byte) 0x3a) { return 22; } } else if (buf[7] == (byte) 0xf9) { if (buf[6] == (byte) 0x90) { return 23; } } else if (buf[7] == (byte) 0xfa) { if (buf[6] == (byte) 0x0a) { return 24; } } } catch (IOException e) { } return 0; }
From source file:com.netflix.raigad.utils.SystemUtils.java
public static String getDataFromUrl(String url) { try {//from w w w . j a v a2 s. c om 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:org.locationtech.geomesa.bigtable.spark.BigtableInputFormatBase.java
public static BigtableExtendedScan stringToScan(String encoded) throws IOException { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(encoded))); int tableLength = dis.readInt(); byte[] table = new byte[tableLength]; dis.read(table, 0, tableLength); int available = dis.available(); byte[] rowsetbytes = new byte[available]; dis.readFully(rowsetbytes);//from www .jav a 2 s .c om RowSet rs = RowSet.parseFrom(rowsetbytes); BigtableExtendedScan scan = new BigtableExtendedScan(); rs.getRowRangesList().forEach(scan::addRange); scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, table); return scan; }