Source code: com/jguild/jrpm/io/datatype/INT16.java
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 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 /**
17 * A representation of a rpm 16 byte integer array data object
18 *
19 * @author kuss
20 */
21 public class INT16 implements DataTypeIf {
22 private static final Logger logger = Logger.getLogger(INT16.class);
23 private short[] data;
24 private long size;
25
26 /**
27 * Get the rpm int16 array as a java short array
28 *
29 * @return An array of shorts
30 */
31 public short[] getData() {
32 return data;
33 }
34
35 /*
36 * @see com.jguild.jrpm.io.datatype.DataTypeIf#getData()
37 */
38 public Object getDataObject() {
39 return data;
40 }
41
42 /*
43 * @see com.jguild.jrpm.io.datatype.DataTypeIf#getType()
44 */
45 public RPMIndexType getType() {
46 return RPMIndexType.INT16;
47 }
48
49 /**
50 * Constructs a type froma stream
51 *
52 * @param inputStream An input stream
53 * @param indexEntry The index informations
54 * @return The size of the read data
55 */
56 public static INT16 readFromStream(DataInputStream inputStream, IndexEntry indexEntry)
57 throws IOException {
58 if (indexEntry.getType() != RPMIndexType.INT16) {
59 throw new IllegalArgumentException("Type <" + indexEntry.getType() + "> does not match <" + RPMIndexType.INT16 + ">");
60 }
61
62 INT16 int16Object = new INT16();
63 int16Object.data = new short[(int) indexEntry.getCount()];
64
65 for (int pos = 0; pos < indexEntry.getCount(); pos++) {
66 int16Object.data[pos] = inputStream.readShort();
67 }
68
69 if (logger.isDebugEnabled()) {
70 logger.debug(int16Object.toString());
71 }
72
73 int16Object.size = indexEntry.getType().getSize() * indexEntry.getCount();
74
75 return int16Object;
76 }
77
78 /*
79 * @see com.jguild.jrpm.io.datatype.DataTypeIf#isArray()
80 */
81 public boolean isArray() {
82 return true;
83 }
84
85 /*
86 * @see com.jguild.jrpm.io.datatype.DataTypeIf#getElementCount()
87 */
88 public long getElementCount() {
89 return data.length;
90 }
91
92 /*
93 * @see com.jguild.jrpm.io.datatype.DataTypeIf#getSize()
94 */
95 public long getSize() {
96 return size;
97 }
98
99 /*
100 * @see com.jguild.jrpm.io.datatype.DataTypeIf#get(int)
101 */
102 public Object get(int i) {
103 return new Short(data[i]);
104 }
105
106 /*
107 * @see java.lang.Object#toString()
108 */
109 public String toString() {
110 StringBuffer buf = new StringBuffer();
111
112 if (data.length > 1) {
113 buf.append("[");
114 }
115
116 for (int pos = 0; pos < data.length; pos++) {
117 buf.append(data[pos] & 0x0FFFF);
118
119 if (pos < (data.length - 1)) {
120 buf.append(", ");
121 }
122 }
123
124 if (data.length > 1) {
125 buf.append("]");
126 }
127
128 return buf.toString();
129 }
130 }