Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/verifpga/BinaryIn.java

Go to the documentation of this file.
00001 /*************************************************************************
00002  *  Compilation:  javac BinaryIn.java
00003  *  Execution:    java BinaryIn input output
00004  *  
00005  *  This library is for reading binary data from an input stream.
00006  *
00007  *  % java BinaryIn http://www.cs.princeton.edu/introcs/cover.jpg output.jpg
00008  *  
00009  *  Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne. 
00010 *       Last updated: Wed Feb 17 14:34:37 EST 2010.
00011  *
00012  *************************************************************************/
00013 package m2m.backend.verifpga;
00014 
00015 import java.net.URLConnection;
00016 import java.net.URL;
00017 import java.net.Socket;
00018 import java.io.File;
00019 import java.io.IOException;
00020 import java.io.InputStream;
00021 import java.io.FileInputStream;
00022 import java.io.BufferedInputStream;
00023 
00024 
00025 
00045 public final class BinaryIn {
00046     private BufferedInputStream in;  // the input stream
00047     private final int EOF = -1;      // end of file
00048     private int buffer;              // one character buffer
00049     private int N;                   // number of bits left in buffer
00050 
00051     private void fillBuffer() {
00052         try {
00053                 buffer = in.read();
00054                 //System.out.println("fillBuffer: " + buffer);  
00055                 N = 8; 
00056         }
00057         catch (IOException e) { System.err.println("EOF"); buffer = EOF; N = -1; }
00058     }
00059 
00063     public BinaryIn() {
00064         in = new BufferedInputStream(System.in);
00065         fillBuffer();
00066     }
00067 
00071     public BinaryIn(InputStream is) {
00072         in = new BufferedInputStream(is);
00073         fillBuffer();
00074     }
00075 
00079     public BinaryIn(Socket socket) {
00080         try {
00081             InputStream is = socket.getInputStream();
00082             in = new BufferedInputStream(is);
00083             //fillBuffer();
00084         }
00085         catch (IOException ioe) {
00086             System.err.println("Could not open " + socket);
00087         }
00088         System.out.println("BinaryIn...");
00089     }
00090 
00094     public BinaryIn(URL url) {
00095         try {
00096             URLConnection site = url.openConnection();
00097             InputStream is     = site.getInputStream();
00098             in = new BufferedInputStream(is);
00099             fillBuffer();
00100         }
00101         catch (IOException ioe) {
00102             System.err.println("Could not open " + url);
00103         }
00104     }
00105 
00109     public BinaryIn(String s) {
00110 
00111         try {
00112             // first try to read file from local file system
00113             File file = new File(s);
00114             if (file.exists()) {
00115                 FileInputStream fis = new FileInputStream(file);
00116                 in = new BufferedInputStream(fis);
00117                 fillBuffer();
00118                 return;
00119             }
00120 
00121             // next try for files included in jar
00122             URL url = getClass().getResource(s);
00123 
00124             // or URL from web
00125             if (url == null) { url = new URL(s); }
00126 
00127             URLConnection site = url.openConnection();
00128             InputStream is     = site.getInputStream();
00129             in = new BufferedInputStream(is);
00130             fillBuffer();
00131         }
00132         catch (IOException ioe) {
00133             System.err.println("Could not open " + s);
00134         }
00135     }
00136 
00140     public boolean exists()  {
00141         return in != null;
00142     }
00143 
00144 
00149     public boolean isEmpty() {
00150         return buffer == EOF;
00151     }
00152 
00158     public boolean readBoolean() {
00159         if (isEmpty()) throw new RuntimeException("Reading from empty input stream");
00160         N--;
00161         boolean bit = ((buffer >> N) & 1) == 1;
00162         if (N == 0) fillBuffer();
00163         return bit;
00164     }
00165 
00171     public char readChar() {
00172         if (isEmpty()) throw new RuntimeException("Reading from empty input stream");
00173 
00174         // special case when aligned byte
00175         if (N == 8) {
00176             int x = buffer;
00177             fillBuffer();
00178             return (char) (x & 0xff);
00179         }
00180 
00181         // combine last N bits of current buffer with first 8-N bits of new buffer
00182         int x = buffer;
00183         x <<= (8-N);
00184         int oldN = N;
00185         fillBuffer();
00186         if (isEmpty()) throw new RuntimeException("Reading from empty input stream");
00187         N = oldN;
00188         x |= (buffer >>> N);
00189         return (char) (x & 0xff);
00190         // the above code doesn't quite work for the last character if N = 8
00191         // because buffer will be -1
00192     }
00193 
00194 
00201     public char readChar(int r) {
00202         if (r < 1 || r > 16) throw new RuntimeException("Illegal value of r = " + r);
00203 
00204         // optimize r = 8 case
00205         if (r == 8) return readChar();
00206 
00207         char x = 0;
00208         for (int i = 0; i < r; i++) {
00209             x <<= 1;
00210             boolean bit = readBoolean();
00211             if (bit) x |= 1;
00212         }
00213         return x;
00214     }
00215 
00216 
00223     public String readString() {
00224         if (isEmpty()) throw new RuntimeException("Reading from empty input stream");
00225 
00226         StringBuilder sb = new StringBuilder();
00227         while (!isEmpty()) {
00228             char c = readChar();
00229             sb.append(c);
00230         }
00231         return sb.toString();
00232     }
00233 
00234 
00240     public short readShort() {
00241         short x = 0;
00242         for (int i = 0; i < 2; i++) {
00243             char c = readChar();
00244             x <<= 8;
00245             x |= c;
00246         }
00247         return x;
00248     }
00249 
00255      public int readInt() {
00256          int x = 0;
00257          for (int i = 0; i < 4; i++) {
00258              char c = readChar();
00259              x <<= 8;
00260              x |= c;
00261          }
00262          return x;
00263      }
00264 
00270       public int readIntOther() {
00271           int x = 0;
00272           for (int i = 0; i < 4; i++) {
00273               char c = readChar();
00274               int y=c;
00275               for(int j=1;j<i;j++)
00276                   y <<= 8;
00277               x |= y;
00278           }
00279           return x;
00280       }
00281 
00288     public int readInt(int r) {
00289         if (r < 1 || r > 32) throw new RuntimeException("Illegal value of r = " + r);
00290 
00291         // optimize r = 32 case
00292         if (r == 32) return readInt();
00293 
00294         int x = 0;
00295         for (int i = 0; i < r; i++) {
00296             x <<= 1;
00297             boolean bit = readBoolean();
00298             if (bit) x |= 1;
00299         }
00300         return x;
00301     }
00302 
00308     public long readLong() {
00309         long x = 0;
00310         for (int i = 0; i < 8; i++) {
00311             char c = readChar();
00312             x <<= 8;
00313             x |= c;
00314         }
00315         return x;
00316     }
00317 
00323     public double readDouble() {
00324         return Double.longBitsToDouble(readLong());
00325     }
00326 
00332     public float readFloat() {
00333         return Float.intBitsToFloat(readInt());
00334     }
00335 
00336 
00342     public byte readByte() {
00343         char c = readChar();
00344         byte x = (byte) (c & 0xff);
00345         return x;
00346     }
00347     
00348 }
00349 
 All Classes Namespaces Files Functions Variables Enumerations