View Javadoc

1   /*
2    * jGuild Project: jRPM
3    * Released under the Apache License ( http://www.apache.org/LICENSE )
4    */
5   package com.jguild.jrpm.io.datatype;
6   
7   import java.io.DataInputStream;
8   import java.io.IOException;
9   
10  import org.apache.log4j.Logger;
11  
12  import com.jguild.jrpm.io.IndexEntry;
13  import com.jguild.jrpm.io.constant.RPMIndexType;
14  
15  /***
16   * A representation of a rpm char array data object
17   * 
18   * @author kuss
19   */
20  public class CHAR implements DataTypeIf {
21  
22      private static final Logger logger = Logger.getLogger(CHAR.class);
23  
24      private char[] data;
25  
26      private long size;
27  
28      CHAR(char[] data) {
29          this.data = data;
30          size = data.length;
31      }
32  
33      /***
34       * Get the rpm char array as a java char array
35       * 
36       * @return An array of chars
37       */
38      public char[] getData() {
39          return data;
40      }
41  
42      /*
43       * @see com.jguild.jrpm.io.datatype.DataTypeIf#getData()
44       */
45      public Object getDataObject() {
46          return data;
47      }
48  
49      /*
50       * @see com.jguild.jrpm.io.datatype.DataTypeIf#getType()
51       */
52      public RPMIndexType getType() {
53          return RPMIndexType.CHAR;
54      }
55  
56      /***
57       * Constructs a type froma stream
58       * 
59       * @param inputStream
60       *           An input stream
61       * @param indexEntry
62       *           The index informations
63       * @return The size of the read data
64       * @throws IOException
65       *            if an I/O error occurs.
66       */
67      public static CHAR readFromStream(DataInputStream inputStream,
68              IndexEntry indexEntry) throws IOException {
69          if (indexEntry.getType() != RPMIndexType.CHAR) { throw new IllegalArgumentException(
70                  "Type <" + indexEntry.getType() + "> does not match <"
71                          + RPMIndexType.CHAR + ">"); }
72  
73          char[] data = new char[(int) indexEntry.getCount()];
74  
75          for (int pos = 0; pos < indexEntry.getCount(); pos++) {
76              data[pos] = (char) inputStream.readByte();
77          }
78  
79          CHAR charObject = new CHAR(data);
80  
81          if (logger.isDebugEnabled()) {
82              logger.debug(charObject.toString());
83          }
84  
85          //charObject.size = indexEntry.getType().getSize()
86          //        * indexEntry.getCount();
87  
88          return charObject;
89      }
90  
91      /*
92       * @see com.jguild.jrpm.io.datatype.DataTypeIf#isArray()
93       */
94      public boolean isArray() {
95          return true;
96      }
97  
98      /*
99       * @see com.jguild.jrpm.io.datatype.DataTypeIf#getElementCount()
100      */
101     public long getElementCount() {
102         return data.length;
103     }
104 
105     /*
106      * @see com.jguild.jrpm.io.datatype.DataTypeIf#getSize()
107      */
108     public long getSize() {
109         return size;
110     }
111 
112     /*
113      * @see com.jguild.jrpm.io.datatype.DataTypeIf#get(int)
114      */
115     public Object get(int i) {
116         return new Character(data[i]);
117     }
118 
119     /*
120      * @see java.lang.Object#toString()
121      */
122     public String toString() {
123         StringBuffer buf = new StringBuffer();
124 
125         if (data.length > 1) {
126             buf.append("[");
127         }
128 
129         for (int pos = 0; pos < data.length; pos++) {
130             buf.append(data[pos]);
131 
132             if (pos < (data.length - 1)) {
133                 buf.append(", ");
134             }
135         }
136 
137         if (data.length > 1) {
138             buf.append("]");
139         }
140 
141         return buf.toString();
142     }
143 }