View Javadoc

1   package baseCode.io;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.DataInputStream;
6   import java.io.DataOutputStream;
7   import java.io.IOException;
8   import java.io.UnsupportedEncodingException;
9   
10  //import org.apache.commons.lang.time.StopWatch;
11  
12  /***
13   * <p>
14   * Class to convert byte arrays (e.g., Blobs) to and from other types of arrays.
15   * <hr>
16   * TODO this could be optimized, and errors are not handled well.
17   * 
18   * @author Kiran Keshav
19   * @author Paul Pavlidis
20   * @version $Id: ByteArrayConverter.java,v 1.8 2005/04/09 09:03:22 pavlidis Exp $
21   */
22  public class ByteArrayConverter {
23      private static final int CHAR_SIZE = 2;
24  
25      private static final int DOUBLE_SIZE = 8;
26      private static final int INT_SIZE = 4;
27      private static final int LONG_SIZE = 8;
28  
29      /***
30       * Convert a byte array with one-byte-per-character ASCII encoding (aka ISO-8859-1).
31       * 
32       * @param barray
33       * @return
34       */
35      public String byteArrayToAsciiString( byte[] barray ) {
36          try {
37              return new String( barray, "ISO-8859-1" );
38          } catch ( UnsupportedEncodingException e ) {
39              e.printStackTrace();
40          }
41          return null;
42  
43      }
44  
45      /***
46       * @param barray
47       * @return char[]
48       */
49      public char[] byteArrayToChars( byte[] barray ) {
50          ByteArrayInputStream bis = new ByteArrayInputStream( barray );
51          DataInputStream dis = new DataInputStream( bis );
52          char[] carray = new char[barray.length / CHAR_SIZE];
53  
54          int i = 0;
55          try {
56              while ( true ) {
57                  carray[i] = dis.readChar();
58                  i++;
59              }
60          } catch ( IOException e ) {
61              // do nothing. e.printStackTrace();
62          }
63  
64          try {
65              dis.close();
66              bis.close();
67          } catch ( IOException e ) {
68              e.printStackTrace();
69          }
70  
71          return carray;
72      }
73  
74      /***
75       * @param barray
76       * @return double[]
77       */
78      public double[] byteArrayToDoubles( byte[] barray ) {
79          ByteArrayInputStream bis = new ByteArrayInputStream( barray );
80          DataInputStream dis = new DataInputStream( bis );
81  
82          double[] darray = new double[barray.length / DOUBLE_SIZE];
83          int i = 0;
84          try {
85              while ( true ) {
86                  darray[i] = dis.readDouble();
87                  i++;
88              }
89          } catch ( IOException e ) {
90              // do nothing.
91          }
92  
93          try {
94              bis.close();
95          } catch ( IOException e1 ) {
96              e1.printStackTrace();
97          }
98          return darray;
99      }
100 
101     /***
102      * @param barray
103      * @return int[]
104      */
105     public int[] byteArrayToInts( byte[] barray ) {
106         ByteArrayInputStream bis = new ByteArrayInputStream( barray );
107         DataInputStream dis = new DataInputStream( bis );
108         int[] iarray = new int[barray.length / INT_SIZE];
109         int i = 0;
110 
111         try {
112             while ( true ) {
113                 iarray[i] = dis.readInt();
114                 i++;
115             }
116         } catch ( IOException e ) {
117             // do nothing.
118         }
119 
120         try {
121             dis.close();
122             bis.close();
123         } catch ( IOException e1 ) {
124             e1.printStackTrace();
125         }
126 
127         return iarray;
128     }
129 
130     /***
131      * @param barray
132      * @return long[] resulting from parse of the bytes.
133      */
134     public long[] byteArrayToLongs( byte[] barray ) {
135         ByteArrayInputStream bis = new ByteArrayInputStream( barray );
136         DataInputStream dis = new DataInputStream( bis );
137         long[] iarray = new long[barray.length / LONG_SIZE];
138         int i = 0;
139 
140         try {
141             while ( true ) {
142                 iarray[i] = dis.readLong();
143                 i++;
144             }
145         } catch ( IOException e ) {
146             // do nothing.
147         }
148 
149         try {
150             dis.close();
151             bis.close();
152         } catch ( IOException e1 ) {
153             e1.printStackTrace();
154         }
155 
156         return iarray;
157     }
158 
159     /***
160      * @param carray
161      * @return byte[]
162      */
163     public byte[] charArrayToBytes( char[] carray ) {
164         ByteArrayOutputStream bos = new ByteArrayOutputStream();
165         DataOutputStream dos = new DataOutputStream( bos );
166 
167         try {
168 
169             for ( int i = 0; i < carray.length; i++ ) {
170                 dos.writeChar( carray[i] );
171             }
172 
173             dos.close();
174             bos.close();
175 
176         } catch ( IOException e ) {
177             // do nothing.
178         }
179 
180         return bos.toByteArray();
181     }
182 
183     /***
184      * @param darray
185      * @return byte[]
186      */
187     public byte[] doubleArrayToBytes( double[] darray ) {
188         ByteArrayOutputStream bos = new ByteArrayOutputStream();
189         DataOutputStream dos = new DataOutputStream( bos );
190         try {
191 
192             for ( int i = 0; i < darray.length; i++ ) {
193                 dos.writeDouble( darray[i] );
194             }
195 
196         } catch ( IOException e ) {
197             e.printStackTrace();
198         }
199         return bos.toByteArray();
200     }
201 
202     /***
203      * @param iarray
204      * @return byte[]
205      */
206     public byte[] intArrayToBytes( int[] iarray ) {
207         ByteArrayOutputStream bos = new ByteArrayOutputStream();
208         DataOutputStream dos = new DataOutputStream( bos );
209         try {
210             for ( int i = 0; i < iarray.length; i++ ) {
211                 dos.writeInt( iarray[i] );
212             }
213             dos.close();
214             bos.close();
215         } catch ( IOException e ) {
216             // do nothing
217         }
218         return bos.toByteArray();
219     }
220 
221 }