Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

InputStream.java

Go to the documentation of this file.
00001 /**************************************************************************
00002  Copyright (c) 2000-2001, Tony Garnock-Jones
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without
00006  modification, are permitted provided that the following conditions are
00007  met:
00008 
00009      * Redistributions of source code must retain the above copyright
00010        notice, this list of conditions and the following disclaimer.
00011 
00012      * Redistributions in binary form must reproduce the above
00013        copyright notice, this list of conditions and the following
00014        disclaimer in the documentation and/or other materials provided
00015        with the distribution.
00016 
00017      * Neither the names of the copyright holders nor the names of this
00018        software's contributors may be used to endorse or promote
00019        products derived from this software without specific prior
00020        written permission.
00021 
00022  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00026  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 **************************************************************************/
00034 
00035 package org.hebe.mps;
00036 
00037 import java.io.*;
00038 import java.net.*;
00039 
00040 /**
00041  * Structures the bitstream coming from an MPS server into usable MPS
00042  * primitive types (mapped onto Java types, of course). Uses a
00043  * Connection object to talk to the other end.
00044  *
00045  * @author Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
00046  * @see org.hebe.mps.Connection
00047  * @see org.hebe.mps.OutputStream
00048  */
00049 public class InputStream {
00050     private Connection connection;
00051     private java.io.InputStream input;
00052 
00053     /**
00054      * Construct an InputStream which decodes the bits coming from a
00055      * Connection object.
00056      *
00057      * @param c the raw connection to filter
00058      */
00059     public InputStream(Connection c) {
00060         connection = c;
00061         input = null;
00062     }
00063 
00064     /**
00065      * Construct an InputStream which decodes bits from an arbitrary
00066      * inputstream.
00067      *
00068      * @param i the raw inputstream to filter
00069      */
00070     public InputStream(java.io.InputStream i) {
00071         connection = null;
00072         input = i;
00073     }
00074 
00075     /**
00076      * Reads a single byte from the server.
00077      *
00078      * @return the byte read
00079      * @exception IOException if there's an error reading our input
00080      * @exception MPSConnectionClosedException if the connection closes unexpectedly
00081      */
00082     private int read() throws IOException, MPSException {
00083         int result = input.read();
00084         if (result < 0)
00085             throw new MPSConnectionClosedException("while reading");
00086         //System.out.println("Read: " + result);
00087         return result;
00088     }
00089 
00090     /**
00091      * Causes the connection to the server to open, if it isn't
00092      * already open.
00093      *
00094      * @exception MPSException from connection.getInputStream() if there's a problem
00095      * @see org.hebe.mps.Connection#getInputStream
00096      */
00097     private void open() throws MPSException {
00098         if (input == null)
00099             input = connection.getInputStream();
00100     }
00101 
00102     /**
00103      * Reads a single <i>int</i> from the server.
00104      *
00105      * @return the 32-bit signed value read from our input
00106      * @exception MPSException if there's a problem reading
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      * Reads a single <i>java.lang.String</i> from the server.
00124      *
00125      * @return the string read
00126      * @see java.lang.String
00127      * @exception MPSException if there's a problem reading
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      * Reads a single <i>boolean</i> from the server.
00152      *
00153      * @return the boolean read
00154      * @exception MPSException if there's a problem reading
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      * Reads a single 64-bit <i>long</i> from the server.
00167      *
00168      * @return the long read
00169      * @exception MPSException if there's a problem reading
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      * Reads a single <i>char</i> from the server.
00191      *
00192      * @return the char read
00193      * @exception MPSException if there's a problem reading
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      * Reads the canonical (bound) name of an MPS object from the
00206      * remote end of the link.
00207      *
00208      * @return the canonical name of the object read from the server
00209      * @exception MPSException if there's a problem reading
00210      * @see org.hebe.mps.OutputStream#writeReference
00211      */
00212     public String readReference() throws MPSException {
00213         return readstring();
00214     }
00215 
00216     /**
00217      * Reads a single <i>float</i> from the server.
00218      *
00219      * @return the float read
00220      * @exception MPSException if there's a problem reading
00221      */
00222     public float readfloat() throws MPSException {
00223         open();
00224         return Float.intBitsToFloat(readint());
00225     }
00226 }

Generated at Wed Aug 15 01:05:34 2001 for mps-java by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001