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