Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.util.io;
 
 import java.io.Writer;

A kind of super-formatter. It is sent a stream of binary data and formats it in a human-readable dump format which is forwarded to its output stream.

Currently, output is in hex though options to change that may be introduced.

Author(s):
Howard Lewis Ship
 
 
 public class BinaryDumpOutputStream extends OutputStream
 {
     private static final char[] HEX =
     { '0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f' };
 
     private PrintWriter _out;
 
     private boolean locked = false;
 
     private boolean _showOffset = true;
 
     private int bytesPerLine = 16;
 
     private int _spacingInterval = 4;
 
     private char substituteChar = '.';
 
     private String offsetSeperator = ": ";
 
     private int offset = 0;
 
     private int lineCount = 0;
 
     private int bytesSinceSpace = 0;
 
     private char[] ascii = null;
 
     private boolean showAscii = true;
 
     private String asciiBegin = "  |";
 
     private String asciiEnd = "|";

    
Creates a PrintWriter for System.out.
 
 
     public BinaryDumpOutputStream()
     {
         this(new PrintWriter(.true));
     }
 
     public BinaryDumpOutputStream(PrintWriter out)
     {
         this. = out;
     }
 
     public BinaryDumpOutputStream(Writer out)
     {
         this. = new PrintWriter(out);
     }
 
     public void close() throws IOException
     {
         if ( != null)
         {
             if ( > 0)
                 finishFinalLine();
 
             .close();
         }
 
          = null;
     }
 
     private void finishFinalLine()
     {
         // Since we only finish the final line after at least one byte has
         // been written to it, we don't need to worry about
        // the offset.
        while ( < )
        {
            // After every <n> bytes, emit a space.
            if ( > 0 &&  == )
            {
                .print(' ');
                 = 0;
            }
            // Two spaces to substitute for the two hex digits.
            .print("  ");
            if ()
                [] = ' ';
            ++;
            ++;
        }
        if ()
        {
            .print();
            .print();
            .print();
        }
        .println();
    }

    
Forwards the flush() to the PrintWriter.
    public void flush() throws IOException
    {
        .flush();
    }
    public String getAsciiBegin()
    {
        return ;
    }
    public String getAsciiEnd()
    {
        return ;
    }
    public int getBytesPerLine()
    {
        return ;
    }
    public String getOffsetSeperator()
    {
        return ;
    }
    public boolean getShowAscii()
    {
        return ;
    }
    public char getSubstituteChar()
    {
        return ;
    }
    public void setAsciiBegin(String value)
    {
        if ()
            throw new IllegalStateException();
         = value;
    }
    public void setAsciiEnd(String value)
    {
        if ()
            throw new IllegalStateException();
         = value;
    }
    public void setBytesPerLine(int value)
    {
        if ()
            throw new IllegalStateException();
         = value;
         = null;
    }
    public void setOffsetSeperator(String value)
    {
        if ()
            throw new IllegalStateException();
         = value;
    }
    public void setShowAscii(boolean value)
    {
        if ()
            throw new IllegalStateException();
         = value;
    }

    
Sets the character used in the ASCII dump that substitutes for characters outside the range of 32..126.
    public void setSubstituteChar(char value)
    {
        if ()
            throw new IllegalStateException();
         = value;
    }
    public void write(int bthrows IOException
    {
        char letter;
        if ( &&  == null)
             = new char[];
        // Prevent further customization after output starts being written.
         = true;
        if ( == )
        {
            if ()
            {
                .print();
                .print();
                .print();
            }
            .println();
             = 0;
             = 0;
             += ;
        }
        if ( == 0 && )
        {
            writeHex(, 4);
            .print();
        }
        // After every <n> bytes, emit a space.
        if ( > 0 &&  == )
        {
            .print(' ');
             = 0;
        }
        writeHex(b, 2);
        if ()
        {
            if (b < 32 | b > 127)
                letter = ;
            else
                letter = (charb;
            [] = letter;
        }
        ++;
        ++;
    }
    private void writeHex(int valueint digits)
    {
        int i;
        int nybble;
        for (i = 0; i < digitsi++)
        {
            nybble = (value >> 4 * (digits - i - 1)) & 0x0f;
            .print([nybble]);
        }
    }
    public void setSpacingInterval(int spacingInterval)
    {
        this. = spacingInterval;
    }
    public boolean isShowOffset()
    {
        return ;
    }
    public void setShowOffset(boolean showOffset)
    {
        this. = showOffset;
    }
    public int getSpacingInterval()
    {
        return ;
    }
New to GrepCode? Check out our FAQ X