private ByteBuffer wrap(GSByteArrayOutputStream bos) { ByteBuffer byteBuffer = ByteBuffer.wrap(bos.getBuffer()); byteBuffer.order(ByteOrder.BIG_ENDIAN); return byteBuffer; }
/** * Wraps a GSByteArrayOutputStream inner buffer with a ByteBuffer * * @param bos stream to wrap * @return wrapping ByteBuffer */ private ByteBuffer wrap(GSByteArrayOutputStream bos) { ByteBuffer byteBuffer = ByteBuffer.wrap(bos.getBuffer()); byteBuffer.order(ByteOrder.BIG_ENDIAN); return byteBuffer; }
private ByteBuffer prepareBuffer(MarshalOutputStream mos, GSByteArrayOutputStream bos, ByteBuffer byteBuffer) throws IOException { mos.flush(); int length = bos.size(); if (byteBuffer.array() != bos.getBuffer()) // the buffer was changed { byteBuffer = wrap(bos); } else { byteBuffer.clear(); } byteBuffer.putInt(length - 4); byteBuffer.position(length); byteBuffer.flip(); return byteBuffer; } }
/** * @param byteBuffer buffer that might be used by the GSByteArrayOutputStream * @return prepared buffer. */ private ByteBuffer prepareBuffer(MarshalOutputStream mos, GSByteArrayOutputStream bos, ByteBuffer byteBuffer) throws IOException { mos.flush(); int length = bos.size(); if (byteBuffer.array() != bos.getBuffer()) // the buffer was changed { byteBuffer = wrap(bos); } else { byteBuffer.clear(); } byteBuffer.putInt(length - LENGTH_SIZE); byteBuffer.position(length); byteBuffer.flip(); return byteBuffer; }
public ByteBuffer serializePacket(T packet) throws IOException { _packetStreamSerializer.writePacketToStream(_oss, packet); _oss.flush(); _oss.reset(); byte[] actualBuffer = _gsByteOutputStream.getBuffer(); //Need to rewrap the buffer because it was changed if (actualBuffer != _buffer) { _buffer = actualBuffer; _byteBuffer = ByteBuffer.wrap(_buffer); } //Remove reset indicator _byteBuffer.limit(_gsByteOutputStream.size() - 1); _byteBuffer.position(0); _gsByteOutputStream.setSize(0); return _byteBuffer; }
@Override public Object cloneValue(Object originalValue, boolean isCloneable, Class<?> clzz, String uid, String entryClassName) { if (isCloneable && clzz != null) { try { return clzz.getMethod("clone", new Class[0]).invoke(originalValue, new Object[0]); } catch (Exception ex) { } } try { if (_mis == null) {//create _os = new GSByteArrayOutputStream(INITIAL_SIZE); _is = new GSByteArrayInputStream(_os.getBuffer()); _mos = new MarshalOutputStream(_os); _mis = new MarshalInputStream(_is); } _mos.writeUnshared(originalValue); _is.setBuffer(_os.getBuffer(), _os.getCount()); Object res = _mis.readUnshared(); return res; } catch (Exception ex) { if (clzz == null) clzz = originalValue.getClass(); throw new FieldValueCloningErrorException(uid, entryClassName, clzz.getName(), originalValue, ex); } finally { reset(); } }