4 package org.pcmm.utils;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
14 public class PCMMUtils {
16 public final static Logger logger = LoggerFactory.getLogger(PCMMUtils.class);
18 public static void WriteBinaryDump(String rootFileName, byte[] buffer) {
20 String fileName = "/tmp/" + rootFileName + "-" + java.util.UUID.randomUUID() + ".bin";
23 logger.info("Open fileName " + fileName);
24 FileOutputStream outputStream = new FileOutputStream(fileName);
26 // write() writes as many bytes from the buffer
27 // as the length of the buffer. You can also
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);
34 // Always close files.
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();
45 public static byte[] ReadBinaryDump(String fileName) {
46 // The name of the file to open.
47 // String fileName = "COPSReportMessage.txt";
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);
58 // Always close files.
61 logger.info("Read " + total + " bytes");
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();