public void closeContext() { _oos.closeContext(); }
public void resetContext() { _oos.resetContext(); }
@Override protected void writeStreamHeader() throws IOException { writeByte(TC_RESET); }
mos = new MarshalOutputStream(bos, false); byteBuffer = wrap(bos); mos.reset(); if (buffer != byteBuffer) // replace the buffer in soft reference if needed _bufferCache.set(buffer); } else { mos.closeContext();
writeInt(CODE_NULL); return; writeInt(CODE_DISABLED); writeObject(obj); return; writeInt(code); return; writeInt(code); writeObject(obj);
/** * 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); } }
@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(); } }
/** * Should only be used for objects that their class is known to SystemJars.DATA_GRID_JAR, * meaning at SystemJars.DATA_GRID_JAR, its dependencies or JDK Objects read and written with * repetitive must be immutable (cannot be changed as they are kept in underlying map, changing * them will affect the next repetitiveRead/Write */ public static void writeRepetitiveObject(ObjectOutput out, Object obj) throws IOException { if (out instanceof MarshalOutputStream) ((MarshalOutputStream) out).writeRepetitiveObject(obj); else writeObject(out, obj); }
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); } }
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; } }
/** * Overrides the original implementation, tries to save sending class descriptor, first checks * if the class was already sent. If it does send only the class index otherwise send the class * descriptor also. * * @param desc class descriptor to write to the stream. * @throws IOException If an I/O error has occurred. */ @Override protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { int i = _context.getObjectStreamClassKey(desc); boolean newClass = false; if (i == 0 || !_optimize) { newClass = true; if (_optimize) { i = _nextClassId++; _context.putObjectStreamClassKey(desc, i);// classMap.put(desc, i); } else { i = -1; } } writeInt(i); // write class ID if (newClass) // new class { super.writeClassDescriptor(desc); } }
public void reset() { try { if (_mos != null) { _is.reset(); _os.reset(); _mos.reset(); //this is the only way to do reset on ObjetInputStream: // add reset flag and let the ObjectInputStream to read it // so all the handles in the ObjectInputStream will be cleared byte[] buffer = _is.getBuffer(); _is.setBuffer(_resetBuffer); _mis.readObject(); _is.setBuffer(buffer); } } catch (Exception ex) { throw new RuntimeException(ex); } }
/** * Shrink string over the wire, should be used for constant number of strings which are * repetitive (i.e space names, class names) */ public static void writeRepetitiveString(ObjectOutput out, String s) throws IOException { if (out instanceof MarshalOutputStream) ((MarshalOutputStream) out).writeRepetitiveObject(s); else writeString(out, s); }
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); }
/** * @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; }