2 * Copyright (c) 2014, 2015 Cable Television Laboratories, Inc. and others. All rights reserved.
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
9 package org.pcmm.utils;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
19 public class PCMMUtils {
21 public final static Logger logger = LoggerFactory.getLogger(PCMMUtils.class);
23 public static void WriteBinaryDump(String rootFileName, byte[] buffer) {
25 String fileName = "/tmp/" + rootFileName + "-" + java.util.UUID.randomUUID() + ".bin";
28 logger.info("Open fileName " + fileName);
29 FileOutputStream outputStream = new FileOutputStream(fileName);
31 // write() writes as many bytes from the buffer
32 // as the length of the buffer. You can also
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);
39 // Always close files.
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();
50 public static byte[] ReadBinaryDump(String fileName) {
51 // The name of the file to open.
52 // String fileName = "COPSReportMessage.txt";
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);
63 // Always close files.
66 logger.info("Read " + total + " bytes");
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();