예제 #1
0
 /**
  * Reads a sequence of bits from standard input (that are encoded
  * using run-length encoding with 8-bit run lengths); decodes them;
  * and writes the results to standard output.
  */
 public static void expand() { 
     boolean b = false; 
     while (!BinaryStdIn.isEmpty()) {
         int run = BinaryStdIn.readInt(LG_R);
         for (int i = 0; i < run; i++)
             BinaryStdOut.write(b);
         b = !b;
     }
     BinaryStdOut.close();
 }
예제 #2
0
 /**
  * Reads in a sequence of bytes from standard input and draws
  * them to standard drawing output as a width-by-height picture,
  * using black for 1 and white for 0 (and red for any leftover
  * pixels).
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) {
     int width = Integer.parseInt(args[0]);
     int height = Integer.parseInt(args[1]);
     Picture picture = new Picture(width, height);
     for (int row = 0; row < height; row++) {
         for (int col = 0; col < width; col++) {
             if (!BinaryStdIn.isEmpty()) {
                 boolean bit = BinaryStdIn.readBoolean();
                 if (bit) picture.set(col, row, Color.BLACK);
                 else     picture.set(col, row, Color.WHITE);
             }
             else {
                 picture.set(col, row, Color.RED);
             }
         }
     }
     picture.show();
 }
예제 #3
0
    /**
     * Reads in a sequence of bytes from standard input and writes
     * them to standard output in binary, k bits per line,
     * where k is given as a command-line integer (defaults
     * to 16 if no integer is specified); also writes the number
     * of bits.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int bitsPerLine = 16;
        if (args.length == 1) {
            bitsPerLine = Integer.parseInt(args[0]);
        }

        int count;
        for (count = 0; !BinaryStdIn.isEmpty(); count++) {
            if (bitsPerLine == 0) {
                BinaryStdIn.readBoolean();
                continue;
            }
            else if (count != 0 && count % bitsPerLine == 0) StdOut.println();
            if (BinaryStdIn.readBoolean()) StdOut.print(1);
            else                           StdOut.print(0);
        }
        if (bitsPerLine != 0) StdOut.println();
        StdOut.println(count + " bits");
    }
예제 #4
0
    /**
     * Reads in a sequence of bytes from standard input and writes
     * them to standard output using hexademical notation, k hex digits
     * per line, where k is given as a command-line integer (defaults
     * to 16 if no integer is specified); also writes the number
     * of bits.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int bytesPerLine = 16;
        if (args.length == 1) {
            bytesPerLine = Integer.parseInt(args[0]);
        }

        int i;
        for (i = 0; !BinaryStdIn.isEmpty(); i++) {
            if (bytesPerLine == 0) {
                BinaryStdIn.readChar();
                continue;
            }
            if (i == 0) StdOut.printf("");
            else if (i % bytesPerLine == 0) StdOut.printf("\n", i);
            else StdOut.print(" ");
            char c = BinaryStdIn.readChar();
            StdOut.printf("%02x", c & 0xff);
        }
        if (bytesPerLine != 0) StdOut.println();
        StdOut.println((i*8) + " bits");
    }
예제 #5
0
 /**
  * Reads a sequence of bits from standard input; compresses
  * them using run-length coding with 8-bit run lengths; and writes the
  * results to standard output.
  */
 public static void compress() { 
     char run = 0; 
     boolean old = false;
     while (!BinaryStdIn.isEmpty()) { 
         boolean b = BinaryStdIn.readBoolean();
         if (b != old) {
             BinaryStdOut.write(run, LG_R);
             run = 1;
             old = !old;
         }
         else { 
             if (run == R-1) { 
                 BinaryStdOut.write(run, LG_R);
                 run = 0;
                 BinaryStdOut.write(run, LG_R);
             }
             run++;
         } 
     } 
     BinaryStdOut.write(run, LG_R);
     BinaryStdOut.close();
 }