PacketSerializer(int initBufferSize, IPacketStreamSerializer<T> packetStreamSerializer) { _packetStreamSerializer = packetStreamSerializer; _buffer = new byte[initBufferSize]; _gsByteOutputStream = new GSByteArrayOutputStream(); _gsByteOutputStream.setBuffer(_buffer); _byteBuffer = ByteBuffer.wrap(_buffer); try { _oss = new ByteBufferObjectOutputStream(_gsByteOutputStream); } catch (IOException e) { throw new RuntimeException("Internal error creating PacketSerializer", e); } }
public Writer(SocketChannel sockChannel, int slowConsumerThroughput, int slowConsumerLatency, int slowConsumerRetries, IWriteInterestManager writeInterestManager) { _sockChannel = sockChannel; _slowConsumerThroughput = slowConsumerThroughput; _slowConsumerLatency = slowConsumerLatency; _slowConsumerRetries = slowConsumerRetries; _slowConsumer = _slowConsumerThroughput > 0; _slowConsumerSleepTime = _slowConsumerLatency / _slowConsumerRetries + 1; _slowConsumerBytes = (_slowConsumerThroughput * _slowConsumerLatency) / 1000; _contexts = new LinkedList<Context>(); _writeInterestManager = writeInterestManager; try { _baos = new GSByteArrayOutputStream(); _baos.setSize(LENGTH_SIZE); // mark the buffer to start writing only after the length place _oos = new MarshalOutputStream(_baos, true); // add a TC_RESET using the MarshalOutputStream.writeStreamHeader() initBuffer(_baos); } catch (Exception e) { if (_logger.isLoggable(Level.SEVERE)) { _logger.log(Level.SEVERE, e.getMessage(), e); } throw new RuntimeException("Failed to initialize LRMI Writer stream: ", e); } }
/** * */ public MarshObjectConvertor(ISmartLengthBasedCacheCallback cacheCallback) { _byteArrayCache = createSerializationByteArrayCache(cacheCallback); try { _bao = new GSByteArrayOutputStream(); _oo = getObjectOutputStream(_bao); _bai = new GSByteArrayInputStream(new byte[0]); try { getObject(getMarshObjectInternal("", true)); } catch (ClassNotFoundException e) { if (_logger.isLoggable(Level.SEVERE)) { _logger.log(Level.SEVERE, e.getMessage(), e); } } } catch (IOException e) { if (_logger.isLoggable(Level.SEVERE)) { _logger.log(Level.SEVERE, e.getMessage(), e); } } }
/** * @param level the compression level (0-9), The default setting is DEFAULT_COMPRESSION. * @throws IllegalArgumentException if the compression level is invalid */ public CompressedMarshObjectConvertor(int level, ISmartLengthBasedCacheCallback cacheCallback) { _byteArrayCache = createSerializationByteArrayCache(cacheCallback); _level = level; try { _bao = new GSByteArrayOutputStream(); _zo = new ZipOutputStream(_bao); _zo.setLevel(_level); _zo.putNextEntry(new ZipEntry(Integer.toString(zipEntryCounter++))); _oo = getObjectOutputStream(_zo); _bai = new GSByteArrayInputStream(new byte[0]); _zi = new ZipInputStream(_bai); getObject(getMarshObjectInternal("", true)); // remove header from // in/out } catch (Exception e) { if (_logger.isLoggable(Level.SEVERE)) { _logger.log(Level.SEVERE, e.getMessage(), e); } } }
public LRMIChat(RequestPacket packet) throws IOException { MarshalOutputStream mos; GSByteArrayOutputStream bos = new GSByteArrayOutputStream(); bos.setSize(4); mos = new MarshalOutputStream(bos, false); ByteBuffer buffer = wrap(bos); packet.writeExternal(mos); msg = prepareBuffer(mos, bos, buffer); mode = Mode.WRITE; headerBuffer = ByteBuffer.allocate(4); headerBuffer.order(ByteOrder.BIG_ENDIAN); }
@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(); } }
/** * Convert the desired object into the new object instance assignable from provided ClassLoader. * NOTE: This method is very expensive and provided object to convert must be {@link * java.io.Serializable}. * * @param obj the object to convert. Can be array as well. * @param classLoader the desired ClassLoader to convert the passed object. * @return the new instance of object derived from passed ClassLoader. * @throws IOException failed to convert **/ static public Object convertToAssignableClassLoader(Object obj, ClassLoader classLoader) throws IOException, ClassNotFoundException { GSByteArrayOutputStream oos = new GSByteArrayOutputStream(); MarshalOutputStream mos = new MarshalOutputStream(oos); mos.writeObject(obj); oos.flush(); GSByteArrayInputStream bais = new GSByteArrayInputStream(oos.toByteArray()); ClassLoader origCL = Thread.currentThread().getContextClassLoader(); try { ClassLoaderHelper.setContextClassLoader(classLoader, true); MarshalInputStream mis = new MarshalInputStream(bais); return mis.readObject(); } finally { ClassLoaderHelper.setContextClassLoader(origCL, true); } }
} else // build a temporal buffer and streams bos = new GSByteArrayOutputStream();