List of usage examples for java.io DataInputStream read
public final int read(byte b[]) throws IOException
b
. From source file:org.nuxeo.theme.presets.PaletteParser.java
public static Map<String, String> parse(InputStream in, String filename) throws IOException, PaletteIdentifyException, PaletteParseException { DataInputStream dis = new DataInputStream(in); byte[] bytes = new byte[dis.available()]; dis.read(bytes); return parse(bytes, filename); }
From source file:com.polivoto.networking.ServicioDeIPExterna.java
public static String obtenerIPExterna() { String ip = null;//from w w w . j a v a2 s .c o m 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.polivoto.networking.ServicioDeIPExterna.java
public static String obtenerIPServidorRemoto() { String ip = null;//from w ww . j a va 2 s . c o m 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:nl.dreamkernel.s4.tweaker.util.RuntimeExec.java
public static String[] execute(String[] commands, boolean needResponce) { try {//from ww w . j a v a 2s . co m Process process = Runtime.getRuntime().exec(commands); if (needResponce) { DataInputStream inputStream = new DataInputStream(process.getInputStream()); if (inputStream != null) { String ret = ""; int size = 0; byte[] buffer = new byte[1024]; try { do { size = inputStream.read(buffer); if (size > 0) { ret += new String(buffer, 0, size, HTTP.UTF_8); } } while (inputStream.available() > 0); } catch (IOException e) { } return ret.split("\n"); } } } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:org.eclipse.swt.snippets.Snippet319.java
static MyType restoreFromByteArray(byte[] bytes) { DataInputStream dataInStream = null; try {/*from w w w . j a v a2s .c om*/ ByteArrayInputStream byteInStream = new ByteArrayInputStream(bytes); dataInStream = new DataInputStream(byteInStream); int size = dataInStream.readInt(); byte[] name = new byte[size]; dataInStream.read(name); MyType result = new MyType(); result.name = new String(name); result.time = dataInStream.readLong(); return result; } catch (IOException ex) { return null; } finally { if (dataInStream != null) { try { dataInStream.close(); } catch (IOException e) { } } } }
From source file:tudarmstadt.lt.ABSentiment.featureExtractor.util.W2vSpace.java
/** * Read a Vector - Array of Floats from the binary model * @param ds input data stream//from w ww.j a v a 2s . co m * @param vectorSize length of each word vector * @return an array of float containing the word vector representation */ public static float[] readFloatVector(DataInputStream ds, int vectorSize) throws IOException { // Vector is an Array of Floats... float[] vector = new float[vectorSize]; // Floats stored as 4 bytes byte[] vectorBuffer = new byte[4 * vectorSize]; // Read the full vector in a single chunk: ds.read(vectorBuffer); // Parse bytes into floats for (int i = 0; i < vectorSize; i++) { // & with 0xFF to get unsigned byte value as int int byte1 = (vectorBuffer[(i * 4) + 0] & 0xFF) << 0; int byte2 = (vectorBuffer[(i * 4) + 1] & 0xFF) << 8; int byte3 = (vectorBuffer[(i * 4) + 2] & 0xFF) << 16; int byte4 = (vectorBuffer[(i * 4) + 3] & 0xFF) << 24; // Encode the 4 byte values (0-255) above into a single int // Reverse bytes for endian compatibility int reverseBytes = (byte1 | byte2 | byte3 | byte4); vector[i] = Float.intBitsToFloat(reverseBytes); } return vector; }
From source file:com.gson.util.HttpKit.java
/** * //from w w w . j a v a 2s.co m * @param url * @param params * @param file * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws NoSuchProviderException * @throws KeyManagementException */ public static String upload(String url, File file) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException { String BOUNDARY = "----WebKitFormBoundaryiDGnV9zdZA1eM1yL"; // ? StringBuffer bufferRes = null; URL urlGet = new URL(url); HttpURLConnection conn = (HttpURLConnection) urlGet.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// ?? StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"media\";filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] data = sb.toString().getBytes(); out.write(data); DataInputStream fs = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = fs.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } out.write("\r\n".getBytes()); // fs.close(); out.write(end_data); out.flush(); out.close(); // BufferedReader???URL? InputStream in = conn.getInputStream(); BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET)); String valueString = null; bufferRes = new StringBuffer(); while ((valueString = read.readLine()) != null) { bufferRes.append(valueString); } in.close(); if (conn != null) { // conn.disconnect(); } return bufferRes.toString(); }
From source file:com.hichengdai.qlqq.front.util.HttpKit.java
/** * //from www . j a v a 2 s .co m * * @param url * @param params * @param file * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws NoSuchProviderException * @throws KeyManagementException */ public static String upload(String url, File file) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException { String BOUNDARY = "----WebKitFormBoundaryiDGnV9zdZA1eM1yL"; // ? StringBuffer bufferRes = null; URL urlGet = new URL(url); HttpURLConnection conn = (HttpURLConnection) urlGet.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// ?? StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"media\";filename=\"" + file.getName() + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] data = sb.toString().getBytes(); out.write(data); DataInputStream fs = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = fs.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } out.write("\r\n".getBytes()); // fs.close(); out.write(end_data); out.flush(); out.close(); // BufferedReader???URL? InputStream in = conn.getInputStream(); BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET)); String valueString = null; bufferRes = new StringBuffer(); while ((valueString = read.readLine()) != null) { bufferRes.append(valueString); } in.close(); if (conn != null) { // conn.disconnect(); } return bufferRes.toString(); }
From source file:com.webarch.common.net.http.HttpService.java
/** * ?/*from w w w. j a va2s.com*/ * * @param protocol ??http?https?ftp?file?jar * @param host ? * @param file * @return */ public static String getRemoteFile(String protocol, String host, String file) { String content = null; try { URL url = new URL(protocol, host, file); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream input = new DataInputStream(conn.getInputStream()); byte[] buffer = new byte[8192]; int count = input.read(buffer); if (count > 0) { byte[] strBuffer = new byte[count]; System.arraycopy(buffer, 0, strBuffer, 0, count); content = new String(strBuffer, "UTF-8"); } input.close(); } catch (Exception e) { e.printStackTrace(); } //? return StringSeriesTools.replaceAllMatch(content, "\\s*|\t|\r|\n", ""); }
From source file:Main.java
/** * The URL looks like a request for a resource, such as a JavaScript or CSS file. Write * the given resource to the response output in binary format (needed for images). *///from w ww . j a v a2 s . c om public static void readWriteBinaryUtil(String sxURL, String resource, OutputStream outStream) throws IOException { DataOutputStream outData = null; DataInputStream inData = null; int byteCnt = 0; byte[] buffer = new byte[4096]; // get full qualified path of class System.out.println("RW Base Directory - " + sxURL); // remove class and add resource relative path sxURL = getResourceURL(sxURL, resource); System.out.println("RW Loading - " + sxURL); try { outData = new DataOutputStream(outStream); inData = new DataInputStream(new URL(sxURL).openConnection().getInputStream()); while ((byteCnt = inData.read(buffer)) != -1) { if (outData != null && byteCnt > 0) { outData.write(buffer, 0, byteCnt); } } } catch (IOException e) { throw e; } finally { try { if (inData != null) { inData.close(); } } catch (IOException ioe) { } } }