1 21 22 package org.apache.derby.iapi.services.io; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import java.io.ObjectOutput ; 26 import java.io.ObjectInput ; 27 import java.io.IOException ; 28 import java.lang.reflect.Array ; 29 30 37 public abstract class ArrayUtil 38 { 39 55 public static void writeArrayLength(ObjectOutput out, Object [] a) 56 throws IOException 57 { 58 out.writeInt(a.length); 59 } 60 61 69 public static void writeArrayItems(ObjectOutput out, Object [] a) 70 throws IOException 71 { 72 if (a == null) 73 return; 74 75 for(int ix = 0; ix < a.length; ix++) 76 { out.writeObject(a[ix]); } 77 } 78 79 88 public static void writeArray(ObjectOutput out, Object [] a) 89 throws IOException 90 { 91 if (a == null) 92 { 93 out.writeInt(0); 94 return; 95 } 96 97 out.writeInt(a.length); 98 for(int ix = 0; ix < a.length; ix++) 99 { out.writeObject(a[ix]); } 100 } 101 102 111 public static void readArrayItems(ObjectInput in, Object [] a) 112 throws IOException , ClassNotFoundException 113 { 114 for (int ix=0; ix<a.length; ix++) 115 { 116 a[ix]=in.readObject(); 117 } 118 } 119 120 129 public static int readArrayLength(ObjectInput in) 130 throws IOException 131 { 132 return in.readInt(); 133 } 134 135 143 public static Object [] readObjectArray(ObjectInput in) 144 throws IOException , ClassNotFoundException 145 { 146 int size = in.readInt(); 147 if ( size == 0 ) { return null; } 148 149 Object [] result = new Object [ size ]; 150 151 readArrayItems( in, result ); 152 153 return result; 154 } 155 156 162 170 public static void writeIntArray(ObjectOutput out, int[] a) throws IOException { 171 if (a == null) 172 out.writeInt(0); 173 else { 174 out.writeInt(a.length); 175 for (int i=0; i<a.length; i++) 176 out.writeInt(a[i]); 177 } 178 } 179 180 189 public static int[] readIntArray(ObjectInput in) throws IOException { 190 int length = in.readInt(); 191 if (length == 0) 192 return null; 193 int[] a = new int[length]; 194 for (int i=0; i<length; i++) 195 a[i] = in.readInt(); 196 return a; 197 } 198 199 public static void writeInts( ObjectOutput out, int[][] val ) 200 throws IOException 201 { 202 if (val == null) 203 { 204 out.writeBoolean(false); 205 } 206 else 207 { 208 out.writeBoolean(true); 209 210 int count = val.length; 211 out.writeInt( count ); 212 213 for (int i = 0; i < count; i++) 214 { 215 ArrayUtil.writeIntArray( out, val[i] ); 216 } 217 } 218 } 219 220 public static int[][] readInts( ObjectInput in ) 221 throws IOException , ClassNotFoundException 222 { 223 int[][] retVal = null; 224 225 if ( in.readBoolean() ) 226 { 227 int count = in.readInt(); 228 229 retVal = new int[ count ][]; 230 231 for (int i = 0; i < count; i++) 232 { 233 retVal[ i ] = ArrayUtil.readIntArray( in ); 234 } 235 } 236 237 return retVal; 238 } 239 240 246 254 public static void writeLongArray(ObjectOutput out, long[] a) throws IOException { 255 if (a == null) 256 out.writeInt(0); 257 else { 258 out.writeInt(a.length); 259 for (int i=0; i<a.length; i++) 260 out.writeLong(a[i]); 261 } 262 } 263 264 273 public static long[] readLongArray(ObjectInput in) throws IOException { 274 int length = in.readInt(); 275 long[] a = new long[length]; 276 for (int i=0; i<length; i++) 277 a[i] = in.readLong(); 278 return a; 279 } 280 281 290 public static String [] readStringArray(ObjectInput in) 291 throws IOException , ClassNotFoundException 292 { 293 Object [] objArray = readObjectArray(in); 294 int size = 0; 295 296 if (objArray == null) 297 return null; 298 299 String [] stringArray = new String [size = objArray.length]; 300 301 for (int i = 0; i < size; i++) 302 { 303 stringArray[i] = (String )objArray[i]; 304 } 305 306 return stringArray; 307 } 308 309 315 323 public static void writeBooleanArray(ObjectOutput out, boolean[] a) throws IOException { 324 if (a == null) 325 out.writeInt(0); 326 else { 327 out.writeInt(a.length); 328 for (int i=0; i<a.length; i++) 329 out.writeBoolean(a[i]); 330 } 331 } 332 333 342 public static boolean[] readBooleanArray(ObjectInput in) throws IOException { 343 int length = in.readInt(); 344 boolean[] a = new boolean[length]; 345 for (int i=0; i<length; i++) 346 a[i] = in.readBoolean(); 347 return a; 348 } 349 } 350 | Popular Tags |