/** * */ 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); } } }
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; }
GSByteArrayInputStream bis = new GSByteArrayInputStream(packetBuffer.array()); MarshalInputStream mis = new MarshalInputStream(bis); ReplyPacket<?> replyPacket = new ReplyPacket();
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; }
@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); } }