Math2mat
|
00001 package m2m.backend.verifpga; 00002 00003 /************************************************************************* 00004 * Compilation: javac BinaryOut.java 00005 * Execution: java BinaryOut 00006 * 00007 * Write binary data to an output stream, either one 1-bit boolean, 00008 * one 8-bit char, one 32-bit int, one 64-bit double, one 32-bit float, 00009 * or one 64-bit long at a time. The output stream can be standard 00010 * output, a file, an OutputStream or a Socket. 00011 * 00012 * The bytes written are not aligned. 00013 * 00014 * Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne. 00015 * 00016 *************************************************************************/ 00017 00018 import java.io.IOException; 00019 import java.io.BufferedOutputStream; 00020 import java.io.FileOutputStream; 00021 import java.io.OutputStream; 00022 import java.net.Socket; 00023 00037 public final class BinaryOut { 00038 00039 private BufferedOutputStream out; // the output stream 00040 private int buffer; // 8-bit buffer of bits to write out 00041 private int N; // number of bits remaining in buffer 00042 00043 00047 public BinaryOut(OutputStream os) { 00048 out = new BufferedOutputStream(os); 00049 } 00050 00054 public BinaryOut() { 00055 this(System.out); 00056 } 00057 00061 public BinaryOut(String s) { 00062 try { 00063 OutputStream os = new FileOutputStream(s); 00064 out = new BufferedOutputStream(os); 00065 } 00066 catch (IOException e) { e.printStackTrace(); } 00067 } 00068 00072 public BinaryOut(Socket socket) { 00073 try { 00074 OutputStream os = socket.getOutputStream(); 00075 out = new BufferedOutputStream(os); 00076 } 00077 catch (IOException e) { e.printStackTrace(); } 00078 } 00079 00080 00084 private void writeBit(boolean bit) { 00085 // add bit to buffer 00086 buffer <<= 1; 00087 if (bit) buffer |= 1; 00088 00089 // if buffer is full (8 bits), write out as a single byte 00090 N++; 00091 if (N == 8) clearBuffer(); 00092 } 00093 00097 private void writeByte(int x) { 00098 assert x >= 0 && x < 256; 00099 00100 // optimized if byte-aligned 00101 if (N == 0) { 00102 try { out.write(x); } 00103 catch (IOException e) { e.printStackTrace(); } 00104 return; 00105 } 00106 00107 // otherwise write one bit at a time 00108 for (int i = 0; i < 8; i++) { 00109 boolean bit = ((x >>> (8 - i - 1)) & 1) == 1; 00110 writeBit(bit); 00111 } 00112 } 00113 00114 // write out any remaining bits in buffer to the binary output stream, padding with 0s 00115 private void clearBuffer() { 00116 if (N == 0) return; 00117 if (N > 0) buffer <<= (8 - N); 00118 try { out.write(buffer); } 00119 catch (IOException e) { e.printStackTrace(); } 00120 N = 0; 00121 buffer = 0; 00122 } 00123 00128 public void flush() { 00129 clearBuffer(); 00130 try { out.flush(); } 00131 catch (IOException e) { e.printStackTrace(); } 00132 } 00133 00137 public void close() { 00138 flush(); 00139 try { out.close(); } 00140 catch (IOException e) { e.printStackTrace(); } 00141 } 00142 00143 00148 public void write(boolean x) { 00149 writeBit(x); 00150 } 00151 00156 public void write(byte x) { 00157 writeByte(x & 0xff); 00158 } 00159 00164 public void write(int x) { 00165 writeByte((x >>> 24) & 0xff); 00166 writeByte((x >>> 16) & 0xff); 00167 writeByte((x >>> 8) & 0xff); 00168 writeByte((x >>> 0) & 0xff); 00169 } 00170 00178 public void write(int x, int r) { 00179 if (r == 32) write(x); 00180 if (r < 1 || r > 32) throw new RuntimeException("Illegal value for r = " + r); 00181 if (x < 0 || x >= (1 << r)) throw new RuntimeException("Illegal " + r + "-bit char = " + x); 00182 for (int i = 0; i < r; i++) { 00183 boolean bit = ((x >>> (r - i - 1)) & 1) == 1; 00184 writeBit(bit); 00185 } 00186 } 00187 00188 00193 public void write(double x) { 00194 write(Double.doubleToRawLongBits(x)); 00195 } 00196 00201 public void write(long x) { 00202 writeByte((int) ((x >>> 56) & 0xff)); 00203 writeByte((int) ((x >>> 48) & 0xff)); 00204 writeByte((int) ((x >>> 40) & 0xff)); 00205 writeByte((int) ((x >>> 32) & 0xff)); 00206 writeByte((int) ((x >>> 24) & 0xff)); 00207 writeByte((int) ((x >>> 16) & 0xff)); 00208 writeByte((int) ((x >>> 8) & 0xff)); 00209 writeByte((int) ((x >>> 0) & 0xff)); 00210 } 00211 00216 public void write(float x) { 00217 write(Float.floatToRawIntBits(x)); 00218 } 00219 00224 public void write(short x) { 00225 writeByte((x >>> 8) & 0xff); 00226 writeByte((x >>> 0) & 0xff); 00227 } 00228 00234 public void write(char x) { 00235 if (x < 0 || x >= 256) throw new RuntimeException("Illegal 8-bit char = " + x); 00236 writeByte(x); 00237 } 00238 00246 public void write(char x, int r) { 00247 if (r == 8) write(x); 00248 if (r < 1 || r > 16) throw new RuntimeException("Illegal value for r = " + r); 00249 if (x < 0 || x >= (1 << r)) throw new RuntimeException("Illegal " + r + "-bit char = " + x); 00250 for (int i = 0; i < r; i++) { 00251 boolean bit = ((x >>> (r - i - 1)) & 1) == 1; 00252 writeBit(bit); 00253 } 00254 } 00255 00262 public void write(String s) { 00263 for (int i = 0; i < s.length(); i++) 00264 write(s.charAt(i)); 00265 } 00266 00267 00276 public void write(String s, int r) { 00277 for (int i = 0; i < s.length(); i++) 00278 write(s.charAt(i), r); 00279 } 00280 00281 }