Changeset 50:e92cccfd60c9 in rrlib_serialization-java


Ignore:
Timestamp:
15.11.2019 07:15:24 (7 months ago)
Author:
Max Reichardt <max.reichardt@…>
Branch:
default
Phase:
public
Message:

Adds 'throws' clause to BinarySerializable interfaces' serialize function (as there are cases where serialization can fail)

Files:
9 edited

Legend:

Unmodified
Added
Removed
  • BinaryOutputStream.java

    r48 r50  
    622622     * @param type Type of object (if serialization is consistent, could be base class) 
    623623     */ 
    624     public void writeObject(Object object) { 
     624    public void writeObject(Object object) throws Exception { 
    625625        writeObject(object, object.getClass()); 
    626626    } 
     
    633633     */ 
    634634    @SuppressWarnings("rawtypes") 
    635     public void writeObject(Object object, Class<?> type) { 
     635    public void writeObject(Object object, Class<?> type) throws Exception { 
    636636        if (BinarySerializable.class.isAssignableFrom(type)) { 
    637637            ((BinarySerializable)object).serialize(this); 
     
    677677     * @param encoding Desired data encoding 
    678678     */ 
    679     public void writeObject(Object object, Class<?> type, Serialization.DataEncoding encoding) { 
     679    public void writeObject(Object object, Class<?> type, Serialization.DataEncoding encoding) throws Exception { 
    680680        if (encoding == Serialization.DataEncoding.BINARY) { 
    681681            writeObject(object, type); 
     
    735735     * @return Whether any register updates have been written to stream 
    736736     */ 
    737     public boolean writeRegisterUpdates(int registerUid, int entryHandle, int handleSize) { 
     737    public boolean writeRegisterUpdates(int registerUid, int entryHandle, int handleSize) throws Exception { 
    738738        int newCounter = 0; 
    739739        if (publishedRegisterStatus.onChangeRegisters != null) { 
  • BinarySerializable.java

    r3 r50  
    4040     * @param stream Binary stream to serialize to 
    4141     */ 
    42     public void serialize(BinaryOutputStream stream); 
     42    public void serialize(BinaryOutputStream stream) throws Exception; 
    4343 
    4444    /** 
  • PortDataListImpl.java

    r26 r50  
    5151 
    5252    @Override 
    53     public void serialize(BinaryOutputStream os) { 
     53    public void serialize(BinaryOutputStream os) throws Exception { 
    5454        os.writeInt(wrapped.size()); 
    5555        os.writeBoolean(true); // const type?  (possibly unnecessary; if we remove it, this will break binary compatibility to 13.10 though) 
  • PublishedRegisters.java

    r29 r50  
    3939         * @param entry Local entry to serialize 
    4040         */ 
    41         public abstract void serializeLocalRegisterEntry(BinaryOutputStream stream, Object entry); 
     41        public abstract void serializeLocalRegisterEntry(BinaryOutputStream stream, Object entry) throws Exception; 
    4242 
    4343        /** 
     
    208208         * @param endElement One past the last element to serialize 
    209209         */ 
    210         void serializeEntries(BinaryOutputStream stream, int startElement, int endElement) { 
     210        void serializeEntries(BinaryOutputStream stream, int startElement, int endElement) throws Exception { 
    211211            for (int i = startElement; i < endElement; i++) { 
    212212                serializer.serializeLocalRegisterEntry(stream, register.get(i)); 
  • Register.java

    r48 r50  
    185185     * @return Returns true if 'UID' encoding has been specified in stream. In this case, the caller must serialize the entry's uid. 
    186186     */ 
    187     public boolean writeEntry(BinaryOutputStream stream, int handle) { 
     187    public boolean writeEntry(BinaryOutputStream stream, int handle) throws Exception { 
    188188        SerializationInfo.RegisterEntryEncoding encoding = stream.getTargetInfo().getRegisterEntryEncoding(uid); 
    189189        if (encoding == SerializationInfo.RegisterEntryEncoding.UID) { 
     
    204204     * @return Returns true if 'UID' encoding has been specified in stream. In this case, the caller must serialize the entry's uid. 
    205205     */ 
    206     public boolean writeLastEntry(BinaryOutputStream stream) { 
     206    public boolean writeLastEntry(BinaryOutputStream stream) throws Exception { 
    207207        return writeEntry(stream, this.size() - 1); 
    208208    } 
  • RegisterUpdate.java

    r26 r50  
    5252 
    5353    @Override 
    54     public void serialize(BinaryOutputStream stream) { 
     54    public void serialize(BinaryOutputStream stream) throws Exception { 
    5555        if (!stream.writeRegisterUpdates(registerUid, Integer.MAX_VALUE, 0)) { 
    5656            stream.writeByte(-1); 
  • Serialization.java

    r48 r50  
    129129     * @param os String output stream 
    130130     */ 
    131     public static void serializeToHexString(BinarySerializable cs, StringOutputStream os) { 
     131    public static void serializeToHexString(BinarySerializable cs, StringOutputStream os) throws Exception { 
    132132        MemoryBuffer cb = new MemoryBuffer(); 
    133133        BinaryOutputStream co = new BinaryOutputStream(cb); 
     
    411411     */ 
    412412    private static <T extends BinarySerializable> void deepCopyImpl(T src, T dest, Factory f, MemoryBuffer buf) { 
    413         buf.clear(); 
    414         BinaryOutputStream os = new BinaryOutputStream(buf); 
    415         src.serialize(os); 
    416  
    417         os.close(); 
    418         BinaryInputStream ci = new BinaryInputStream(buf); 
    419         //ci.setFactory(f); 
    420  
    421413        try { 
     414            buf.clear(); 
     415            BinaryOutputStream os = new BinaryOutputStream(buf); 
     416            src.serialize(os); 
     417 
     418            os.close(); 
     419            BinaryInputStream ci = new BinaryInputStream(buf); 
    422420            dest.deserialize(ci); 
     421            ci.close(); 
    423422        } catch (Exception e) { 
    424423            Log.log(LogLevel.ERROR, e); // If this happens, serialization of objects of type T is not implemented correctly 
    425424        } 
    426         ci.close(); 
    427425    } 
    428426 
     
    436434     */ 
    437435    public static boolean equals(Object obj1, Object obj2) { 
    438         if (obj1 == obj2) { 
    439             return true; 
    440         } 
    441         if (obj1 == null || obj2 == null || obj1.getClass() != obj2.getClass()) { 
    442             return false; 
    443         } 
    444         if (obj1 instanceof GenericObject) { 
    445             return equals(((GenericObject)obj1).getData(), ((GenericObject)obj2).getData()); 
    446         } 
    447         if (obj1 instanceof BinarySerializable) { 
    448             MemoryBuffer buf1 = new MemoryBuffer(); 
    449             MemoryBuffer buf2 = new MemoryBuffer(); 
    450             BinaryOutputStream os1 = new BinaryOutputStream(buf1); 
    451             BinaryOutputStream os2 = new BinaryOutputStream(buf2); 
    452             os1.writeObject(obj1); 
    453             os2.writeObject(obj2); 
    454             os1.close(); 
    455             os2.close(); 
    456             return buf1.equals(buf2); 
    457         } else if (obj1 instanceof Serializable) { 
    458             return Arrays.equals(toByteArray((Serializable)obj1), toByteArray((Serializable)obj2)); 
    459         } else if (obj1 instanceof StringSerializable) { 
    460             return serialize(obj1).equals(serialize(obj2)); 
    461         } else if (obj1 instanceof XMLSerializable) { 
    462             MemoryBuffer buf1 = new MemoryBuffer(); 
    463             MemoryBuffer buf2 = new MemoryBuffer(); 
    464             BinaryOutputStream os1 = new BinaryOutputStream(buf1); 
    465             BinaryOutputStream os2 = new BinaryOutputStream(buf2); 
    466             os1.writeObject(obj1, obj1.getClass(), DataEncoding.XML); 
    467             os2.writeObject(obj2, obj2.getClass(), DataEncoding.XML); 
    468             os1.close(); 
    469             os2.close(); 
    470             return buf1.equals(buf2); 
    471         } else { 
    472             throw new RuntimeException("Objects of type " + obj1.getClass().getName() + " cannot be serialized and compared"); 
    473         } 
     436        try { 
     437            if (obj1 == obj2) { 
     438                return true; 
     439            } 
     440            if (obj1 == null || obj2 == null || obj1.getClass() != obj2.getClass()) { 
     441                return false; 
     442            } 
     443            if (obj1 instanceof GenericObject) { 
     444                return equals(((GenericObject)obj1).getData(), ((GenericObject)obj2).getData()); 
     445            } 
     446            if (obj1 instanceof BinarySerializable) { 
     447                MemoryBuffer buf1 = new MemoryBuffer(); 
     448                MemoryBuffer buf2 = new MemoryBuffer(); 
     449                BinaryOutputStream os1 = new BinaryOutputStream(buf1); 
     450                BinaryOutputStream os2 = new BinaryOutputStream(buf2); 
     451                os1.writeObject(obj1); 
     452                os2.writeObject(obj2); 
     453                os1.close(); 
     454                os2.close(); 
     455                return buf1.equals(buf2); 
     456            } else if (obj1 instanceof Serializable) { 
     457                return Arrays.equals(toByteArray((Serializable)obj1), toByteArray((Serializable)obj2)); 
     458            } else if (obj1 instanceof StringSerializable) { 
     459                return serialize(obj1).equals(serialize(obj2)); 
     460            } else if (obj1 instanceof XMLSerializable) { 
     461                MemoryBuffer buf1 = new MemoryBuffer(); 
     462                MemoryBuffer buf2 = new MemoryBuffer(); 
     463                BinaryOutputStream os1 = new BinaryOutputStream(buf1); 
     464                BinaryOutputStream os2 = new BinaryOutputStream(buf2); 
     465                os1.writeObject(obj1, obj1.getClass(), DataEncoding.XML); 
     466                os2.writeObject(obj2, obj2.getClass(), DataEncoding.XML); 
     467                os1.close(); 
     468                os2.close(); 
     469                return buf1.equals(buf2); 
     470            } 
     471        } catch (Exception e) { 
     472        } 
     473        throw new RuntimeException("Objects of type " + obj1.getClass().getName() + " cannot be serialized and compared"); 
    474474    } 
    475475 
  • rtti/DataTypeBase.java

    r46 r50  
    407407     * @param stream Stream to serialize this type to 
    408408     */ 
    409     public void serialize(BinaryOutputStream stream) { 
     409    public void serialize(BinaryOutputStream stream) throws Exception { 
    410410        if (types.writeEntry(stream, getUid())) { 
    411411            stream.writeString(getName()); 
  • rtti/GenericObject.java

    r3 r50  
    112112     * @param enc Encoding to use 
    113113     */ 
    114     public void serialize(BinaryOutputStream stream, Serialization.DataEncoding enc) { 
     114    public void serialize(BinaryOutputStream stream, Serialization.DataEncoding enc) throws Exception { 
    115115        stream.writeObject(getData(), type.getJavaClass(), enc); 
    116116    } 
Note: See TracChangeset for help on using the changeset viewer.