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:Util.PacketGenerator.java

public static void GeneratePacket() {
    try {/* w  w w.j a v  a2  s  .c o  m*/
        File file = new File(
                "D:\\Mestrado\\Prototipo\\Trace\\Los Angeles\\equinix-sanjose.dirA.20120920-130000.UTC.anon.pcap.csv");
        File newFile = new File("D:\\Mestrado\\SketchMatrix\\trunk\\Tracer_100_Pacotes.csv");

        FileInputStream tracesFIS = new FileInputStream(file);
        DataInputStream tracesDIS = new DataInputStream(tracesFIS);
        BufferedReader tracesBR = new BufferedReader(new InputStreamReader(tracesDIS));

        FileWriter tracesFW = new FileWriter(newFile);

        String tracesStr = tracesBR.readLine();

        Random random = new Random();

        long numberOfPackets = 0;

        while (tracesStr != null && numberOfPackets < 100) {

            String[] packetTokens = tracesStr.split(",");

            long time = Long.parseLong(packetTokens[0]);
            long srcIP = Long.parseLong(packetTokens[1]);
            long dstIP = Long.parseLong(packetTokens[2]);
            int srcPort = Integer.parseInt(packetTokens[3]);
            int dstPort = Integer.parseInt(packetTokens[4]);
            int payload = random.nextInt(256);

            tracesFW.write(
                    time + "," + srcIP + "," + dstIP + "," + srcPort + "," + dstPort + "," + payload + "\n");

            tracesStr = tracesBR.readLine();
            numberOfPackets++;
            if ((numberOfPackets % 100000) == 0) {
                System.out.println(numberOfPackets / 1000 + "K Pacotes");
            }
        }
        tracesBR.close();
        tracesDIS.close();
        tracesFIS.close();
        tracesFW.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.lunix.cheata.utils.file.FileManager.java

public static String readFile(String fileName) {
    try {// w  ww. ja va2 s. c  om
        File file = new File(getDir().getAbsolutePath(), fileName);
        String fileContents;
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new DataInputStream(new FileInputStream(file))));
        fileContents = FileUtils.readFileToString(file);
        reader.close();
        return fileContents;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

public static short byteArrayToShort(byte[] input) {
    try {//from   w w w . j a  v  a  2s. c  o m
        ByteArrayInputStream bis = new ByteArrayInputStream(input);
        DataInputStream dis = new DataInputStream(bis);
        short numb = dis.readShort();
        dis.close();
        return numb;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:gaffer.utils.WritableToStringConverter.java

public static Writable deserialiseFromString(String serialised) throws IOException {
    // Convert the base64 string to a byte array
    byte[] b = Base64.decodeBase64(serialised);

    // Deserialise the writable from the byte array
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    DataInput in = new DataInputStream(bais);
    String className = Text.readString(in);
    try {//  w  w  w.  j  ava2 s.  com
        Writable writable = (Writable) Class.forName(className).newInstance();
        writable.readFields(in);
        return writable;
    } catch (InstantiationException e) {
        throw new IOException("Exception deserialising writable: " + e);
    } catch (IllegalAccessException e) {
        throw new IOException("Exception deserialising writable: " + e);
    } catch (ClassNotFoundException e) {
        throw new IOException("Exception deserialising writable: " + e);
    } catch (ClassCastException e) {
        throw new IOException("Exception deserialising writable: " + e);
    }
}

From source file:CounterServer.java

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 5;/*ww  w .  j a va2  s.  com*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    DataInputStream in = new DataInputStream(req.getInputStream());
    resp.setContentType("application/octet-stream");
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.writeInt(count);
    out.flush();
    byte[] buf = byteOut.toByteArray();
    resp.setContentLength(buf.length);
    ServletOutputStream servletOut = resp.getOutputStream();
    servletOut.write(buf);
    servletOut.close();
}

From source file:com.yattatech.io.ShalomFileReader.java

public Seminary read(String path) {

    Seminary seminary = null;//from  w ww .  ja v  a2 s  .c  om
    DataInputStream input = null;
    try {
        input = new DataInputStream(new FileInputStream(path));
        long checksum = input.readLong();
        input.readUTF(); // skip /n character
        String json = input.readUTF();
        long checksum2 = ChecksumCalculator.calculateChecksum(json);
        if (checksum == checksum2) {
            seminary = new Gson().fromJson(json, Seminary.class);
        }
    } catch (IOException ioe) {
        LOGGER.log(Level.SEVERE, ioe.getMessage());
    } finally {
        IOUtils.closeQuietly(input);
        return seminary;
    }
}

From source file:mr.robotto.engine.loader.file.MrMrrLoader.java

public MrMrrLoader(InputStream inputStream) {
    mStream = new DataInputStream(inputStream);
}

From source file:com.vaadin.testbench.testutils.ImageLoader.java

public static byte[] loadImageBytes(String folder, String filename) throws IOException {
    File imgFile = getImageFile(folder, filename);
    assertTrue(imgFile.exists());/*from  w  w w . jav  a  2s. c o m*/

    byte[] bytes = new byte[(int) imgFile.length()];
    new DataInputStream(new FileInputStream(imgFile)).readFully(bytes);
    return bytes;
}

From source file:com.mongodb.hadoop.util.BSONLoader.java

public BSONLoader(final InputStream input) {
    _input = new DataInputStream(input);
}

From source file:com.bigdata.dastor.db.RangeSliceReply.java

public static RangeSliceReply read(byte[] body) throws IOException {
    ByteArrayInputStream bufIn = new ByteArrayInputStream(body);
    DataInputStream dis = new DataInputStream(bufIn);
    int rowCount = dis.readInt();
    List<Row> rows = new ArrayList<Row>(rowCount);
    for (int i = 0; i < rowCount; i++) {
        rows.add(Row.serializer().deserialize(dis));
    }/*ww w .  j  av  a 2s .  co  m*/
    return new RangeSliceReply(rows);
}