private void resetStreamState(MarshalInputStream mis) throws IOException, ClassNotFoundException { if (mis == _ois) { try { //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 _bais.setBuffer(_resetBuffer); mis.readObject(); } finally { if (_bufferIsOccupied) { _bais.setBuffer(DUMMY_BUFFER); // release the internal reference for the byte array _bufferIsOccupied = false; } } } }
/** * Reads a repetitive object which was written by {@link MarshalOutputStream#writeRepetitiveObject}. */ public Object readRepetitiveObject() throws IOException, ClassNotFoundException { // Read object code: int code = readInt(); // If null return: if (code == CODE_NULL) return null; // If repetitive optimization was disabled: if (code == CODE_DISABLED) return readObject(); // Look for cached object by code: Object value = _context.getRepetitiveObjectsCache().get(code); if (value != null) return value; // If object is not cached, read it from the stream and cache it: value = readObject(); // If object is string, intern it: if (value instanceof String) value = ((String) value).intern(); _context.getRepetitiveObjectsCache().put(code, value); return value; }
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); } }
/** * 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); } }