View Javadoc

1   /*
2    * Copyright 2009, openv4j.sf.net, and individual contributors as indicated
3    * by the @authors tag. See the copyright.txt in the distribution for a
4    * full listing of individual contributors.
5    *
6    * This is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU General Public License as
8    * published by the Free Software Foundation; either version 3 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   *
21   * $Id: $
22   *
23   * @author arnep
24   */
25  package net.sf.openv4j.protocolhandlers;
26  
27  import java.util.Arrays;
28  
29  /**
30   * DOCUMENT ME!
31   *
32   * @author aploese
33   */
34  public class DataBlock {
35      private byte[] data;
36      private final int baseAddress;
37  
38      /**
39       * Creates a new DataBlock object.
40       *
41       * @param baseAddress DOCUMENT ME!
42       * @param size DOCUMENT ME!
43       */
44      public DataBlock(int baseAddress, int size) {
45          super();
46          this.baseAddress = baseAddress;
47          data = new byte[size];
48          Arrays.fill(data, (byte) 0xff);
49      }
50  
51      /**
52       * Creates a new DataBlock object.
53       *
54       * @param baseAddress DOCUMENT ME!
55       */
56      DataBlock(int baseAddress, int... bytes) {
57          super();
58          this.baseAddress = baseAddress;
59          data = new byte[bytes.length];
60  
61          for (int i = 0; i < bytes.length; i++) {
62              data[i] = (byte) bytes[i];
63          }
64      }
65  
66      public int getBaseAddress() {
67          return baseAddress;
68      }
69  
70      public byte getByteAtPos(int pos) {
71          return data[pos];
72      }
73  
74      public byte[] getBytes() {
75          return data;
76      }
77  
78      public int getLength() {
79          return data.length;
80      }
81  
82      public void setByteAtPos(int pos, int theData) {
83          data[pos] = (byte) theData;
84      }
85  
86      public void setBytesAtPos(int pos, byte[] theData) {
87          if (theData.length == data.length) {
88              data = theData;
89          } else {
90              for (int i = 0; i < theData.length; i++) {
91                  data[i + pos] = theData[i];
92              }
93          }
94      }
95  
96      public void writeTo(StringBuilder sb, boolean suppressFirstNewLine) {
97          for (int i = 0; i < data.length; i++) {
98              if (((i + baseAddress) % 16) == 0) {
99                  if (suppressFirstNewLine) {
100                     sb.append(String.format("%04x ", i + baseAddress));
101                     suppressFirstNewLine = false;
102                 } else {
103                     sb.append(String.format("\n%04x ", i + baseAddress));
104                 }
105             } else if (((i + baseAddress) % 4) == 0) {
106                 sb.append(" |");
107             }
108 
109             sb.append(String.format(" %02x", data[i]));
110         }
111     }
112 }