001         package com.croftsoft.core.io;
002    
003         import java.io.*;
004         import java.util.*;
005    
006         /*********************************************************************
007         *
008         * A library of static methods for the manipulation of input and
009         * output streams.
010         *
011         * <P>
012         *
013         * @version
014         *   2000-04-30
015         * @since
016         *   1999-08-15
017         * @author
018         *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
019         *********************************************************************/
020    
021         public final class  StreamLib
022         //////////////////////////////////////////////////////////////////////
023         //////////////////////////////////////////////////////////////////////
024         {
025    
026         private  StreamLib ( ) { }
027    
028         //////////////////////////////////////////////////////////////////////
029         //////////////////////////////////////////////////////////////////////
030    
031         public static void  main ( String [ ]  args )
032         //////////////////////////////////////////////////////////////////////
033         {
034           System.out.println ( System.getProperty ( "file.encoding" ) );
035         }
036    
037         /*********************************************************************
038         * Replaces bytes in a stream as it is being copied from the
039         * InputStream to the OutputStream.
040         *
041         * <P>
042         * 
043         * Does not automatically close the streams after completion.
044         * Maintains an internal buffer with the same size as oldBytes.
045         *
046         * @return
047         *   Returns true if bytes were replaced.
048         *********************************************************************/
049         public static boolean  replaceBytes (
050           InputStream   in,
051           OutputStream  out,
052           byte [ ]      oldBytes,
053           byte [ ]      newBytes )
054           throws IOException
055         //////////////////////////////////////////////////////////////////////
056         {
057           byte [ ]  bufBytes = new byte [ oldBytes.length ];
058    
059           boolean  hasChanged = false;
060    
061           int  i;
062    
063           byte  b;
064    
065           int  index = 0;
066    
067           while ( ( i = in.read ( ) ) > -1 )
068           {
069             b = ( byte ) i;
070    
071             if ( b == oldBytes [ index ] )
072             {
073               if ( index + 1 < bufBytes.length )
074               {
075                 bufBytes [ index++ ] = b;
076               }
077               else
078               {
079                 out.write ( newBytes );
080    
081                 hasChanged = true;
082    
083                 index = 0;
084               }
085             }
086             else
087             {
088               out.write ( bufBytes, 0, index );
089    
090               out.write ( b );
091    
092               index = 0;
093             }
094           }
095    
096           return hasChanged;
097         }
098    
099         /*********************************************************************
100         * Converts the bytes of the InputStream to a String.
101         *
102         * <P>
103         * 
104         * Does not automatically close the stream after completion.
105         *********************************************************************/
106         public static String  toString (
107           InputStream  inputStream,
108           String       encoding )
109           throws IOException, UnsupportedEncodingException
110         //////////////////////////////////////////////////////////////////////
111         {
112           ByteArrayOutputStream  byteArrayOutputStream
113             = new ByteArrayOutputStream ( );
114    
115           int  i;
116    
117           while ( ( i = inputStream.read ( ) ) > -1 )
118           {
119             byteArrayOutputStream.write ( i );
120           }
121    
122           return byteArrayOutputStream.toString ( encoding );
123         }
124    
125         /*********************************************************************
126         * Converts the bytes of the InputStream to a String.
127         *
128         * <P>
129         * 
130         * Does not automatically close the stream after completion.
131         *********************************************************************/
132         public static String  toString (
133           InputStream  inputStream )
134           throws IOException
135         //////////////////////////////////////////////////////////////////////
136         {
137           ByteArrayOutputStream  byteArrayOutputStream
138             = new ByteArrayOutputStream ( );
139    
140           int  i;
141    
142           while ( ( i = inputStream.read ( ) ) > -1 )
143           {
144             byteArrayOutputStream.write ( i );
145           }
146    
147           return byteArrayOutputStream.toString ( );
148         }
149    
150         //////////////////////////////////////////////////////////////////////
151         //////////////////////////////////////////////////////////////////////
152         }