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 string data object
17   * 
18   * @author kuss
19   */
20  public class STRING implements DataTypeIf {
21  
22      private static final Logger logger = Logger.getLogger(STRING.class);
23  
24      private String data;
25  
26      private int size;
27  
28      STRING(String data) {
29          this.data = data;
30          // add one to size for the \0 in C strings.
31          size = data.length() + 1;
32      }
33  
34      /***
35       * Get the rpm string as a java string
36       * 
37       * @return A string
38       */
39      public String getData() {
40          return data;
41      }
42  
43      /*
44       * @see com.jguild.jrpm.io.datatype.DataTypeIf#getData()
45       */
46      public Object getDataObject() {
47          return data;
48      }
49  
50      /*
51       * @see com.jguild.jrpm.io.datatype.DataTypeIf#getType()
52       */
53      public RPMIndexType getType() {
54          return RPMIndexType.STRING;
55      }
56  
57      /***
58       * Constructs a type froma stream
59       * 
60       * @param inputStream
61       *           An input stream
62       * @param indexEntry
63       *           The index informations
64       * @param length
65       *           the length of the data
66       * @return The size of the read data
67       * @throws IOException
68       *            if an I/O error occurs.
69       */
70      public static STRING readFromStream(DataInputStream inputStream,
71              IndexEntry indexEntry, long length) throws IOException {
72          if (indexEntry.getType() != RPMIndexType.STRING) { throw new IllegalArgumentException(
73                  "Type <" + indexEntry.getType() + "> does not match <"
74                          + RPMIndexType.STRING + ">"); }
75  
76          if (indexEntry.getCount() != 1) { throw new IllegalArgumentException(
77                  "There can be only one string per tag of type STRING"); }
78  
79          // initialize temporary space for data
80          byte[] stringData = new byte[(int) length];
81  
82          // and read it from stream
83          inputStream.readFully(stringData);
84  
85          String str = RPMUtil.cArrayToString(stringData, 0);
86          STRING stringObject = new STRING(str);
87  
88          if (logger.isDebugEnabled()) {
89              logger.debug(stringObject.toString());
90              if (stringObject.size != stringData.length)
91                      logger.warn("STRING size differs (is:" + stringData.length
92                              + ";should:" + stringObject.size + ")");
93          }
94  
95          stringObject.size = stringData.length;
96  
97          return stringObject;
98      }
99  
100     /*
101      * @see com.jguild.jrpm.io.datatype.DataTypeIf#isArray()
102      */
103     public boolean isArray() {
104         return false;
105     }
106 
107     /*
108      * @see com.jguild.jrpm.io.datatype.DataTypeIf#getElementCount()
109      */
110     public long getElementCount() {
111         return 1;
112     }
113 
114     /*
115      * @see com.jguild.jrpm.io.datatype.DataTypeIf#getSize()
116      */
117     public long getSize() {
118         return size;
119     }
120 
121     /*
122      * @see com.jguild.jrpm.io.datatype.DataTypeIf#get(int)
123      */
124     public Object get(int i) {
125         if (i != 0) { throw new IndexOutOfBoundsException(); }
126 
127         return data;
128     }
129 
130     /*
131      * @see java.lang.Object#toString()
132      */
133     public String toString() {
134         return data;
135     }
136 }