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

Quick Search    Search Deep

Source code: org/fudaa/dodico/fortran/NativeBinaryInputStream.java


1   /*
2    * @file         NativeBinaryInputStream.java
3    * @creation     1998-08-19
4    * @modification $Date: 2002/12/17 16:47:12 $
5    * @license      GNU General Public License 2
6    * @copyright    (c)1998-2001 CETMEF 2 bd Gambetta F-60231 Compiegne
7    * @mail         devel@fudaa.org
8    */
9   
10  package org.fudaa.dodico.fortran;
11  
12  import java.io.*;
13  
14  /**
15   * Une classe etendant DataInputStream et permettant de facilement lire
16   * des fichiers binaires dependants de la machine (sparc, i386, ...)
17   * Attention: cette classe est utilisée par FortranBinaryInputStream. A chaque
18   * appel de fonction le compteur de FortranBinaryInputStream est incrémenté.
19   * Pour l'instant ,il ne faut pas chainer les appels de methodes.<br/>
20   * Ne pas faire:<br/>
21   * public int readToto()
22   * {
23   *   readToto2();
24   * }
25   * il y aura eu 2 increments dans FortranBinaryInputStream ( pour readToto
26   * et pour readToto2)) ce qui fausse la lecture.
27   *
28   * @version      $Revision: 1.8 $ $Date: 2002/12/17 16:47:12 $ by $Author: deniger $
29   * @author       Axel von Arnim
30   */
31  public class NativeBinaryInputStream
32               extends DataInputStream
33  {
34    public final static String X86="86";
35    public final static String X86_NAME="X86";
36    public final static String SPARC="sparc";
37    public final static String SPARC_NAME="SPARC";
38  
39    /**
40     * entier renvoye par la methode read.
41     */
42    private int nbByteLus_;
43  
44    int l1, l2, l3, l4, l5, l6, l7, l8;
45    int i1, i2, i3, i4;
46    int s1, s2;
47    byte buf[];
48  
49    public NativeBinaryInputStream(InputStream _in, String _machine) throws IOException
50    {
51      super(_in);
52      buf=null;
53      setMachineType(_machine);
54    }
55  
56  /**
57   * Renvoie a partir de l'identifiant de la machine <code>_desc</code>,
58   * l'identifiant correctement géré par cette classe. Si non trouvee, <code>null</code>
59   * est retourne.
60   */
61    public static String getMachineId(String _desc)
62    {
63      if(isX86(_desc)) return X86;
64      else if(isSparc(_desc)) return SPARC;
65      else return null;
66    }
67  
68  
69    /**
70     * Renvoie true si <code>_machine</code> est du type X86 ( si la chaine finit
71     * par <code>"X86"</code>).
72     */
73    public static boolean isX86(String _machine)
74    {
75      if(_machine==null) return false;
76      if( _machine.endsWith(X86) ) return true;
77      return false;
78    }
79  
80    /**
81     * Renvoie true, si <code>_machine</code> est un identifiant d'une machine
82     * sparc ( si finit par <code>"sparc"</code>).
83     */
84    public static boolean isSparc(String _machine)
85    {
86      if(_machine==null) return false;
87      if( _machine.endsWith(SPARC) ) return true;
88      return false;
89    }
90  
91    /**
92     * Renvoie <code>true</code> si <code>_machine</code> est geree par cette
93     * classe.
94     */
95    public static boolean isMachineKnown(String _machine)
96    {
97      return (isX86(_machine)||isSparc(_machine));
98    }
99  
100   public void setMachineType(String _machine)
101   {
102     if( isX86(_machine )) {
103       l1=i1=s1=0; l2=i2=s2=1; l3=i3=2; l4=i4=3; l5=4; l6=5; l7=6; l8=7;
104     } else {
105       l1=7; l2=6; l3=5; l4=4; l5=i1=3; l6=i2=2; l7=i3=s1=1; l8=i4=s2=0;
106     }
107   }
108 
109   public byte readInt_8() throws IOException
110   {
111     buf=new byte[1];
112     nbByteLus_=read(buf);
113     return buf[0];
114   }
115 
116   public short readUInt_8() throws IOException
117   {
118     buf=new byte[1];
119     nbByteLus_=read(buf);
120     return buf[0]<0?(short)(256+buf[0]):(short)buf[0];
121   }
122 
123   /**
124    * Indique si la fin du fichier est atteinte. Si  <code>true</code>
125    * ,cela signifie que la derniere lecture est erronnee.
126    * @return si fin du fichier.
127    */
128   public boolean isFinFichier()
129   {
130     return nbByteLus_==-1;
131   }
132 
133 
134   public short readInt_16() throws IOException
135   {
136     buf=new byte[2];
137     nbByteLus_=read(buf);
138     return (short)((buf[s1]<0?(256+buf[s1]):buf[s1])+
139                    (buf[s2]*0x100));
140   }
141 
142   public int readUInt_16() throws IOException
143   {
144     buf=new byte[2];
145     nbByteLus_=read(buf);
146     return (buf[s1]<0?(256+buf[s1]):buf[s1])+
147            (buf[s2]<0?(256+buf[s2]):buf[s2])*0x100;
148   }
149 
150   public int readInt_32() throws IOException
151   {
152     buf=new byte[4];
153     nbByteLus_=read(buf);
154     return (buf[i1]<0?(256+buf[i1]):buf[i1])+
155            (buf[i2]<0?(256+buf[i2]):buf[i2])*0x100+
156            (buf[i3]<0?(256+buf[i3]):buf[i3])*0x10000+
157            (buf[i4]*0x1000000);
158   }
159 
160   public long readUInt_32() throws IOException
161   {
162     buf=new byte[4];
163     nbByteLus_=read(buf);
164     return (buf[i1]<0?(256+buf[i1]):buf[i1])+
165            (buf[i2]<0?(256+buf[i2]):buf[i2])*0x100L+
166            (buf[i3]<0?(256+buf[i3]):buf[i3])*0x10000L+
167            (buf[i4]<0?(256+buf[i4]):buf[i4])*0x1000000L;
168   }
169 
170   public long readInt_64() throws IOException
171   {
172     buf=new byte[8];
173     nbByteLus_=read(buf);
174     return (buf[l1]<0?(256+buf[l1]):buf[l1])+
175            (buf[l2]<0?(256+buf[l2]):buf[l2])*0x100L+
176            (buf[l3]<0?(256+buf[l3]):buf[l3])*0x10000L+
177            (buf[l4]<0?(256+buf[l4]):buf[l4])*0x1000000L+
178            (buf[l5]<0?(256+buf[l5]):buf[l5])*0x100000000L+
179            (buf[l6]<0?(256+buf[l6]):buf[l6])*0x10000000000L+
180            (buf[l7]<0?(256+buf[l7]):buf[l7])*0x1000000000000L+
181            (buf[l8])*0x100000000000000L;
182   }
183 
184   public float readFloat_32() throws IOException
185   {
186     /*Reviens a faire readInt_32().
187     Ne pas utiliser cette méthode car fausse l'incrementation de
188     FortranBinaryInputStream*/
189     buf=new byte[4];
190     nbByteLus_=read(buf);
191     int temp= (buf[i1]<0?(256+buf[i1]):buf[i1])+
192            (buf[i2]<0?(256+buf[i2]):buf[i2])*0x100+
193            (buf[i3]<0?(256+buf[i3]):buf[i3])*0x10000+
194            (buf[i4]*0x1000000);
195     return Float.intBitsToFloat(temp);
196   }
197 
198   public double readFloat_64() throws IOException
199   {
200     /*Reviens a faire readInt_64().
201     Ne pas utiliser cette méthode car fausse l'incrementation de
202     FortranBinaryInputStream*/
203     buf=new byte[8];
204     nbByteLus_=read(buf);
205     long temp= (buf[l1]<0?(256+buf[l1]):buf[l1])+
206            (buf[l2]<0?(256+buf[l2]):buf[l2])*0x100L+
207            (buf[l3]<0?(256+buf[l3]):buf[l3])*0x10000L+
208            (buf[l4]<0?(256+buf[l4]):buf[l4])*0x1000000L+
209            (buf[l5]<0?(256+buf[l5]):buf[l5])*0x100000000L+
210            (buf[l6]<0?(256+buf[l6]):buf[l6])*0x10000000000L+
211            (buf[l7]<0?(256+buf[l7]):buf[l7])*0x1000000000000L+
212            (buf[l8])*0x100000000000000L;
213     return Double.longBitsToDouble(temp);
214   }
215 }