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 I18N string data object
17   * 
18   * @author kuss
19   */
20  public class I18NSTRING implements DataTypeIf {
21  
22      private static final Logger logger = Logger.getLogger(I18NSTRING.class);
23  
24      private String[] data;
25  
26      private int localeIndex;
27  
28      private long size;
29  
30      I18NSTRING(String[] data) {
31          this.data = data;
32          // add 1 to each string for \0 in C strings
33          for (int pos = 0; pos < data.length; pos++)
34              size += data[pos].length() + 1;
35      }
36  
37      /***
38       * Get the rpm I18N string array as a java string array
39       * 
40       * @return An array of I18N strings (as defined in the tag HEADERI18NTABLE)
41       */
42      public String[] getData() {
43          return data;
44      }
45  
46      /*
47       * @see com.jguild.jrpm.io.datatype.DataTypeIf#getData()
48       */
49      public Object getDataObject() {
50          return data;
51      }
52  
53      /***
54       * Set the locale that should be returned by toString()
55       * 
56       * @param index
57       *           The index of the I18N string array. This should match the tag
58       *           HEADERI18NTABLE.
59       */
60      public void setLocaleIndex(int index) {
61          if (index < 0) { throw new IndexOutOfBoundsException(
62                  "Index less than 0"); }
63  
64          if (index > data.length) { throw new IndexOutOfBoundsException(
65                  "Index grater than " + (data.length - 1)); }
66  
67          localeIndex = index;
68      }
69  
70      /*
71       * @see com.jguild.jrpm.io.datatype.DataTypeIf#getType()
72       */
73      public RPMIndexType getType() {
74          return RPMIndexType.I18NSTRING;
75      }
76  
77      /***
78       * Constructs a type froma stream
79       * 
80       * @param inputStream
81       *           An input stream
82       * @param indexEntry
83       *           The index informations
84       * @param length
85       *           the length of the data
86       * @return The size of the read data
87       * @throws IOException
88       *            if an I/O error occurs.
89       */
90      public static I18NSTRING readFromStream(DataInputStream inputStream,
91              IndexEntry indexEntry, long length) throws IOException {
92          if (indexEntry.getType() != RPMIndexType.I18NSTRING) { throw new IllegalArgumentException(
93                  "Type <" + indexEntry.getType() + "> does not match <"
94                          + RPMIndexType.I18NSTRING + ">"); }
95  
96          // initialize temporary space for data
97          byte[] stringData = new byte[(int) length];
98  
99          // and read it from stream
100         inputStream.readFully(stringData);
101 
102         String[] data = new String[(int) indexEntry.getCount()];
103 
104         int off = 0;
105 
106         for (int pos = 0; pos < indexEntry.getCount(); pos++) {
107             data[pos] = RPMUtil.cArrayToString(stringData, off);
108 
109             // offset for new string is stringlength + 1 for the \0 in c
110             // strings
111             if (data[pos].length() == 0) {
112                 off += data[pos].length();
113             } else {
114                 off += (data[pos].length() + 1);
115             }
116 
117             if (off > stringData.length) { throw new IllegalStateException(
118                     "Index wrong; Strings doesn't fit into data area. [" + off
119                             + ", " + stringData.length + "]"); }
120         }
121 
122         I18NSTRING stringObject = new I18NSTRING(data);
123 
124         if (logger.isDebugEnabled()) {
125             logger.debug(stringObject.toString());
126             if (stringObject.size != stringData.length)
127                     logger.warn("STRING size differs (is:" + stringData.length
128                             + ";should:" + stringObject.size + ")");
129         }
130 
131         stringObject.size = stringData.length;
132 
133         return stringObject;
134     }
135 
136     /*
137      * @see com.jguild.jrpm.io.datatype.DataTypeIf#isArray()
138      */
139     public boolean isArray() {
140         return true;
141     }
142 
143     /*
144      * @see com.jguild.jrpm.io.datatype.DataTypeIf#getElementCount()
145      */
146     public long getElementCount() {
147         return data.length;
148     }
149 
150     /*
151      * @see com.jguild.jrpm.io.datatype.DataTypeIf#getSize()
152      */
153     public long getSize() {
154         return size;
155     }
156 
157     /*
158      * @see com.jguild.jrpm.io.datatype.DataTypeIf#get(int)
159      */
160     public Object get(int i) {
161         return data[i];
162     }
163 
164     /*
165      * @see java.lang.Object#toString()
166      */
167     public String toString() {
168         return data[localeIndex];
169     }
170 }