Clover coverage report - baseCode - 0.2.5 Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT FRAMES NO FRAMES |
file stats: | LOC: | 221 |  | Methods: | 8
| NCLOC: | 137 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
ByteArrayConverter.java | 33.3% | 21.8% | 25% | 22.8% |

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 | 0 |
         public String byteArrayToAsciiString( byte[] barray ) {
36 | 0 |
             try {
37 | 0 |
                 return new String( barray, "ISO-8859-1" );
38 |  |
             } catch ( UnsupportedEncodingException e ) {
39 | 0 |
                 e.printStackTrace();
40 |  |
             }
41 | 0 |
             return null;
42 |  |
43 |  |
         }
44 |  |
45 |  |
         /**
46 |  |
          * @param barray
47 |  |
          * @return char[]
48 |  |
          */
49 | 0 |
         public char[] byteArrayToChars( byte[] barray ) {
50 | 0 |
             ByteArrayInputStream bis = new ByteArrayInputStream( barray );
51 | 0 |
             DataInputStream dis = new DataInputStream( bis );
52 | 0 |
             char[] carray = new char[barray.length / CHAR_SIZE];
53 |  |
54 | 0 |
             int i = 0;
55 | 0 |
             try {
56 | 0 |
                 while ( true ) {
57 | 0 |
                     carray[i] = dis.readChar();
58 | 0 |
                     i++;
59 |  |
                 }
60 |  |
             } catch ( IOException e ) {
61 |  |
                 // do nothing. e.printStackTrace();
62 |  |
             }
63 |  |
64 | 0 |
             try {
65 | 0 |
                 dis.close();
66 | 0 |
                 bis.close();
67 |  |
             } catch ( IOException e ) {
68 | 0 |
                 e.printStackTrace();
69 |  |
             }
70 |  |
71 | 0 |
             return carray;
72 |  |
         }
73 |  |
74 |  |
         /**
75 |  |
          * @param barray
76 |  |
          * @return double[]
77 |  |
          */
78 | 2 |
         public double[] byteArrayToDoubles( byte[] barray ) {
79 | 2 |
             ByteArrayInputStream bis = new ByteArrayInputStream( barray );
80 | 2 |
             DataInputStream dis = new DataInputStream( bis );
81 |  |
82 | 2 |
             double[] darray = new double[barray.length / DOUBLE_SIZE];
83 | 2 |
             int i = 0;
84 | 2 |
             try {
85 | 2 |
                 while ( true ) {
86 | 952513 |
                     darray[i] = dis.readDouble();
87 | 952511 |
                     i++;
88 |  |
                 }
89 |  |
             } catch ( IOException e ) {
90 |  |
                 // do nothing.
91 |  |
             }
92 |  |
93 | 2 |
             try {
94 | 2 |
                 bis.close();
95 |  |
             } catch ( IOException e1 ) {
96 | 0 |
                 e1.printStackTrace();
97 |  |
             }
98 | 2 |
             return darray;
99 |  |
         }
100 |  |
101 |  |
         /**
102 |  |
          * @param barray
103 |  |
          * @return int[]
104 |  |
          */
105 | 0 |
         public int[] byteArrayToInts( byte[] barray ) {
106 | 0 |
             ByteArrayInputStream bis = new ByteArrayInputStream( barray );
107 | 0 |
             DataInputStream dis = new DataInputStream( bis );
108 | 0 |
             int[] iarray = new int[barray.length / INT_SIZE];
109 | 0 |
             int i = 0;
110 |  |
111 | 0 |
             try {
112 | 0 |
                 while ( true ) {
113 | 0 |
                     iarray[i] = dis.readInt();
114 | 0 |
                     i++;
115 |  |
                 }
116 |  |
             } catch ( IOException e ) {
117 |  |
                 // do nothing.
118 |  |
             }
119 |  |
120 | 0 |
             try {
121 | 0 |
                 dis.close();
122 | 0 |
                 bis.close();
123 |  |
             } catch ( IOException e1 ) {
124 | 0 |
                 e1.printStackTrace();
125 |  |
             }
126 |  |
127 | 0 |
             return iarray;
128 |  |
         }
