diff options
author | Benoit Foucher <benoit@zeroc.com> | 2009-02-09 19:20:38 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2009-02-09 19:20:38 +0100 |
commit | 124b43c7bd718655556bd3cba2a554293bf63d16 (patch) | |
tree | 5c3138e3c96e9f3a4763e1b6eea92018d892b241 /java/src | |
parent | Bug 3519 - fix use of tolower in slice2freeze (diff) | |
download | ice-124b43c7bd718655556bd3cba2a554293bf63d16.tar.bz2 ice-124b43c7bd718655556bd3cba2a554293bf63d16.tar.xz ice-124b43c7bd718655556bd3cba2a554293bf63d16.zip |
Support for serializable and protobuf metadata (from cs_serial branch)
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/Ice/InputStream.java | 2 | ||||
-rw-r--r-- | java/src/Ice/InputStreamI.java | 6 | ||||
-rw-r--r-- | java/src/Ice/OutputStream.java | 2 | ||||
-rw-r--r-- | java/src/Ice/OutputStreamI.java | 6 | ||||
-rw-r--r-- | java/src/IceInternal/BasicStream.java | 45 | ||||
-rw-r--r-- | java/src/IceInternal/InputStreamWrapper.java | 94 | ||||
-rw-r--r-- | java/src/IceInternal/OutputStreamWrapper.java | 173 |
7 files changed, 327 insertions, 1 deletions
diff --git a/java/src/Ice/InputStream.java b/java/src/Ice/InputStream.java index da108992b7b..9028cad551d 100644 --- a/java/src/Ice/InputStream.java +++ b/java/src/Ice/InputStream.java @@ -21,6 +21,8 @@ public interface InputStream byte readByte(); byte[] readByteSeq(); + java.io.Serializable readSerializable(); + short readShort(); short[] readShortSeq(); diff --git a/java/src/Ice/InputStreamI.java b/java/src/Ice/InputStreamI.java index a5f970e54d4..9ee2c868b4a 100644 --- a/java/src/Ice/InputStreamI.java +++ b/java/src/Ice/InputStreamI.java @@ -61,6 +61,12 @@ public class InputStreamI implements InputStream return _is.readByteSeq(); } + public java.io.Serializable + readSerializable() + { + return _is.readSerializable(); + } + public short readShort() { diff --git a/java/src/Ice/OutputStream.java b/java/src/Ice/OutputStream.java index d481187c89d..4105fbc94b6 100644 --- a/java/src/Ice/OutputStream.java +++ b/java/src/Ice/OutputStream.java @@ -19,6 +19,8 @@ public interface OutputStream void writeByte(byte v); void writeByteSeq(byte[] v); + void writeSerializable(java.io.Serializable o); + void writeShort(short v); void writeShortSeq(short[] v); diff --git a/java/src/Ice/OutputStreamI.java b/java/src/Ice/OutputStreamI.java index 39130ee350a..934f8047e85 100644 --- a/java/src/Ice/OutputStreamI.java +++ b/java/src/Ice/OutputStreamI.java @@ -56,6 +56,12 @@ public class OutputStreamI implements OutputStream } public void + writeSerializable(java.io.Serializable v) + { + _os.writeSerializable(v); + } + + public void writeShort(short v) { _os.writeShort(v); diff --git a/java/src/IceInternal/BasicStream.java b/java/src/IceInternal/BasicStream.java index 10c9b8a25f7..659995acc2a 100644 --- a/java/src/IceInternal/BasicStream.java +++ b/java/src/IceInternal/BasicStream.java @@ -692,6 +692,28 @@ public class BasicStream } } + public void + writeSerializable(java.io.Serializable o) + { + if(o == null) + { + writeSize(0); + return; + } + try + { + OutputStreamWrapper w = new OutputStreamWrapper(this); + java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(w); + out.writeObject(o); + out.close(); + w.close(); + } + catch(java.lang.Exception ex) + { + throw new Ice.MarshalException("cannot serialize object: " + ex); + } + } + public byte readByte() { @@ -733,6 +755,27 @@ public class BasicStream } } + public java.io.Serializable + readSerializable() + { + int sz = readSize(); + if (sz == 0) + { + return null; + } + checkFixedSeq(sz, 1); + try + { + InputStreamWrapper w = new InputStreamWrapper(sz, this); + java.io.ObjectInputStream in = new java.io.ObjectInputStream(w); + return (java.io.Serializable)in.readObject(); + } + catch(java.lang.Exception ex) + { + throw new Ice.MarshalException("cannot deserialize object: " + ex); + } + } + public void writeBool(boolean v) { @@ -2056,7 +2099,7 @@ public class BasicStream return ucStream; } - private void + public void expand(int n) { if(!_unlimited && _buf.b != null && _buf.b.position() + n > _messageSizeMax) diff --git a/java/src/IceInternal/InputStreamWrapper.java b/java/src/IceInternal/InputStreamWrapper.java new file mode 100644 index 00000000000..aa12dda716c --- /dev/null +++ b/java/src/IceInternal/InputStreamWrapper.java @@ -0,0 +1,94 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +package IceInternal; + +import java.io.*; + +// +// Class to provide a java.io.InputStream on top of a BasicStream. +// We use this to deserialize arbitrary Java serializable classes from +// a Slice byte sequence. This class is a wrapper around a BasicStream +// that passes all methods through. +// + +public class InputStreamWrapper extends java.io.InputStream +{ + public + InputStreamWrapper(int size, BasicStream s) + { + _s = s; + _markPos = 0; + } + + public int + read() throws IOException + { + try + { + return _s.getBuffer().b.get(); + } + catch(java.lang.Exception ex) + { + throw new IOException(ex.toString()); + } + } + + public int + read(byte[] b) throws IOException + { + return read(b, 0, b.length); + } + + public int + read(byte[] b, int offset, int count) throws IOException + { + try + { + _s.getBuffer().b.get(b, offset, count); + } + catch(java.lang.Exception ex) + { + throw new IOException(ex.toString()); + } + return count; + } + + public int + available() + { + return _s.getBuffer().b.remaining(); + } + + public void + mark(int readlimit) + { + _markPos = _s.pos(); + } + + public void + reset() throws IOException + { + _s.pos(_markPos); + } + + public boolean + markSupported() + { + return true; + } + + public void + close() throws IOException + { + } + + private BasicStream _s; + private int _markPos; +} diff --git a/java/src/IceInternal/OutputStreamWrapper.java b/java/src/IceInternal/OutputStreamWrapper.java new file mode 100644 index 00000000000..471eef7358c --- /dev/null +++ b/java/src/IceInternal/OutputStreamWrapper.java @@ -0,0 +1,173 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +package IceInternal; + +import java.io.*; + +// +// Class to provide a java.io.OutputStream on top of a BasicStream. +// We use this to serialize arbitrary Java serializable classes into +// +// Slice sequences are encoded on the wire as a count of elements, followed +// by the sequence contents. For arbitrary Java classes, we do not know how +// big the sequence that is eventually written will be. To avoid excessive +// data copying, this class mantains a private _bytes array of 254 bytes and, +// initially, writes data into that array. If more than 254 bytes end up being +// written, we write a dummy sequence size of 255 (which occupies five bytes +// on the wire) into the BasicStream and, once this stream is closed, patch +// that size to match the actual size. Otherwise, if the _bytes buffer contains +// fewer than 255 bytes when this stream is closed, we write the sequence size +// as a single byte, followed by the contents of the _bytes buffer. +// + +public class OutputStreamWrapper extends java.io.OutputStream +{ + public + OutputStreamWrapper(BasicStream s) + { + _s = s; + _spos = s.pos(); + _bytes = new byte[254]; + _pos = 0; + } + + public void + write(int b) throws IOException + { + try + { + if(_bytes != null) + { + // + // If we can fit the data into the first 254 bytes, write it to _bytes. + // + if(_pos < _bytes.length) + { + _bytes[_pos++] = (byte)b; + return; + } + + _s.writeSize(255); // Dummy size, until we know how big the stream + // really is and can patch the size. + + if(_pos > 0) + { + // + // Write the current contents of _bytes. + // + _s.expand(_pos); + _s.getBuffer().b.put(_bytes, 0, _pos); + } + _bytes = null; + } + + // + // Write data passed by caller. + // + _s.expand(1); + _s.getBuffer().b.put((byte)b); + _pos += 1; + } + catch(java.lang.Exception ex) + { + throw new IOException(ex.toString()); + } + } + + public void + write(byte[] b) throws IOException + { + write(b, 0, b.length); + } + + public void + write(byte[] bytes, int offset, int count) throws IOException + { + try + { + if(_bytes != null) + { + // + // If we can fit the data into the first 254 bytes, write it to _bytes. + // + if(count <= _bytes.length - _pos) + { + System.arraycopy(bytes, offset, _bytes, _pos, count); + _pos += count; + return; + } + + _s.writeSize(255); // Dummy size, until we know how big the stream + // really is and can patch the size. + + if(_pos > 0) + { + // + // Write the current contents of _bytes. + // + _s.expand(_pos); + _s.getBuffer().b.put(_bytes, 0, _pos); + } + _bytes = null; + } + + // + // Write data passed by caller. + // + _s.expand(count); + _s.getBuffer().b.put(bytes, offset, count); + _pos += count; + } + catch(java.lang.Exception ex) + { + throw new IOException(ex.toString()); + } + } + + public void + flush() throws IOException + { + // This does nothing because we do not know the final size of a writable stream until it is closed, + // and we cannot write to the BasicStream until we know whether the final size is < 255 or not. + } + + public void + close() throws IOException + { + try + { + if(_bytes != null) + { + assert(_pos <= _bytes.length); + _s.pos(_spos); + _s.writeSize(_pos); + _s.expand(_pos); + _s.getBuffer().b.put(_bytes, 0, _pos); + _bytes = null; + } + else + { + int currentPos = _s.pos(); + _s.pos(_spos); + _s.writeSize(_pos); // Patch previously-written dummy value. + _s.pos(currentPos); + } + } + catch(java.lang.Exception ex) + { + throw new IOException(ex.toString()); + } + } + + private BasicStream _s; + private int _spos; + private byte[] _bytes; + private int _pos; +} |