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 binary array data object
17   * 
18   * @author kuss
19   */
20  public class BIN implements DataTypeIf {
21  
22      private static final Logger logger = Logger.getLogger(BIN.class);
23  
24      private byte[] data;
25  
26      private long size;
27  
28      BIN(byte[] data) {
29          this.data = data;
30          size = data.length;
31      }
32  
33      /***
34       * Get the rpm byte array as a java byte array
35       * 
36       * @return An array of bytes
37       */
38      public byte[] 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.BIN;
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 BIN readFromStream(DataInputStream inputStream,
68              IndexEntry indexEntry) throws IOException {
69          if (indexEntry.getType() != RPMIndexType.BIN) { throw new IllegalArgumentException(
70                  "Type <" + indexEntry.getType() + "> does not match <"
71                          + RPMIndexType.BIN + ">"); }
72  
73          byte[] data = new byte[(int) indexEntry.getCount()];
74  
75          for (int pos = 0; pos < indexEntry.getCount(); pos++) {
76              data[pos] = inputStream.readByte();
77          }
78  
79          BIN binObject = new BIN(data);
80  
81          if (logger.isDebugEnabled()) {
82              logger.debug(binObject.toString());
83          }
84  
85          //binObject.size = indexEntry.getType().getSize() *
86          // indexEntry.getCount();
87  
88          return binObject;
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 Byte(data[i]);
117     }
118 
119     /*
120      * @see java.lang.Object#toString()
121      */
122     public String toString() {
123         return RPMUtil.byteArrayToHexString(data);
124     }
125 }