1    
2    /*
3    
4      The author of this software is Ian Kaplan
5      Bear Products International
6      www.bearcave.com
7      iank@bearcave.com
8    
9      Copyright (c) Ian Kaplan, 1999, 2000
10   
11     See copyright file for usage and licensing
12   
13   */
14   
15   package util;
16   
17   import java.io.*;
18   
19   /*
20    * dataRead
21    *
22   
23      Support read of u1, u2 and u4 data items.
24   
25    */
26   public class dataRead {
27     private static int bytesRead = 0;
28   
29     public final int getBytesRead() { return bytesRead; }
30   
31     //
32     // readU1: read an 8-bit byte
33     // 
34     // Is there a problem with sign extend here?  Java's lack of
35     // unsigned types is a pain.  What would be nice is an unsigned
36     // byte.
37     //
38     public final int readU1( DataInputStream dStream ) {
39       int byteVal = -1;
40   
41       try {
42         byteVal = dStream.readUnsignedByte();
43         bytesRead++;
44       } catch (Exception e) {
45         if (e instanceof IOException) {
46   	if (! (e instanceof EOFException)) {
47   	  errorMessage.errorPrint( "(readU1): " + e.getMessage() );
48   	}
49   	else {
50   	  errorMessage.errorPrint( "(readU1): unexpected end of file" + 
51   				   e.getMessage() );
52   	}
53         }
54         else {
55   	errorMessage.errorPrint("(readU1): " + e.toString() + 
56   				"unexpected exception" );
57         }
58       } // catch
59   
60       return byteVal;
61     } // readU1
62   
63   
64     //
65     // readU2: read a 16-bit unsigned short
66     //
67     public final int readU2( DataInputStream dStream ) {
68       int shortVal = -1;
69   
70       try {
71         shortVal = dStream.readUnsignedShort();
72         bytesRead += 2;
73       } catch (Exception e) {
74         if (e instanceof IOException) {
75   	if (! (e instanceof EOFException)) {
76   	  errorMessage.errorPrint( "(readU2): " + e.getMessage() );
77   	}
78   	else {
79   	  errorMessage.errorPrint( "(readU2): unexpected end of file" + 
80   				   e.getMessage() );
81   	}
82         }
83         else {
84   	errorMessage.errorPrint("(readU2): " + e.toString() + 
85   				"unexpected exception" );
86         }
87       } // catch    
88       return shortVal;
89     } // readU2
90   
91   
92     //
93     // readU4: read a 32-bit int
94     //
95     public final int readU4( DataInputStream dStream ) {
96       int intVal = -1;
97   
98       try {
99         intVal = dStream.readInt();
100        bytesRead += 4;
101      } catch (Exception e) {
102        if (e instanceof IOException) {
103  	if (! (e instanceof EOFException)) {
104  	  errorMessage.errorPrint( "(readU4): " + e.getMessage() );
105  	}
106  	else {
107  	  errorMessage.errorPrint( "(readU4): unexpected end of file" + 
108  				   e.getMessage() );
109  	}
110        }
111        else {
112  	errorMessage.errorPrint("(readU4): " + e.toString() + 
113  				"unexpected exception" );
114        }
115      } // catch    
116      return intVal;
117    } // readU4
118  
119  } // dataRead
120  
121