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

Quick Search    Search Deep

Source code: com/yaftp/utils/EbcdicString.java


1    /**
2    *
3    * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package com.yaftp.utils ;
20  
21  
22  /**
23  Copyright Jean-Yves MENGANT 1998,1999,2000
24  
25  
26     The class bellow Implements a new EBCDIC string data type
27  
28    EbcdicString class EXTENDS DataStructure
29  
30    // Construct object using given Byte Array
31    public EbcdicString ( byte GivenBuffer[] ,
32                          int  BufferPos     , // Starting Position
33                          int  CurSize
34                        )
35    // Convert to ebcdic into _EbcdicBuffer
36    // if origin is larger than dest , exception is thrown
37    public void toEbcdic ( String InStr )
38    throws ArrayIndexOutOfBoundsException
39  
40    // Convert into JavaStr and return the value
41    public String toString()
42  
43    @author Jean-Yves MENGANT
44  
45  */
46  import java.io.* ;
47  
48  public class EbcdicString extends DataStructure  {
49  
50    // Instance private data
51    private byte[]       _AsciiBuffer      ; // Conversion Ascii buffer
52    private EbcdicTable  _conversionTable  ; // provided by OS constructor
53  
54    /*///////////////////////////////////////////////////////////////*/
55    /* PUBLIC STARTS HERE                                            */
56    /*///////////////////////////////////////////////////////////////*/
57  
58    /**
59      Construct object using given Byte Array
60    */
61    public EbcdicString ( EbcdicTable conversionTable ,
62                          byte GivenBuffer[] ,
63                          int  BufferPos     , // Starting Position
64                          int  CurSize
65                        )
66    {
67      super ( GivenBuffer , BufferPos , CurSize ) ;
68      _conversionTable = conversionTable          ;
69    }
70  
71    public EbcdicString ( EbcdicTable conversionTable ,
72                          byte GivenBuffer[]
73                        )
74    {
75      super ( GivenBuffer , 0 , GivenBuffer.length ) ;
76      _conversionTable = conversionTable  ;
77    }
78  
79    private byte FromEbcdic( int Ii )
80    {
81    int Pos = GiveElem(Ii) ;
82    int tEbcdic2Ascii[] = _conversionTable.get_tEbcdic2Ascii() ;
83  
84      if ( Pos < 0  )
85        return ((byte)(tEbcdic2Ascii[256+Pos])) ;
86      else
87        return ((byte)(tEbcdic2Ascii[Pos])) ;
88    }
89  
90    /**
91      Convert to ebcdic into _EbcdicBuffer
92      if origin is larger than dest , exception is thrown
93    */
94    public void toEbcdic ( String InStr )
95    throws ArrayIndexOutOfBoundsException
96    {
97    int CopySize = InStr.length() ;
98      // First Copy In Place
99      if ( _Size < CopySize )
100       throw new ArrayIndexOutOfBoundsException("Input String too big") ;
101 
102     try {
103     byte wkByte[] = InStr.getBytes(ISO_LATIN_1) ;
104       System.arraycopy( wkByte , 0 , _Struct , _Offset , CopySize );
105     } catch ( UnsupportedEncodingException e )
106     { e.printStackTrace() ; }
107 
108     int tAscii2Ebcdic[] = _conversionTable.get_tAscii2Ebcdic() ;
109 
110     for ( int Ii = 0 ; Ii < CopySize ; Ii++ )
111       SetElem( Ii , (byte)(tAscii2Ebcdic[GiveElem(Ii)]) ) ;
112 
113     byte BlankChar = (byte)(tAscii2Ebcdic[(byte)(' ')]) ;
114 
115   for ( int Ii = CopySize ; Ii < _Size ; Ii++ )
116       SetElem( Ii , BlankChar ) ;
117   }
118 
119   /**
120      Convert into JavaStr and return the value
121   */
122   public String toString()
123   {
124     _AsciiBuffer  = new byte[_Size]   ;
125 
126     for ( int Ii = 0 ; Ii < (_Size-1) ; Ii++ )
127       _AsciiBuffer[Ii] = FromEbcdic(Ii) ;
128 
129     // Return a String
130     return( new String(_AsciiBuffer,0,_Size-1) );
131   }
132 
133   /**
134     use this method just to debug or as a sample implementation
135     of the EbcdicString class
136   */
137   public static void main ( String Argv[] )
138   {
139   byte myBuffer[] = new byte[50] ; // PIC X(50) cobol DATA with trailing blanks
140   EbcdicTable myEbcdic = new EbcdicTable() ;
141   myEbcdic.set_pageCode( EbcdicTable.USA_CANADA_00037 );
142   EbcdicString myEbcdicString = new EbcdicString( myEbcdic ,
143                                                   myBuffer ,
144                                                   0 ,
145                                                   myBuffer.length
146                                                  ) ;
147     /* convert to EBCDIC and store converted string into internal Array */
148     myEbcdicString.toEbcdic("Hello World") ;
149     String myString = myEbcdicString.toString() ;
150 
151     /*
152        convert back to String "Hello World should be displayed
153        padded with blanks (just call java trim() method when
154        you need to remove COBOL trailing blanks
155     */
156     System.out.println( myString  )  ;
157 
158 
159   }
160 }