OutputStream.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 public class OutputStream { 00049 private Connection connection; 00050 private java.io.OutputStream output; 00051 private StringBuffer buffer; 00052 00053 00054 00055 00056 00057 00058 00059 public OutputStream(Connection c) { 00060 connection = c; 00061 output = null; 00062 setupBuffer(); 00063 } 00064 00065 00066 00067 00068 private void setupBuffer() { 00069 buffer = new StringBuffer(); 00070 } 00071 00072 00073 00074 public void resetBuffer() { 00075 setupBuffer(); 00076 } 00077 00078 00079 00080 00081 00082 00083 00084 public OutputStream(java.io.OutputStream o) { 00085 connection = null; 00086 output = o; 00087 setupBuffer(); 00088 } 00089 00090 00091 00092 00093 00094 00095 00096 00097 private void write(int ch) { 00098
00099 buffer.append((char) ch); 00100 00101
00102 00103 00104 00105 00106 00107 00108 } 00109 00110 00111 00112 00113 00114 00115 00116 00117 private void open() throws MPSException { 00118 if (output == null) 00119 output = connection.getOutputStream(); 00120 } 00121 00122 00123 00124 00125 00126 00127 public void writeint(int i) { 00128 write((i >> 24) & 255); 00129 write((i >> 16) & 255); 00130 write((i >> 8) & 255); 00131 write(i & 255); 00132 } 00133 00134 00135 00136 00137 00138 00139 public void writestring(String s) { 00140 for (int i = 0; i < s.length(); i++) { 00141 char c = s.charAt(i); 00142 if (c == 0) 00143 break; 00144 write(c); 00145 } 00146 write(0); 00147 } 00148 00149 00150 00151 00152 00153 00154 public void writebool(boolean b) { 00155 write(b ? 1 : 0); 00156 } 00157 00158 00159 00160 00161 00162 00163 public void writelong(long i) { 00164 write((int) (i >> 56) & 255); 00165 write((int) (i >> 48) & 255); 00166 write((int) (i >> 40) & 255); 00167 write((int) (i >> 32) & 255); 00168 write((int) (i >> 24) & 255); 00169 write((int) (i >> 16) & 255); 00170 write((int) (i >> 8) & 255); 00171 write((int) i & 255); 00172 } 00173 00174 00175 00176 00177 00178 00179 public void writechar(char ch) { 00180 write(ch); 00181 } 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 public void writeReference(Object o) throws MPSException { 00199 Server svr = Server.serverFor(o); 00200 00201 if (svr == null) { 00202
00203
00204 try { 00205 Proxy p = (Proxy) o; 00206 writestring(p.getReference()); 00207 } catch (ClassCastException cce) { 00208
00209 throw new MPSException("Cannot write reference to unserved, non-Proxy object"); 00210 } 00211 } else { 00212
00213
00214 writestring(svr.getBoundName()); 00215 } 00216 } 00217 00218 00219 00220 00221 00222 00223 00224 public void writefloat(float f) throws MPSException { 00225 writeint(Float.floatToIntBits(f)); 00226 } 00227 00228 00229 00230 00231 00232 00233 00234 00235 public void flush() throws MPSException { 00236 if (buffer.length() > 0) { 00237 try { 00238
00239 open(); 00240 output.write(buffer.toString().getBytes()); 00241 setupBuffer(); 00242 } catch (IOException ioe) { 00243 throw new MPSException("Error flushing OutputStream buffer"); 00244 } 00245 } 00246 } 00247 }