129 |  |
130 |  |
         /**
131 |  |
          * @param barray
132 |  |
          * @return long[] resulting from parse of the bytes.
133 |  |
          */
134 | 0 |
         public long[] byteArrayToLongs( byte[] barray ) {
135 | 0 |
             ByteArrayInputStream bis = new ByteArrayInputStream( barray );
136 | 0 |
             DataInputStream dis = new DataInputStream( bis );
137 | 0 |
             long[] iarray = new long[barray.length / LONG_SIZE];
138 | 0 |
             int i = 0;
139 |  |
140 | 0 |
             try {
141 | 0 |
                 while ( true ) {
142 | 0 |
                     iarray[i] = dis.readLong();
143 | 0 |
                     i++;
144 |  |
                 }
145 |  |
             } catch ( IOException e ) {
146 |  |
                 // do nothing.
147 |  |
             }
148 |  |
149 | 0 |
             try {
150 | 0 |
                 dis.close();
151 | 0 |
                 bis.close();
152 |  |
             } catch ( IOException e1 ) {
153 | 0 |
                 e1.printStackTrace();
154 |  |
             }
155 |  |
156 | 0 |
             return iarray;
157 |  |
         }
158 |  |
159 |  |
         /**
160 |  |
          * @param carray
161 |  |
          * @return byte[]
162 |  |
          */
163 | 0 |
         public byte[] charArrayToBytes( char[] carray ) {
164 | 0 |
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
165 | 0 |
             DataOutputStream dos = new DataOutputStream( bos );
166 |  |
167 | 0 |
             try {
168 |  |
169 | 0 |
                 for ( int i = 0; i < carray.length; i++ ) {
170 | 0 |
                     dos.writeChar( carray[i] );
171 |  |
                 }
172 |  |
173 | 0 |
                 dos.close();
174 | 0 |
                 bos.close();
175 |  |
176 |  |
             } catch ( IOException e ) {
177 |  |
                 // do nothing.
178 |  |
             }
179 |  |
180 | 0 |
             return bos.toByteArray();
181 |  |
         }
182 |  |
183 |  |
         /**
184 |  |
          * @param darray
185 |  |
          * @return byte[]
186 |  |
          */
187 | 4 |
         public byte[] doubleArrayToBytes( double[] darray ) {
188 | 4 |
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
189 | 4 |
             DataOutputStream dos = new DataOutputStream( bos );
190 | 4 |
             try {
191 |  |
192 | 4 |
                 for ( int i = 0; i < darray.length; i++ ) {
193 | 1905022 |
                     dos.writeDouble( darray[i] );
194 |  |
                 }
195 |  |
196 |  |
             } catch ( IOException e ) {
197 | 0 |
                 e.printStackTrace();
198 |  |
             }
199 | 4 |
             return bos.toByteArray();
200 |  |
         }
201 |  |
202 |  |
         /**
203 |  |
          * @param iarray
204 |  |
          * @return byte[]
205 |  |
          */
206 | 0 |
         public byte[] intArrayToBytes( int[] iarray ) {
207 | 0 |
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
208 | 0 |
             DataOutputStream dos = new DataOutputStream( bos );
209 | 0 |
             try {
210 | 0 |
                 for ( int i = 0; i < iarray.length; i++ ) {
211 | 0 |
                     dos.writeInt( iarray[i] );
212 |  |
                 }
213 | 0 |
                 dos.close();
214 | 0 |
                 bos.close();
215 |  |
             } catch ( IOException e ) {
216 |  |
                 // do nothing
217 |  |
             }
218 | 0 |
             return bos.toByteArray();
219 |  |
         }
220 |  |
221 |  |
     }