Merge changes I99dfd3ce,Ib55b1ca2,I584ed58f,I0020da7e,If9376252
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / utils / PCMMUtils.java
1 /**
2  @header@
3  */
4 package org.pcmm.utils;
5
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13
14 public class PCMMUtils {
15
16     public final static Logger logger = LoggerFactory.getLogger(PCMMUtils.class);
17
18     public static void WriteBinaryDump(String rootFileName, byte[] buffer) {
19         // Make this Unique
20         String fileName = "/tmp/" + rootFileName + "-" + java.util.UUID.randomUUID() + ".bin";
21         try {
22
23             logger.info("Open fileName " + fileName);
24             FileOutputStream outputStream = new FileOutputStream(fileName);
25
26             // write() writes as many bytes from the buffer
27             // as the length of the buffer. You can also
28             // use
29             // write(buffer, offset, length)
30             // if you want to write a specific number of
31             // bytes, or only part of the buffer.
32             outputStream.write(buffer);
33
34             // Always close files.
35             outputStream.close();
36
37             logger.info("Wrote " + buffer.length + " bytes");
38         } catch (IOException ex) {
39             logger.error("Error writing file '" + fileName + "'", ex);
40             // Or we could just do this:
41             // ex.printStackTrace();
42         }
43     }
44
45     public static byte[] ReadBinaryDump(String fileName) {
46         // The name of the file to open.
47         // String fileName = "COPSReportMessage.txt";
48         try {
49             FileInputStream inputStream = new FileInputStream(fileName);
50             // Use this for reading the data.
51             byte[] buffer = new byte[inputStream.available()];
52             // read fills buffer with data and returns
53             // the number of bytes read (which of course
54             // may be less than the buffer size, but
55             // it will never be more).
56             int total = inputStream.read(buffer);
57
58             // Always close files.
59             inputStream.close();
60
61             logger.info("Read " + total + " bytes");
62             return buffer;
63         } catch (FileNotFoundException ex) {
64             logger.error("Unable to open file '" + fileName + "'", ex);
65         } catch (IOException ex) {
66             logger.error("Error reading file '" + fileName + "'", ex);
67             // Or we could just do this:
68             // ex.printStackTrace();
69         }
70         return null;
71     }
72 }