1 6 21 22 package de.schlichtherle.io.util; 23 24 import java.io.DataOutput ; 25 import java.io.FilterOutputStream ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 29 42 public class LEDataOutputStream 43 extends FilterOutputStream  44 implements DataOutput { 45 46 47 private final byte[] buf = new byte[8]; 48 49 53 protected volatile long written; 54 55 64 public LEDataOutputStream(OutputStream out) { 65 super(out); 66 } 67 68 72 private final void incCount(int inc) { 73 final long temp = written + inc; 74 written = temp >= 0 ? temp : Long.MAX_VALUE; 75 } 76 77 91 public void write(int b) throws IOException { 92 out.write(b); 93 incCount(1); 94 } 95 96 110 public void write(byte b[], int off, int len) 111 throws IOException { 112 out.write(b, off, len); 113 incCount(len); 114 } 115 116 130 public final void writeBoolean(boolean b) throws IOException { 131 out.write(b ? 1 : 0); 132 incCount(1); 133 } 134 135 146 public final void writeByte(int b) throws IOException { 147 out.write(b); 148 incCount(1); 149 } 150 151 162 public final void writeChar(int c) throws IOException { 163 writeShort(c); 164 } 165 166 177 public final void writeShort(int s) throws IOException { 178 buf[0] = (byte) s; 179 s >>= 8; 180 buf[1] = (byte) s; 181 out.write(buf, 0, 2); 182 incCount(2); 183 } 184 185 196 public final void writeInt(int i) throws IOException { 197 buf[0] = (byte) i; 198 i >>= 8; 199 buf[1] = (byte) i; 200 i >>= 8; 201 buf[2] = (byte) i; 202 i >>= 8; 203 buf[3] = (byte) i; 204 out.write(buf, 0, 4); 205 incCount(4); 206 } 207 208 219 public final void writeLong(long l) throws IOException { 220 buf[0] = (byte) l; 221 l >>= 8; 222 buf[1] = (byte) l; 223 l >>= 8; 224 buf[2] = (byte) l; 225 l >>= 8; 226 buf[3] = (byte) l; 227 l >>= 8; 228 buf[4] = (byte) l; 229 l >>= 8; 230 buf[5] = (byte) l; 231 l >>= 8; 232 buf[6] = (byte) l; 233 l >>= 8; 234 buf[7] = (byte) l; 235 out.write(buf, 0, 8); 236 incCount(8); 237 } 238 239 254 public final void writeFloat(float f) throws IOException { 255 writeInt(Float.floatToIntBits(f)); 256 } 257 258 273 public final void writeDouble(double d) throws IOException { 274 writeLong(Double.doubleToLongBits(d)); 275 } 276 277 290 public final void writeBytes(String s) throws IOException { 291 final int len = s.length(); 292 for (int i = 0 ; i < len ; i++) 293 writeByte(s.charAt(i)); 294 } 295 296 310 public final void writeChars(String s) throws IOException { 311 final int len = s.length(); 312 for (int i = 0 ; i < len ; i++) 313 writeShort(s.charAt(i)); 314 } 315 316 321 public void writeUTF(String str) throws IOException { 322 throw new UnsupportedOperationException (); 323 } 324 325 334 public final long size() { 335 return written; 336 } 337 } 338 | Popular Tags |