Example usage for java.io DataInputStream DataInputStream

List of usage examples for java.io DataInputStream DataInputStream

Introduction

In this page you can find the example usage for java.io DataInputStream DataInputStream.

Prototype

public DataInputStream(InputStream in) 

Source Link

Document

Creates a DataInputStream that uses the specified underlying InputStream.

Usage

From source file:com.scaleunlimited.classify.vectors.WritableComparableVectorTest.java

@Test
public void testToFromStream() throws Exception {
    WritableComparableVector vector1 = new WritableComparableVector(makeVector());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    vector1.write(dos);//from   w  w  w.  j  a va2  s.  c o m
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    WritableComparableVector vector2 = new WritableComparableVector(null);
    vector2.readFields(dis);
    compareVectors(vector1.getVector(), vector2.getVector());
}

From source file:fr.noop.subtitle.stl.StlParser.java

public StlObject parse(InputStream is, boolean strict)
        throws SubtitleParsingException, InvalidTimeRangeException {
    BufferedInputStream bis = new BufferedInputStream(is);
    DataInputStream dis = new DataInputStream(bis);

    // Create STL subtitle
    StlObject stl;/*ww w. j  av a 2  s. co m*/

    try {
        // Read GSI block
        StlGsi gsi = this.readGsi(dis);
        stl = new StlObject(gsi);
    } catch (IOException e) {
        throw new SubtitleParsingException("Unable to parse Gsi block");
    }

    // Iterate over all TTI blocks and parse them
    int subtitleIndex = 0;

    while (subtitleIndex++ < stl.getGsi().getTnb()) {
        StlTti tti;

        try {
            tti = this.readTti(dis, stl.getGsi());
        } catch (IOException e) {
            throw new SubtitleParsingException("Unable to parse tti block");
        }

        stl.addTti(tti);
    }

    return stl;
}

From source file:com.offbynull.peernetic.debug.visualizer.XStreamPlayer.java

/**
 * Constructs a {@link XStreamPlayer} object.
 * @param is input stream//from   w ww.  j a v a2  s. c  o m
 * @throws NullPointerException if any arguments are {@code null}
 */
public XStreamPlayer(InputStream is) {
    Validate.notNull(is);

    this.is = new DataInputStream(is);
    xstream = new XStream();
}

From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java

public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException {
    String type = "application/json";
    URL u = new URL(URL);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);/*from w w w.jav  a2  s  .c  o m*/
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", type);
    conn.setRequestProperty("User-Agent", UserAgent);
    OutputStream os = conn.getOutputStream();
    os.write(JSONRaw.getBytes());
    os.flush();
    os.close();

    String response = null;
    DataInputStream input = new DataInputStream(conn.getInputStream());
    while (null != ((response = input.readLine()))) {
        input.close();
        return response;
    }
    return null;
}

From source file:com.google.code.rptm.mailarchive.DefaultMailingListArchive.java

private static boolean isMboxFile(File file) throws IOException {
    byte[] firstBytes = new byte[5];
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from  w  w  w. j a v  a  2s .  co  m*/
        in.readFully(firstBytes);
    } finally {
        in.close();
    }
    return Arrays.equals(firstBytes, MBOX_MAGIC);
}

From source file:com.igormaznitsa.jhexed.values.HexSVGImageValue.java

public HexSVGImageValue(final InputStream in) throws IOException {
    super(in);//from   w  w  w. ja v a2 s  .  c o  m
    final DataInputStream din = in instanceof DataInputStream ? (DataInputStream) in : new DataInputStream(in);
    final byte[] buffer = new byte[din.readInt()];
    IOUtils.readFully(in, buffer);
    this.image = new SVGImage(new ByteArrayInputStream(buffer), true);
}

From source file:J2MEImageLoader.java

public void run() {
    HttpConnection hc = null;//  w w w .j a  va 2s  . c o  m
    DataInputStream in = null;

    try {
        hc = (HttpConnection) Connector.open("http://www.y.com/image.gif");
        int length = (int) hc.getLength();
        byte[] data = null;
        if (length != -1) {
            data = new byte[length];
            in = new DataInputStream(hc.openInputStream());
            in.readFully(data);
        } else {
            int chunkSize = 512;
            int index = 0;
            int readLength = 0;
            in = new DataInputStream(hc.openInputStream());
            data = new byte[chunkSize];
            do {
                if (data.length < index + chunkSize) {
                    byte[] newData = new byte[index + chunkSize];
                    System.arraycopy(data, 0, newData, 0, data.length);
                    data = newData;
                }
                readLength = in.read(data, index, chunkSize);
                index += readLength;
            } while (readLength == chunkSize);
            length = index;
        }
        Image image = Image.createImage(data, 0, length);
        ImageItem imageItem = new ImageItem(null, image, 0, null);
        mForm.append(imageItem);
        mForm.setTitle("Done.");
    } catch (IOException ioe) {
        mForm.setTitle("Error.");
    }
}

