1 32 package com.imagero.uio; 33 34 import java.io.ByteArrayOutputStream ; 35 import java.io.DataOutputStream ; 36 import java.io.IOException ; 37 38 45 public abstract class AbstractRandomAccess extends AbstractRandomAccessRO implements RandomAccess { 46 47 private DataOutputBE dobe = new DataOutputBE(); 48 private DataOutputLE dole = new DataOutputLE(); 49 50 EDataOutput dataOutput; 51 52 public void writeBoolean(boolean b) throws IOException { 53 write(b ? 1 : 0); 54 } 55 56 public void writeByte(int b) throws IOException { 57 write(b); 58 } 59 60 public void write(short[] sh) throws IOException { 61 write(sh, getByteOrder()); 62 } 63 64 public void write(short[] sh, int byteOrder) throws IOException { 65 write(sh, 0, sh.length, byteOrder); 66 } 67 68 public void write(short[] sh, int offset, int length) throws IOException { 69 write(sh, offset, length, getByteOrder()); 70 } 71 72 public void write(short[] sh, int offset, int length, int byteOrder) throws IOException { 73 RandomAccessRO ro = RandomAccessFactory.createBufferedRO(sh, offset, length); 74 ro.setByteOrder(byteOrder); 75 byte[] b = new byte[length << 1]; 76 ro.readFully(b); 77 write(b); 78 } 79 80 public void write(char[] sh) throws IOException { 81 write(sh, getByteOrder()); 82 } 83 84 public void write(char[] sh, int byteOrder) throws IOException { 85 write(sh, 0, sh.length, byteOrder); 86 } 87 88 public void write(char[] sh, int offset, int length) throws IOException { 89 write(sh, offset, length, getByteOrder()); 90 } 91 92 public void write(char[] sh, int offset, int length, int byteOrder) throws IOException { 93 RandomAccessRO ro = RandomAccessFactory.createBufferedRO(sh, offset, length); 94 ro.setByteOrder(byteOrder); 95 byte[] b = new byte[length << 1]; 96 ro.readFully(b); 97 write(b); 98 } 99 100 public void write(int[] source) throws IOException { 101 write(source, getByteOrder()); 102 } 103 104 public void write(int[] source, int byteOrder) throws IOException { 105 write(source, 0, source.length, byteOrder); 106 } 107 108 public void write(int[] source, int offset, int length) throws IOException { 109 write(source, offset, length, getByteOrder()); 110 } 111 112 public void write(int[] source, int offset, int length, int byteOrder) throws IOException { 113 RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length); 114 ro.setByteOrder(byteOrder); 115 byte[] b = new byte[length << 2]; 116 ro.readFully(b); 117 write(b); 118 } 119 120 public void write(float[] source) throws IOException { 121 write(source, getByteOrder()); 122 } 123 124 public void write(float[] source, int byteOrder) throws IOException { 125 write(source, 0, source.length, byteOrder); 126 } 127 128 public void write(float[] source, int offset, int length) throws IOException { 129 write(source, offset, length, getByteOrder()); 130 } 131 132 public void write(float[] source, int offset, int length, int byteOrder) throws IOException { 133 RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length); 134 ro.setByteOrder(byteOrder); 135 byte[] b = new byte[length << 2]; 136 ro.readFully(b); 137 write(b); 138 } 139 140 public void write(long[] source) throws IOException { 141 write(source, getByteOrder()); 142 } 143 144 public void write(long[] source, int byteOrder) throws IOException { 145 write(source, 0, source.length, byteOrder); 146 } 147 148 public void write(long[] source, int offset, int length) throws IOException { 149 write(source, offset, length, getByteOrder()); 150 } 151 152 public void write(long[] source, int offset, int length, int byteOrder) throws IOException { 153 RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length); 154 ro.setByteOrder(byteOrder); 155 byte[] b = new byte[length << 3]; 156 ro.readFully(b); 157 write(b); 158 } 159 160 public void write(double[] source) throws IOException { 161 write(source, getByteOrder()); 162 } 163 164 public void write(double[] source, int byteOrder) throws IOException { 165 write(source, 0, source.length, byteOrder); 166 } 167 168 public void write(double[] source, int offset, int length) throws IOException { 169 write(source, offset, length, getByteOrder()); 170 } 171 172 public void write(double[] source, int offset, int length, int byteOrder) throws IOException { 173 RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length); 174 ro.setByteOrder(byteOrder); 175 byte[] b = new byte[length << 3]; 176 ro.readFully(b); 177 write(b); 178 } 179 180 public final void writeFloat(float v) throws IOException { 181 final int a = Float.floatToIntBits(v); 182 writeInt(a); 183 } 184 185 public final void writeDouble(double v) throws IOException { 186 writeLong(Double.doubleToLongBits(v)); 187 } 188 189 public void writeBytes(String s) throws IOException { 190 write(s.getBytes()); 191 } 192 193 public void writeChars(String s) throws IOException { 194 for(int i = 0; i < s.length(); i++) { 195 writeChar(s.charAt(i)); 196 } 197 } 198 199 public void writeUTF(String str) throws IOException { 200 ByteArrayOutputStream out = new ByteArrayOutputStream (str.length()); 201 DataOutputStream dataOut = new DataOutputStream (out); 202 dataOut.writeUTF(str); 203 dataOut.flush(); 204 dataOut.close(); 205 byte[] b = out.toByteArray(); 206 write(b); 207 } 208 209 public void writeShort(int v) throws IOException { 210 dataOutput.writeShort(v); 211 } 212 213 public void writeChar(int v) throws IOException { 214 dataOutput.writeChar(v); 215 } 216 217 public void writeInt(int v) throws IOException { 218 dataOutput.writeInt(v); 219 } 220 221 public void writeLong(long v) throws IOException { 222 dataOutput.writeLong(v); 223 } 224 225 public void setByteOrder(int byteOrder) throws IOException { 226 if(byteOrder == RandomAccessFactory.AUTO_ENDIAN) { 227 byteOrder = readByteOrder(); 228 } 229 if(this.byteOrder == byteOrder) { 230 return; 231 } 232 switch(byteOrder) { 233 case BIG_ENDIAN: 234 dataOutput = dobe; 235 break; 236 case LITTLE_ENDIAN: 237 dataOutput = dole; 238 break; 239 default: 240 throw new RuntimeException ("Wrong byteOrder:" + byteOrder); 241 } 242 super.setByteOrder(byteOrder); 243 } 244 245 250 protected void _setByteOrder(int byteOrder) throws IOException { 251 if(byteOrder == RandomAccessFactory.AUTO_ENDIAN) { 252 byteOrder = readByteOrder(); 253 } 254 if(this.byteOrder == byteOrder) { 255 return; 256 } 257 switch(byteOrder) { 258 case BIG_ENDIAN: 259 dataOutput = dobe; 260 break; 261 case LITTLE_ENDIAN: 262 dataOutput = dole; 263 break; 264 default: 265 byteOrder = BIG_ENDIAN; 266 dataOutput = dobe; 267 } 268 super._setByteOrder(byteOrder); 269 } 270 271 class DataOutputLE implements EDataOutput { 272 public final void writeShort(int a) throws IOException { 273 write(a & 0xFF); 274 write((a >> 8) & 0xFF); 275 } 276 277 public final void writeChar(int a) throws IOException { 278 write(a & 0xFF); 279 write((a >> 8) & 0xFF); 280 } 281 282 public final void writeInt(int a) throws IOException { 283 write(a & 0xFF); 284 write((a >>> 8) & 0xFF); 285 write((a >>> 16) & 0xFF); 286 write((a >>> 24) & 0xFF); 287 } 288 289 public final void writeLong(long a) throws IOException { 290 writeInt((int) (a & 0xFFFFFFFF)); 291 writeInt((int) ((a >>> 32) & 0xFFFFFFFF)); 292 } 293 } 294 295 class DataOutputBE implements EDataOutput { 296 public void writeShort(int a) throws IOException { 297 write((a >>> 8) & 0xFF); 298 write(a & 0xFF); 299 } 300 301 public void writeChar(int a) throws IOException { 302 write((a >>> 8) & 0xFF); 303 write(a & 0xFF); 304 } 305 306 public void writeInt(int a) throws IOException { 307 write((a >>> 24) & 0xFF); 308 write((a >>> 16) & 0xFF); 309 write((a >>> 8) & 0xFF); 310 write(a & 0xFF); 311 } 312 313 public void writeLong(long a) throws IOException { 314 writeInt((int) ((a >>> 32) & 0xFFFFFFFF)); 315 writeInt((int) (a & 0xFFFFFFFF)); 316 } 317 } 318 } 319 | Popular Tags |