coherence/io/AbstractReadBuffer.hpp

00001 /*
00002 * AbstractReadBuffer.hpp
00003 *
00004 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_ABSTRACT_READ_BUFFER_HPP
00017 #define COH_ABSTRACT_READ_BUFFER_HPP
00018 
00019 #include "coherence/lang.ns"
00020 
00021 #include "coherence/io/ReadBuffer.hpp"
00022 
00023 COH_OPEN_NAMESPACE2(coherence,io)
00024 
00025 
00026 /**
00027 * Abstract base implementation of the ReadBuffer interface.
00028 *
00029 * @author jh  2007.12.31
00030 */
00031 class COH_EXPORT AbstractReadBuffer
00032     : public abstract_spec<AbstractReadBuffer,
00033         extends<Object>,
00034         implements<ReadBuffer> >
00035     {
00036     // ----- handle definitions (needed for nested classes) -----------------
00037 
00038     public:
00039         typedef this_spec::Handle Handle;
00040         typedef this_spec::View   View;
00041         typedef this_spec::Holder Holder;
00042 
00043 
00044     // ----- ReadBuffer interface -------------------------------------------
00045 
00046     public:
00047         /**
00048         * {@inheritDoc}
00049         */
00050         virtual BufferInput::Handle getBufferInput() const;
00051 
00052         /**
00053         * {@inheritDoc}
00054         */
00055         virtual ReadBuffer::View getReadBuffer(size32_t of, size32_t cb) const;
00056 
00057         /**
00058         * {@inheritDoc}
00059         */
00060         virtual Array<octet_t>::View toOctetArray() const;
00061 
00062         /**
00063         * {@inheritDoc}
00064         */
00065         virtual Array<octet_t>::View toOctetArray(size32_t of, size32_t cb) const;
00066 
00067 
00068     // ----- Object interface -----------------------------------------------
00069 
00070     public:
00071         /**
00072         * {@inheritDoc}
00073         */
00074         virtual bool equals(Object::View v) const;
00075 
00076 
00077     // ----- internal methods -----------------------------------------------
00078 
00079     protected:
00080         /**
00081         * Factory method: Instantiate a ReadBuffer for a portion of this
00082         * ReadBuffer.
00083         *
00084         * @param of  the beginning index, inclusive
00085         * @param cb  the number of bytes to include in the resulting
00086         *            ReadBuffer
00087         *
00088         * @return a ReadBuffer that represents a portion of this ReadBuffer
00089         */
00090         virtual ReadBuffer::View instantiateReadBuffer(size32_t of,
00091                 size32_t cb) const = 0;
00092 
00093         /**
00094         * Factory method: Instantiate a BufferInput object to read data from
00095         * the ReadBuffer.
00096         *
00097         * @return a new BufferInput reading from this ReadBuffer
00098         */
00099         virtual BufferInput::Handle instantiateBufferInput() const;
00100 
00101         /**
00102         * Check if the specified read is within bounds.
00103         *
00104         * @param of  the absolute offset of the read operation
00105         * @param cb  the length in bytes of the read operation
00106         *
00107         * @throws IndexOutOfBoundsException if the specified read is not
00108         *         within bounds
00109         */
00110         virtual void checkBounds(size32_t of, size32_t cb) const;
00111 
00112 
00113     // ----- AbstractBufferInput inner class --------------------------------
00114 
00115     public:
00116         /**
00117         * This is an implementation of the BufferInput interface that
00118         * delegates its work back to its ReadBuffer.
00119         *
00120         * @author jh  2007.12.31
00121         */
00122         class COH_EXPORT AbstractBufferInput
00123             : public class_spec<AbstractBufferInput,
00124                 extends<Object>,
00125                 implements<BufferInput> >
00126             {
00127             friend class factory<AbstractBufferInput>;
00128 
00129             // ----- constructors ---------------------------------------
00130 
00131             protected:
00132                 /**
00133                 * Create a new AbstractBufferInput instance that delegates
00134                 * to the given AbstractReadBuffer.
00135                 *
00136                 * @param vBuf  the delegate AbstractReadBuffer
00137                 */
00138                 AbstractBufferInput(AbstractReadBuffer::View vBuf);
00139 
00140 
00141             // ----- BufferInput interface ------------------------------
00142 
00143             public:
00144                 /**
00145                 * {@inheritDoc}
00146                 */
00147                 virtual ReadBuffer::View getBuffer() const;
00148 
00149                 /**
00150                 * {@inheritDoc}
00151                 */
00152                 virtual size32_t getOffset() const;
00153 
00154                 /**
00155                 * {@inheritDoc}
00156                 */
00157                 virtual void setOffset(size32_t of);
00158 
00159                 /**
00160                 * {@inheritDoc}
00161                 */
00162                 virtual size32_t available() const;
00163 
00164                 /**
00165                 * {@inheritDoc}
00166                 */
00167                 virtual void skip(size32_t cb);
00168 
00169                 /**
00170                 * {@inheritDoc}
00171                 */
00172                 virtual octet_t read();
00173 
00174                 /**
00175                 * {@inheritDoc}
00176                 */
00177                 virtual void read(Array<octet_t>::Handle hab);
00178 
00179                 /**
00180                 * {@inheritDoc}
00181                 */
00182                 virtual void read(Array<octet_t>::Handle hab, size32_t of,
00183                         size32_t cb);
00184 
00185                 /**
00186                 * {@inheritDoc}
00187                 */
00188                 virtual ReadBuffer::View readBuffer(size32_t cb);
00189 
00190                 /**
00191                 * {@inheritDoc}
00192                 */
00193                 virtual bool readBoolean();
00194 
00195                 /**
00196                 * {@inheritDoc}
00197                 */
00198                 virtual char16_t readChar16();
00199 
00200                 /**
00201                 * {@inheritDoc}
00202                 */
00203                 virtual String::View readString();
00204 
00205                 /**
00206                 * {@inheritDoc}
00207                 */
00208                 virtual int16_t readInt16();
00209 
00210                 /**
00211                 * {@inheritDoc}
00212                 */
00213                 virtual int32_t readInt32();
00214 
00215                 /**
00216                 * {@inheritDoc}
00217                 */
00218                 virtual int64_t readInt64();
00219 
00220                 /**
00221                 * {@inheritDoc}
00222                 */
00223                 virtual float32_t readFloat32();
00224 
00225                 /**
00226                 * {@inheritDoc}
00227                 */
00228                 virtual float64_t readFloat64();
00229 
00230             // ----- internal methods -----------------------------------
00231 
00232             protected:
00233                 /**
00234                 * Update the internal stream offset.
00235                 *
00236                 * @param of  the new offset
00237                 */
00238                 virtual void setOffsetInternal(size32_t of);
00239 
00240                 /**
00241                 * Reads the specified-length UTF-8 data and converts it to a
00242                 * String value.
00243                 *
00244                 * @param cb  the number of bytes that the UTF data takes up
00245                 *            in the stream
00246                 *
00247                 * @return a String value
00248                 *
00249                 * @throws UTFDataFormatException if the bytes that were
00250                 *         read were not a valid UTF-8 encoded string
00251                 * @throws EOFException if the value could not be read because
00252                 *         no more data remains to be read
00253                 * @throws IOException if an I/O error occurs
00254                 */
00255                 virtual String::View readUTF(size32_t cb);
00256 
00257                 /**
00258                 * Convert a UTF-8 encoded section of the binary stream into a
00259                 * String.
00260                 *
00261                 * @param of  the offset within the stream
00262                 * @param cb  the length in bytes within the stream
00263                 *
00264                 * @return the String value
00265                 *
00266                 * @throws IOException on convertion error
00267                 */
00268                 virtual String::View convertUTF(size32_t of, size32_t cb) const;
00269 
00270             // ----- data members ---------------------------------------
00271 
00272             protected:
00273                 /**
00274                 * The delegate AbstractReadBuffer.
00275                 */
00276                 FinalView<AbstractReadBuffer> m_vBuf;
00277 
00278                 /**
00279                 * Current stream offset into the delegate AbstractReadBuffer.
00280                 */
00281                 size32_t m_of;
00282             };
00283 
00284 
00285     // ----- constants ------------------------------------------------------
00286 
00287     public:
00288         /**
00289         * An empty octet array (by definition immutable).
00290         */
00291         static Array<octet_t>::Handle getEmptyOctetArray();
00292 
00293         /**
00294         * A zero-length ReadBuffer (by definition immutable).
00295         */
00296         static ReadBuffer::Handle getEmptyReadBuffer();
00297     };
00298 
00299 COH_CLOSE_NAMESPACE2
00300 
00301 #endif // COH_ABSTRACT_READ_BUFFER_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.