private Reader(SocketChannel sockChannel, int slowConsumerRetries, SystemRequestHandler systemRequestHandler) { _socketChannel = sockChannel; _headerBuffer.order(ByteOrder.BIG_ENDIAN); _streamContext = MarshalInputStream.createContext(); try { _ois = new MarshalInputStream(_bais, _streamContext); } catch (IOException e) { if (_logger.isLoggable(Level.SEVERE)) { _logger.log(Level.SEVERE, e.getMessage(), e); } throw new RuntimeException("Failed to initialize LRMI Reader stream: ", e); } this._slowConsumerRetries = slowConsumerRetries; this._systemRequestHandler = systemRequestHandler; }
public void closeContext() { _ois.closeContext(); }
/** * 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 <T> T readRepetitiveObject(ObjectInput in) throws IOException, ClassNotFoundException { if (in instanceof MarshalInputStream) return (T) ((MarshalInputStream) in).readRepetitiveObject(); return (T) readObject(in); }
/** * 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; }
/** * Overrides the super method and tries to get the class descriptor from a local map. If not * found read from the stream and save to local map. * * @return class descriptor */ @Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass cl = null; int index = readInt(); if (index != -1) cl = _context.getObjectStreamClass(index); if (cl == null) { if (_logger.isLoggable(Level.FINEST)) { Long classLoaderKey = ClassLoaderCache.getCache().getClassLoaderKey(ClassLoaderHelper.getContextClassLoader()); logFinest("Received new incoming ObjectStreamClass with key " + index + ", context class loader key " + classLoaderKey + ", reading it from stream"); } cl = super.readClassDescriptor(); if (index != -1) _context.addObjectStreamClass(index, cl); } return cl; }
/** * 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(); } }
private <T extends IPacket> T bytesToPacket(T packet, boolean createNewBuffer, int slowConsumerTimeout, int sizeLimit) throws IOException, ClassNotFoundException, IOFilterException { if (_bufferIsOccupied || createNewBuffer) { GSByteArrayInputStream bis = new GSByteArrayInputStream(readBytesBlocking(true, slowConsumerTimeout, sizeLimit)); MarshalInputStream mis = new MarshalInputStream(bis, _streamContext); unmarshall(packet, mis); if (_logger.isLoggable(Level.FINEST)) { _logger.finest("<-- Read Packet " + packet); } return packet; } _bufferIsOccupied = true; _bais.setBuffer(readBytesBlocking(false, slowConsumerTimeout, sizeLimit)); unmarshall(packet, _ois); if (_logger.isLoggable(Level.FINEST)) { _logger.finest("<-- Read packet " + packet); } return packet; }
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; } } } }
public void resetContext() { _ois.resetContext(); }
readFromSocketChannel(socketChannel, packetBuffer, "read packet", absoluteTimeout); GSByteArrayInputStream bis = new GSByteArrayInputStream(packetBuffer.array()); MarshalInputStream mis = new MarshalInputStream(bis); ReplyPacket<?> replyPacket = new ReplyPacket(); try {
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); } }
_ois = new MarshalInputStream(_bais, _streamContext); } catch (IOException ioe) { throw new UnMarshallingException("Failed to unmarsh :" + packet, ioe);
/** * Read strings that were Shrinked using {@link #writeRepetitiveString(ObjectOutput, String)} */ public static String readRepetitiveString(ObjectInput in) throws IOException, ClassNotFoundException { if (in instanceof MarshalInputStream) return (String) ((MarshalInputStream) in).readRepetitiveObject(); return readString(in); }
private MarshalInputStream bytesToStream(Context ctx) throws IOException, IOFilterException { boolean startOfRequest = (ctx.phase == Context.Phase.START); if (_bufferIsOccupied && startOfRequest) ctx.createNewBuffer = true; else _bufferIsOccupied = true; byte[] res = readBytesNonBlocking(ctx); boolean endOfRequest = ctx.phase == Context.Phase.FINISH; if (endOfRequest) { if (ctx.isSystemRequest()) { ctx.systemRequestContext.prepare(res); } else { ctx.bytes = res; if (ctx.createNewBuffer) { ctx.createNewBuffer = false; return new MarshalInputStream(new GSByteArrayInputStream(res), _streamContext); } _bais.setBuffer(res); return _ois; } } return null; }