Changeset 48:a6d6f21b5d27 in rrlib_serialization-java


Ignore:
Timestamp:
14.07.2019 23:11:08 (5 weeks ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Tags:
tip
Message:

Adds throws clause to all read functions related to binary input streams - for cleaner/explicit exception handling in this regard

Files:
11 edited

Legend:

Unmodified
Added
Removed
  • BinaryInputStream.java

    r31 r48  
    229229     * @param s String 
    230230     */ 
    231     public String readUnicode() { 
     231    public String readUnicode() throws Exception { 
    232232        StringBuilder sb = new StringBuilder(); 
    233233        while (true) { 
     
    246246     * @param length Length of string to read (including possible termination) 
    247247     */ 
    248     public String readUnicode(int length) { 
     248    public String readUnicode(int length) throws Exception { 
    249249        StringBuilder sb = new StringBuilder(length); 
    250250        int count = 0; 
     
    263263     * Skips null-terminated string (16 Bit Characters) 
    264264     */ 
    265     public void skipUnicode() { 
     265    public void skipUnicode() throws Exception { 
    266266        while (true) { 
    267267            char c = readChar(); 
     
    277277     * @return String 
    278278     */ 
    279     public String readString() { 
     279    public String readString() throws Exception { 
    280280        StringBuilder sb = new StringBuilder(); // no shortcut in C++, since String could be in this chunk only partly 
    281281        readStringImpl(sb); 
     
    288288     * @param sb StringOutputStream object to write result to 
    289289     */ 
    290     public void readString(StringOutputStream sb) { 
     290    public void readString(StringOutputStream sb) throws Exception { 
    291291        sb.clear(); 
    292292        readStringImpl(sb.wrapped); 
     
    298298     * @param sb StringBuilder object to write result to 
    299299     */ 
    300     public void readString(StringBuilder sb) { 
     300    public void readString(StringBuilder sb) throws Exception { 
    301301        sb.delete(0, sb.length()); 
    302302        readStringImpl(sb); 
     
    308308     * @param sb StringBuilder object to write result to 
    309309     */ 
    310     public void readStringImpl(StringBuilder sb) { 
     310    public void readStringImpl(StringBuilder sb) throws Exception { 
    311311 
    312312        baos.reset(); 
     
    329329     * @return String 
    330330     */ 
    331     public String readString(int length) { 
     331    public String readString(int length) throws Exception { 
    332332        StringOutputStream sb = new StringOutputStream(); // no shortcut in C++, since String could be in this chunk only partly 
    333333        readString(sb, length); 
     
    341341     * @param length Length of string to read (including possible termination) 
    342342     */ 
    343     public void readString(StringOutputStream sb, int length) { 
     343    public void readString(StringOutputStream sb, int length) throws Exception { 
    344344        int count = 0; 
    345345        while (true) { 
     
    356356     * Skips null-terminated string (8 Bit Characters) 
    357357     */ 
    358     public void skipString() { 
     358    public void skipString() throws Exception { 
    359359        while (true) { 
    360360            byte c = readByte(); 
     
    368368     * Ensures that the specified number of bytes is available for reading 
    369369     */ 
    370     protected void ensureAvailable(int required) { 
     370    protected void ensureAvailable(int required) throws Exception { 
    371371        assert(!closed); 
    372372        int available = remaining(); 
     
    382382     * Fills buffer with contents from input stream 
    383383     * 
    384      * @param minRequired2 Minimum number of bytes to read (block otherwise) 
    385      */ 
    386     protected void fetchNextBytes(int minRequired2) { 
     384     * @param minRequired2 Minimum number of bytes to read (block or throw Exception otherwise) 
     385     */ 
     386    protected void fetchNextBytes(int minRequired2) throws Exception { 
    387387        assert(minRequired2 <= 8); 
    388388        assert(source != null); 
     
    442442     * @return boolean value (byte is read from stream and compared against zero) 
    443443     */ 
    444     public boolean readBoolean() { 
     444    public boolean readBoolean() throws Exception { 
    445445        return readByte() != 0; 
    446446    } 
     
    449449     * @return 8 bit integer 
    450450     */ 
    451     public byte readByte() { 
     451    public byte readByte() throws Exception { 
    452452        ensureAvailable(1); 
    453453        byte b = curBuffer.buffer.getByte(curBuffer.position); 
     
    459459     * @return Next byte - without forwarding read position though 
    460460     */ 
    461     public byte peek() { 
     461    public byte peek() throws Exception { 
    462462        ensureAvailable(1); 
    463463        byte b = curBuffer.buffer.getByte(curBuffer.position); 
     
    468468     * @return 16 bit character 
    469469     */ 
    470     public char readChar() { 
     470    public char readChar() throws Exception { 
    471471        ensureAvailable(2); 
    472472        char c = curBuffer.buffer.getChar(curBuffer.position); 
     
    478478     * @return 64 bit floating point 
    479479     */ 
    480     public double readDouble() { 
     480    public double readDouble() throws Exception { 
    481481        ensureAvailable(8); 
    482482        double d = curBuffer.buffer.getDouble(curBuffer.position); 
     
    488488     * @return 32 bit floating point 
    489489     */ 
    490     public float readFloat() { 
     490    public float readFloat() throws Exception { 
    491491        ensureAvailable(4); 
    492492        float f = curBuffer.buffer.getFloat(curBuffer.position); 
     
    500500     * @param b destination array 
    501501     */ 
    502     public void readFully(byte[] b) { 
     502    public void readFully(byte[] b) throws Exception { 
    503503        readFully(b, 0, b.length); 
    504504    } 
     
    511511     * @param len Number of bytes to read 
    512512     */ 
    513     public void readFully(byte[] b, int off, int len) { 
     513    public void readFully(byte[] b, int off, int len) throws Exception { 
    514514        while (true) { 
    515515            int read = Math.min(curBuffer.remaining(), len); 
     
    531531     * @param b destination buffer 
    532532     */ 
    533     public void readFully(FixedBuffer bb) { 
     533    public void readFully(FixedBuffer bb) throws Exception { 
    534534        readFully(bb, 0, bb.capacity()); 
    535535    } 
     
    542542     * @param len number of bytes to copy 
    543543     */ 
    544     public void readFully(FixedBuffer bb, int off, int len) { 
     544    public void readFully(FixedBuffer bb, int off, int len) throws Exception { 
    545545        while (true) { 
    546546            int read = Math.min(curBuffer.remaining(), len); 
     
    574574     * @return 32 bit integer 
    575575     */ 
    576     public int readInt() { 
     576    public int readInt() throws Exception { 
    577577        ensureAvailable(4); 
    578578        int i = curBuffer.buffer.getInt(curBuffer.position); 
     
    584584     * @return sizeOf Size of integer in byte (1, 2, 4, or 8) 
    585585     */ 
    586     public long readInt(int sizeOf) { 
     586    public long readInt(int sizeOf) throws Exception { 
    587587        switch (sizeOf) { 
    588588        case 1: 
     
    603603     * @return String/Line from stream (ends either at line delimiter or 0-character) 
    604604     */ 
    605     public String readLine() { 
     605    public String readLine() throws Exception { 
    606606        StringOutputStream sb = new StringOutputStream(); 
    607607        while (true) { 
     
    618618     * @return 8 byte integer 
    619619     */ 
    620     public long readLong() { 
     620    public long readLong() throws Exception { 
    621621        ensureAvailable(8); 
    622622        long l = curBuffer.buffer.getLong(curBuffer.position); 
     
    628628     * @return 2 byte integer 
    629629     */ 
    630     public short readShort() { 
     630    public short readShort() throws Exception { 
    631631        ensureAvailable(2); 
    632632        short s = curBuffer.buffer.getShort(curBuffer.position); 
     
    638638     * @return unsigned 1 byte integer 
    639639     */ 
    640     public int readUnsignedByte() { 
     640    public int readUnsignedByte() throws Exception { 
    641641        ensureAvailable(1); 
    642642        int i = curBuffer.buffer.getUnsignedByte(curBuffer.position); 
     
    648648     * @return unsigned 2 byte integer 
    649649     */ 
    650     public int readUnsignedShort() { 
     650    public int readUnsignedShort() throws Exception { 
    651651        ensureAvailable(2); 
    652652        int i = curBuffer.buffer.getUnsignedShort(curBuffer.position); 
     
    659659     */ 
    660660    @SuppressWarnings({ "rawtypes"}) 
    661     public <E extends Enum> E readEnum(Class<E> c) { 
     661    public <E extends Enum> E readEnum(Class<E> c) throws Exception { 
    662662        return c.getEnumConstants()[readEnum(c.getEnumConstants())]; 
    663663    } 
     
    666666     * @return Enum value 
    667667     */ 
    668     public int readEnum(Object[] strings) { 
     668    public int readEnum(Object[] strings) throws Exception { 
    669669        if (strings.length == 0) { 
    670670            return readInt(); 
     
    682682     * Skip specified number of bytes 
    683683     */ 
    684     public void skip(int n) { 
     684    public void skip(int n) throws Exception { 
    685685        /*      if (this.streamBuffer.source == null) { 
    686686                    readPos += n; 
     
    701701     * Read "skip offset" at current position and store it internally 
    702702     */ 
    703     public void readSkipOffset() { 
     703    public void readSkipOffset() throws Exception { 
    704704        curSkipOffsetTarget = absoluteReadPos + curBuffer.position; 
    705705        curSkipOffsetTarget += readInt(); 
     
    710710     * Move to target of last read skip offset 
    711711     */ 
    712     public void toSkipTarget() { 
     712    public void toSkipTarget() throws Exception { 
    713713        long pos = curBuffer.position; 
    714714        assert(curSkipOffsetTarget >= absoluteReadPos + pos); 
     
    785785     * @return Duration in milliseconds 
    786786     */ 
    787     public long readDuration() { 
     787    public long readDuration() throws Exception { 
    788788        return readLong() / 1000000; 
    789789    } 
  • BinaryOutputStream.java

    r33 r48  
    608608     * @param inputStream Input Stream 
    609609     */ 
    610     public void writeAllAvailable(BinaryInputStream inputStream) { 
     610    public void writeAllAvailable(BinaryInputStream inputStream) throws Exception { 
    611611        while (inputStream.moreDataAvailable()) { 
    612612            inputStream.ensureAvailable(1); 
  • EnumValue.java

    r36 r48  
    5252 
    5353    @Override 
    54     public void deserialize(BinaryInputStream is) { 
     54    public void deserialize(BinaryInputStream is) throws Exception { 
    5555        enumIndex = is.readEnum(type.getEnumConstants()); 
    5656    } 
  • InputStreamSource.java

    r0 r48  
    5151        try { 
    5252            wrapped.close(); 
    53             buffer.reset(); 
    54             state = State.CLOSED; 
    5553        } catch (IOException e) { 
    56             throw new RuntimeException(e); 
     54            Log.log(LogLevel.ERROR, this, "Error closing InputStream: ", e); 
    5755        } 
     56        buffer.reset(); 
     57        state = State.CLOSED; 
    5858    } 
    5959 
    6060    @Override 
    61     public void directRead(BinaryInputStream inputStreamBuffer, FixedBuffer buffer, int offset, int len) { 
    62         try { 
    63             byte[] tmp = new byte[MemoryBuffer.TEMP_ARRAY_SIZE]; 
    64             while (len > 0) { 
    65                 int r = wrapped.read(tmp, 0, Math.min(len, tmp.length)); 
    66                 if (r < 0) { 
    67                     throw new EOFException(); 
    68                 } 
    69                 buffer.put(offset, tmp, 0, r); 
    70                 offset += r; 
    71                 len -= r; 
     61    public void directRead(BinaryInputStream inputStreamBuffer, FixedBuffer buffer, int offset, int len) throws Exception { 
     62        byte[] tmp = new byte[MemoryBuffer.TEMP_ARRAY_SIZE]; 
     63        while (len > 0) { 
     64            int r = wrapped.read(tmp, 0, Math.min(len, tmp.length)); 
     65            if (r < 0) { 
     66                throw new EOFException(); 
    7267            } 
    73         } catch (IOException e) { 
    74             throw new RuntimeException(e); 
     68            buffer.put(offset, tmp, 0, r); 
     69            offset += r; 
     70            len -= r; 
    7571        } 
    7672    } 
     
    8278 
    8379    @Override 
    84     public void read(BinaryInputStream inputStreamBuffer, BufferInfo buffer, int len) { 
     80    public void read(BinaryInputStream inputStreamBuffer, BufferInfo buffer, int len) throws Exception { 
    8581        assert(len < buffer.buffer.capacity()); 
    86         try { 
    87             int offset = 0; 
    88             int av = wrapped.available(); 
    89             int read = Math.min(av, buffer.buffer.capacity() - offset); 
    90             read = Math.max(read, len); 
    91             byte[] tmp = new byte[2048]; 
    92             while (read > 0 || len > 0) { 
    93                 int r = wrapped.read(tmp, 0, Math.min(read, tmp.length)); 
    94                 if (r < 0) { 
    95                     throw new EOFException(); 
    96                 } 
    97                 buffer.buffer.put(offset, tmp, 0, r); 
    98                 offset += r; 
    99                 read -= r; 
    100                 len -= r; 
     82        int offset = 0; 
     83        int av = wrapped.available(); 
     84        int read = Math.min(av, buffer.buffer.capacity() - offset); 
     85        read = Math.max(read, len); 
     86        byte[] tmp = new byte[2048]; 
     87        while (read > 0 || len > 0) { 
     88            int r = wrapped.read(tmp, 0, Math.min(read, tmp.length)); 
     89            if (r < 0) { 
     90                throw new EOFException(); 
    10191            } 
    102             buffer.setRange(0, offset); 
    103             buffer.position = 0; 
    104         } catch (IOException e) { 
    105             throw new RuntimeException(e); 
     92            buffer.buffer.put(offset, tmp, 0, r); 
     93            offset += r; 
     94            read -= r; 
     95            len -= r; 
    10696        } 
     97        buffer.setRange(0, offset); 
     98        buffer.position = 0; 
    10799    } 
    108100 
  • MemoryBuffer.java

    r39 r48  
    249249 
    250250    @Override 
    251     public void deserialize(BinaryInputStream rv) { 
     251    public void deserialize(BinaryInputStream rv) throws Exception { 
    252252        long size = rv.readLong(); 
    253253        deserialize(rv, (int)size); 
     
    333333     * @param size Number of bytes to 
    334334     */ 
    335     public void deserialize(BinaryInputStream rv, int size) { 
     335    public void deserialize(BinaryInputStream rv, int size) throws Exception { 
    336336        curSize = 0; 
    337337        reallocate(size, false, -1); 
  • Register.java

    r26 r48  
    142142     * @throw Throws exception if read handle is out of bounds 
    143143     */ 
    144     public Entry readEntry(BinaryInputStream stream) { 
     144    public Entry readEntry(BinaryInputStream stream) throws Exception { 
    145145        SerializationInfo.RegisterEntryEncoding encoding = stream.getSourceInfo().getRegisterEntryEncoding(uid); 
    146146        if (encoding == SerializationInfo.RegisterEntryEncoding.UID) { 
     
    149149        int handle = (int)stream.readInt(sizeOfHandle); 
    150150        if (handle >= size()) { 
    151             throw new RuntimeException("Register.ReadEntry: handle is out of bounds"); 
     151            throw new Exception("Register.ReadEntry: handle is out of bounds"); 
    152152        } 
    153153        return get(handle); 
  • Serialization.java

    r31 r48  
    134134        cs.serialize(co); 
    135135        co.close(); 
    136         BinaryInputStream ci = new BinaryInputStream(cb); 
    137         convertBinaryToHexString(ci, os); 
    138         ci.close(); 
     136        try { 
     137            BinaryInputStream ci = new BinaryInputStream(cb); 
     138            convertBinaryToHexString(ci, os); 
     139            ci.close(); 
     140        } catch (Exception e) { 
     141            Log.log(LogLevel.ERROR, cs, "Error converting to hex string: ", e); 
     142        } 
    139143    } 
    140144 
     
    145149     * @param co Output stream to write hex string to 
    146150     */ 
    147     public static void convertBinaryToHexString(BinaryInputStream src, StringOutputStream os) { 
     151    public static void convertBinaryToHexString(BinaryInputStream src, StringOutputStream os) throws Exception { 
    148152        while (src.moreDataAvailable()) { 
    149153            byte b = src.readByte(); 
  • Source.java

    r0 r48  
    5757     * @param len Minimum number of bytes to read 
    5858     */ 
    59     public void read(BinaryInputStream inputStreamBuffer, BufferInfo buffer, int len); 
     59    public void read(BinaryInputStream inputStreamBuffer, BufferInfo buffer, int len) throws Exception; 
    6060 
    6161    /** 
     
    7373     * @param len Exact number of bytes to read 
    7474     */ 
    75     public void directRead(BinaryInputStream inputStreamBuffer, FixedBuffer buffer, int offset, int len); 
     75    public void directRead(BinaryInputStream inputStreamBuffer, FixedBuffer buffer, int offset, int len) throws Exception; 
    7676 
    7777    /** 
  • rtti/datatype/BoolList.java

    r38 r48  
    6565 
    6666    @Override 
    67     public void deserialize(BinaryInputStream is) { 
     67    public void deserialize(BinaryInputStream is) throws Exception { 
    6868        size = is.readInt(); 
    6969        if (size > buffer.length) { 
  • rtti/datatype/PrimitiveTypeList.java

    r38 r48  
    6969 
    7070    @Override 
    71     public void deserialize(BinaryInputStream is) { 
     71    public void deserialize(BinaryInputStream is) throws Exception { 
    7272        int size = is.readInt(); 
    7373        boolean constType = is.readBoolean(); 
     
    8383     * @param arraySize Number of entries (in array type) 
    8484     */ 
    85     public void deserializeArray(BinaryInputStream stream, int arraySize) { 
     85    public void deserializeArray(BinaryInputStream stream, int arraySize) throws Exception { 
    8686        setSize(arraySize); 
    8787        stream.readFully(super.getBuffer(), 0, arraySize * typeSize); 
  • rtti/datatype/StringList.java

    r42 r48  
    2424import java.util.ArrayList; 
    2525import java.util.Arrays; 
     26import java.util.Iterator; 
    2627import java.util.List; 
    27 import java.util.Iterator; 
    2828 
    2929import org.rrlib.serialization.BinaryInputStream; 
     
    147147 
    148148    @Override 
    149     public void deserialize(BinaryInputStream is) { 
     149    public void deserialize(BinaryInputStream is) throws Exception { 
    150150        int size = is.readInt(); 
    151151        boolean constType = is.readBoolean(); 
Note: See TracChangeset for help on using the changeset viewer.