/** * Copy in to out. Caller MUST close the streams. * * @param in non-null * @param out non-null * @since 0.9.29 */ public static void copy(InputStream in, OutputStream out) throws IOException { final ByteCache cache = ByteCache.getInstance(8, 8*1024); final ByteArray ba = cache.acquire(); try { final byte buf[] = ba.getData(); int read; while ((read = in.read(buf)) != -1) { out.write(buf, 0, read); } } finally { cache.release(ba); } }
/** * Get a cache responsible for objects of the given size. * Warning, if you store the result in a static field, the cleaners will * not operate after a restart on Android, as the old context's SimpleTimer2 will have shut down. * TODO tie this to the context or clean up all calls. * * @param cacheSize how large we want the cache to grow * (number of objects, NOT memory size) * before discarding released objects. * Since 0.7.14, a limit of 1MB / size is enforced * for the typical 128MB max memory JVM * @param size how large should the objects cached be? */ public static ByteCache getInstance(int cacheSize, int size) { if (cacheSize * size > MAX_CACHE) cacheSize = MAX_CACHE / size; Integer sz = Integer.valueOf(size); ByteCache cache; synchronized(_caches) { cache = _caches.get(sz); if (cache == null) { cache = new ByteCache(cacheSize, size); _caches.put(sz, cache); } } cache.resize(cacheSize); //I2PAppContext.getGlobalContext().logManager().getLog(ByteCache.class).error("ByteCache size: " + size + " max: " + cacheSize, new Exception("from")); return cache; }
public CachingByteArrayOutputStream(int cacheQuantity, int arraySize) { super(0); _cache = ByteCache.getInstance(cacheQuantity, arraySize); _buf = _cache.acquire(); super.buf = _buf.getData(); }
public void releaseResources() { _released = true; for (int i = 0; i < _fragments.length; i++) { if (_fragments[i] != null) { _fragmentCache.release(_fragments[i]); _fragments[i] = null; } } }
private static ByteArray acquireReadBuf() { return _dataReadBufs.acquire(); }
public V2StreamSender ( I2PSocket s, int id ) throws IOException { super ( s, id ); _data = new ArrayList<ByteArray> ( 1 ); _cache = ByteCache.getInstance ( 10, 32 * 1024 ); _out = s.getOutputStream(); _stillRunning = true; }
/** * Clear everything (memory pressure) * @since 0.7.14 */ public static void clearAll() { for (ByteCache bc : _caches.values()) bc.clear(); //_log.warn("WARNING: Low memory, clearing byte caches"); }
/** * Put this structure back onto the available cache for reuse * */ @Override public final void release(ByteArray entry) { release(entry, true); }
public HTTPResponseOutputStream(OutputStream raw) { super(raw); _context = I2PAppContext.getGlobalContext(); // all createRateStat in I2PTunnelHTTPClient.startRunning() _log = _context.logManager().getLog(getClass()); _headerBuffer = _cache.acquire(); _buf1 = new byte[1]; }
public MessageOutputStream(I2PAppContext ctx, SimpleTimer2 timer, DataReceiver receiver, int bufSize, int passiveFlushDelay) { super(); _dataCache = ByteCache.getInstance(128, bufSize); _originalBufferSize = bufSize; _currentBufferSize = bufSize; _context = ctx; _log = ctx.logManager().getLog(MessageOutputStream.class); _buf = _dataCache.acquire().getData(); // new byte[bufSize]; _dataReceiver = receiver; _dataLock = new Object(); _writeTimeout = -1; _passiveFlushDelay = passiveFlushDelay; _nextBufferSize = 0; //_sendPeriodBeginTime = ctx.clock().now(); //_context.statManager().createRateStat("stream.sendBps", "How fast we pump data through the stream", "Stream", new long[] { 60*1000, 5*60*1000, 60*60*1000 }); _flusher = new Flusher(timer); //if (_log.shouldLog(Log.DEBUG)) // _log.debug("MessageOutputStream created"); }
/** * Does not start itself. Caller must start() */ public StreamForwarder(InputStream in, OutputStream out, boolean toI2P) { this.in = in; this.out = out; _toI2P = toI2P; direction = (toI2P ? "toI2P" : "fromI2P"); _cache = ByteCache.getInstance(32, NETWORK_BUFFER_SIZE); setName("StreamForwarder " + _runnerId + '.' + direction); }
private static void releaseReadBuf(ByteArray buf) { _dataReadBufs.release(buf, false); }
ByteCache cache = ByteCache.getInstance(8, MAX_UNCOMPRESSED); ByteArray outBuf = cache.acquire(); try { int written = 0; return rv; } finally { cache.release(outBuf); ReusableGZIPInputStream.release(in);
/** * Send bytes through the SAM STREAM session socket sender * * @throws IOException if the client didnt provide enough data */ @Override public void sendBytes(InputStream in, int size) throws IOException { if (_log.shouldLog(Log.DEBUG)) _log.debug("Handler " + _id + ": sending " + size + " bytes"); ByteArray ba = _cache.acquire(); int read = DataHelper.read(in, ba.getData(), 0, size); if (read != size) throw new IOException("Insufficient data from the SAM client (" + read + "/" + size + ")"); ba.setValid(read); synchronized (_data) { _data.add(ba); _data.notifyAll(); } }
public MessageOutputStream(I2PAppContext ctx, SimpleTimer2 timer, DataReceiver receiver, int bufSize, int passiveFlushDelay) { super(); _dataCache = ByteCache.getInstance(128, bufSize); _originalBufferSize = bufSize; _currentBufferSize = bufSize; _context = ctx; _log = ctx.logManager().getLog(MessageOutputStream.class); _buf = _dataCache.acquire().getData(); // new byte[bufSize]; _dataReceiver = receiver; _dataLock = new Object(); _writeTimeout = -1; _passiveFlushDelay = passiveFlushDelay; _nextBufferSize = 0; //_sendPeriodBeginTime = ctx.clock().now(); //_context.statManager().createRateStat("stream.sendBps", "How fast we pump data through the stream", "Stream", new long[] { 60*1000, 5*60*1000, 60*60*1000 }); _flusher = new Flusher(timer); //if (_log.shouldLog(Log.DEBUG)) // _log.debug("MessageOutputStream created"); }
/** * Does not start itself. Caller must start() */ private StreamForwarder(InputStream in, OutputStream out, boolean toI2P) { this.in = in; this.out = out; _toI2P = toI2P; direction = (toI2P ? "toOutproxy" : "fromOutproxy"); _cache = ByteCache.getInstance(32, NETWORK_BUFFER_SIZE); setName("OutproxyForwarder " + _runnerId + '.' + __forwarderId.incrementAndGet()); }
public void releaseBuffer() { _cache.release(_buf); } }