InputStream.java Source File (original) (raw)
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package org.hebe.mps;
00036
00037 import java.io.;
00038 import java.net.;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 public class InputStream {
00050 private Connection connection;
00051 private java.io.InputStream input;
00052
00053
00054
00055
00056
00057
00058
00059 public InputStream(Connection c) {
00060 connection = c;
00061 input = null;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070 public InputStream(java.io.InputStream i) {
00071 connection = null;
00072 input = i;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082 private int read() throws IOException, MPSException {
00083 int result = input.read();
00084 if (result < 0)
00085 throw new MPSConnectionClosedException("while reading");
00086
00087 return result;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097 private void open() throws MPSException {
00098 if (input == null)
00099 input = connection.getInputStream();
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 public int readint() throws MPSException {
00109 try {
00110 int result = 0;
00111 open();
00112 result = result | read();
00113 result = (result << 8) | read();
00114 result = (result << 8) | read();
00115 result = (result << 8) | read();
00116 return result;
00117 } catch (IOException e) {
00118 throw new MPSException("Error reading an int from remote end");
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129 public String readstring() throws MPSException {
00130 StringBuffer result = new StringBuffer();
00131
00132 try {
00133 char ch;
00134
00135 open();
00136 while (true) {
00137 ch = (char) read();
00138 if (ch == 0)
00139 break;
00140
00141 result.append(ch);
00142 }
00143 } catch (IOException e) {
00144 throw new MPSException("Error reading a string from remote end");
00145 }
00146
00147 return result.toString();
00148 }
00149
00150
00151
00152
00153
00154
00155
00156 public boolean readbool() throws MPSException {
00157 try {
00158 open();
00159 return (read() != 0);
00160 } catch (IOException e) {
00161 throw new MPSException("Error reading a bool from remote end");
00162 }
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 public long readlong() throws MPSException {
00172 try {
00173 long result = 0;
00174 open();
00175 result = result | read();
00176 result = (result << 8) | read();
00177 result = (result << 8) | read();
00178 result = (result << 8) | read();
00179 result = (result << 8) | read();
00180 result = (result << 8) | read();
00181 result = (result << 8) | read();
00182 result = (result << 8) | read();
00183 return result;
00184 } catch (IOException e) {
00185 throw new MPSException("Error reading a long from remote end");
00186 }
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 public char readchar() throws MPSException {
00196 try {
00197 open();
00198 return (char) read();
00199 } catch (IOException e) {
00200 throw new MPSException("Error reading a char from remote end");
00201 }
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 public String readReference() throws MPSException {
00213 return readstring();
00214 }
00215
00216
00217
00218
00219
00220
00221
00222 public float readfloat() throws MPSException {
00223 open();
00224 return Float.intBitsToFloat(readint());
00225 }
00226 }