4 package org.pcmm.utils;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
11 public class PCMMUtils {
13 public static void WriteBinaryDump(String rootFileName, byte[] buffer) {
15 String fileName = "/tmp/" + rootFileName + "-" + java.util.UUID.randomUUID() + ".bin";
18 System.out.println("Open fileName " + fileName);
19 FileOutputStream outputStream = new FileOutputStream(fileName);
21 // write() writes as many bytes from the buffer
22 // as the length of the buffer. You can also
24 // write(buffer, offset, length)
25 // if you want to write a specific number of
26 // bytes, or only part of the buffer.
27 outputStream.write(buffer);
29 // Always close files.
32 System.out.println("Wrote " + buffer.length + " bytes");
33 } catch (IOException ex) {
34 System.out.println("Error writing file '" + fileName + "'");
35 // Or we could just do this:
36 // ex.printStackTrace();
40 public static byte[] ReadBinaryDump(String fileName) {
41 // The name of the file to open.
42 // String fileName = "COPSReportMessage.txt";
44 FileInputStream inputStream = new FileInputStream(fileName);
45 // Use this for reading the data.
46 byte[] buffer = new byte[inputStream.available()];
47 // read fills buffer with data and returns
48 // the number of bytes read (which of course
49 // may be less than the buffer size, but
50 // it will never be more).
51 int total = inputStream.read(buffer);
53 // Always close files.
56 System.out.println("Read " + total + " bytes");
58 } catch (FileNotFoundException ex) {
59 System.out.println("Unable to open file '" + fileName + "'");
60 } catch (IOException ex) {
61 System.out.println("Error reading file '" + fileName + "'");
62 // Or we could just do this:
63 // ex.printStackTrace();