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 16 byte integer array data object
17   * 
18   * @author kuss
19   */
20  public class INT16 implements DataTypeIf {
21  
22      private static final Logger logger = Logger.getLogger(INT16.class);
23  
24      private short[] data;
25  
26      private long size;
27  
28      INT16(short[] data) {
29          this.data = data;
30          size = data.length * 2;
31      }
32  
33      /***
34       * Get the rpm int16 array as a java short array
35       * 
36       * @return An array of shorts
37       */
38      public short[] 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.INT16;
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 INT16 readFromStream(DataInputStream inputStream,
68              IndexEntry indexEntry) throws IOException {
69          if (indexEntry.getType() != RPMIndexType.INT16) { throw new IllegalArgumentException(
70                  "Type <" + indexEntry.getType() + "> does not match <"
71                          + RPMIndexType.INT16 + ">"); }
72  
73          short[] data = new short[(int) indexEntry.getCount()];
74  
75          for (int pos = 0; pos < indexEntry.getCount(); pos++) {
76              data[pos] = inputStream.readShort();
77          }
78  
79          INT16 int16Object = new INT16(data);
80  
81          if (logger.isDebugEnabled()) {
82              logger.debug(int16Object.toString());
83          }
84  
85          //int16Object.size = indexEntry.getType().getSize()
86          //        * indexEntry.getCount();
87  
88          return int16Object;
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 Short(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] & 0x0FFFF);
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 }