Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/anotherbigidea/io/Byte4ByteDebugStreams.java


1   /****************************************************************
2    * Copyright (c) 2001, David N. Main, All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or
5    * without modification, are permitted provided that the 
6    * following conditions are met:
7    *
8    * 1. Redistributions of source code must retain the above 
9    * copyright notice, this list of conditions and the following 
10   * disclaimer. 
11   * 
12   * 2. Redistributions in binary form must reproduce the above 
13   * copyright notice, this list of conditions and the following 
14   * disclaimer in the documentation and/or other materials 
15   * provided with the distribution.
16   * 
17   * 3. The name of the author may not be used to endorse or 
18   * promote products derived from this software without specific 
19   * prior written permission. 
20   * 
21   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY 
22   * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
23   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
24   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
25   * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
27   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
28   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
29   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
30   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
31   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
32   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   ****************************************************************/
34  package com.anotherbigidea.io;
35  
36  import java.io.*;
37  
38  public class Byte4ByteDebugStreams extends OutputStream 
39  {
40      protected ByteArrayInputStream in;
41      
42      protected byte[] bytesIn;
43      protected byte[] bytesOut;
44      
45      protected int bytePtr = 0;
46      
47      public Byte4ByteDebugStreams( String filenameIn ) throws Exception 
48      {
49          RandomAccessFile raIn = new RandomAccessFile( filenameIn, "r" );
50          
51          bytesIn  = new byte[ (int)raIn.length() ];
52          bytesOut = new byte[ (int)raIn.length() ];
53          raIn.readFully( bytesIn );
54          raIn.close();
55      }
56      
57      public Byte4ByteDebugStreams( byte[] bytesIn )
58      {
59          this.bytesIn = bytesIn;
60          bytesOut = new byte[ bytesIn.length ];    
61      }
62  
63      public InputStream getInputStream()
64      {
65          if( in != null ) return in;
66          
67          in = new ByteArrayInputStream( bytesIn );
68          
69          return in;
70      }
71      
72      public void setInputBytes( byte[] inBytes ) 
73      {
74          bytesIn = inBytes; 
75      }
76      
77      public void write( int b ) throws IOException
78      {
79          if( b > 127 ) b = (b & 0x7f) - 128;
80          
81          bytesOut[bytePtr] = (byte)b;
82          
83          if( bytesOut[bytePtr] != bytesIn[bytePtr] )
84          {
85              IOException ioe = new IOException("Byte mismatch between input and output at byte #" + bytePtr 
86                                                + " 0x" + Integer.toHexString( bytePtr )
87                                                + "\nexpected 0x" 
88                                                + Integer.toHexString(bytesIn[bytePtr]) 
89                                                + " but got 0x" 
90                                                +  Integer.toHexString(b));
91                  
92              ioe.printStackTrace();
93              
94              throw ioe;
95          }
96          
97          bytePtr++;
98      }
99  
100     public void write( String filenameOut ) throws IOException 
101     {
102         FileOutputStream out = new FileOutputStream( filenameOut );
103         
104         out.write( bytesOut );
105         out.flush();
106         out.close();
107     }
108 }