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