From source file:com.netflix.raigad.utils.SystemUtils.java

public static String getDataFromUrl(String url) {
    try {/*from   w  w w  .j  a  va2  s . c o  m*/
        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:CounterApp.java

public int getCount() throws Exception {
    java.net.URL url = new java.net.URL(servletURL);
    java.net.URLConnection con = url.openConnection();
    if (sessionCookie != null) {
        con.setRequestProperty("cookie", sessionCookie);
    }//from   ww  w . j  a va  2s.  co  m
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.flush();
    byte buf[] = byteOut.toByteArray();
    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();
    DataInputStream in = new DataInputStream(con.getInputStream());
    int count = in.readInt();
    in.close();
    if (sessionCookie == null) {
        String cookie = con.getHeaderField("set-cookie");
        if (cookie != null) {
            sessionCookie = parseCookie(cookie);
            System.out.println("Setting session ID=" + sessionCookie);
        }
    }

    return count;
}

From source file:epn.edu.ec.bibliotecadigital.cliente.Client.java

@Override
public void run() {
    try {//from www.  ja  va 2 s  . c  om
        clientSocketBalancer = new Socket(InetAddress.getByName(serverIP), portBalancer);
        DataInputStream dataInBalancer = new DataInputStream(clientSocketBalancer.getInputStream());
        DataOutputStream dataOut = new DataOutputStream(clientSocketBalancer.getOutputStream());
        dataOut.writeUTF((String) params[0]);//nombre de usuario
        String ipServer = dataInBalancer.readUTF();
        int portServer = dataInBalancer.readInt();
        clientSocketBalancer.close();
        Socket clientSocket = new Socket(ipServer, portServer);
        dataOut = new DataOutputStream(clientSocket.getOutputStream());
        dataOut.writeUTF(accion);
        dataOut.writeUTF((String) params[0]);//nombre de usuario
        InputStream in;
        DataInputStream dataIn;
        switch (accion) {
        case "bajar":

            dataOut = new DataOutputStream(clientSocket.getOutputStream());
            dataOut.writeUTF((String) params[1]);
            dataIn = new DataInputStream(clientSocket.getInputStream());
            boolean encontrado = dataIn.readBoolean();
            if (!encontrado) {
                System.out.println("Libro con el cdigo: " + params[1] + " no encontrado");
                break;
            }
            String fileName = dataIn.readUTF();
            System.out.println(
                    "Descargando libro " + fileName + " con cdigo " + params[1] + " en la carpeta Donwloads");
            String home = System.getProperty("user.home");

            in = clientSocket.getInputStream();
            try {
                FileOutputStream out = new FileOutputStream(new File(home + "\\Downloads\\" + fileName));
                byte[] bytes = new byte[64 * 1024];

                int count;
                while ((count = in.read(bytes)) > 0) {
                    out.write(bytes, 0, count);
                }
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(in);
            }
            break;
        case "subir":
            dataOut = new DataOutputStream(clientSocket.getOutputStream());
            dataOut.writeUTF(((File) params[1]).getName());
            OutputStream out = clientSocket.getOutputStream();
            try {
                byte[] bytes = new byte[64 * 1024];
                in = new FileInputStream((File) params[1]);

                int count;
                while ((count = in.read(bytes)) > 0) {
                    out.write(bytes, 0, count);
                }
                in.close();
            } finally {
                IOUtils.closeQuietly(out);
            }
            break;
        case "obtenerLista":
            ObjectInputStream inFromClient = new ObjectInputStream(clientSocket.getInputStream());
            System.out.println("Libros disponibles: \n");
            List<Libro> libros = (List<Libro>) inFromClient.readObject();
            System.out.println("\tCdigo\tNommbre\n");
            for (Libro lbr : libros) {
                System.out.println("\t" + lbr.getCodigolibro() + "\t" + lbr.getNombre());
            }
            inFromClient.close();
            break;
        case "verificar":

            break;
        }
        dataOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
}