Inspired from Guava com.google.common.io.ByteStreams
506555
protected void readBytes(final InputStream in, final ProgressByteProcessor processor) throws IOException {
        final byte[] buf = new byte[BUF_SIZE];
        try {
            int amt;
            do {
                amt = in.read(buf);
                if (amt == -1) {
                    break;
                }
            } while (processor.processBytes(buf, 0, amt));
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
257198
@Override
    public ByteBuffer readBytes(int numBytes) throws IOException {
        final int appsz = applicationBufferSize();
        ByteBuf readbuf = m_ce.allocator().ioBuffer(packetBufferSize());
        CompositeByteBuf msgbb = Unpooled.compositeBuffer();

        try {
            ByteBuf clear = doUnwrap(readbuf, appsz);

            msgbb.addComponent(true, clear);
            int needed = numBytes;
            if (numBytes == NOT_AVAILABLE) {
                needed = msgbb.readableBytes() >= 4 ? validateLength(msgbb.readInt()) : NOT_AVAILABLE;
            }
            while (msgbb.readableBytes() < (needed == NOT_AVAILABLE ? 4 : needed)) {
                clear = doUnwrap(readbuf, appsz);

                msgbb.addComponent(true, clear);

                if (needed == NOT_AVAILABLE && msgbb.readableBytes() >= 4) {
                    needed = validateLength(msgbb.readInt());
                }
            }

            ByteBuffer retbb = ByteBuffer.allocate(needed);
            msgbb.readBytes(retbb);
            msgbb.discardReadComponents();

            assert !msgbb.isReadable() : "read from unblocked channel that received multiple messages?";

            return (ByteBuffer)retbb.flip();
        } finally {
            readbuf.release();
            msgbb.release();
        }
    }
877420
private static InputStreamWithMetadata compressStreamWithGZIP(
      InputStream inputStream) throws SnowflakeSQLException
  {
    FileBackedOutputStream tempStream =
        new FileBackedOutputStream(MAX_BUFFER_SIZE, true);

    try
    {

      DigestOutputStream digestStream = new DigestOutputStream(tempStream,
                                                               MessageDigest.getInstance("SHA-256"));

      CountingOutputStream countingStream =
          new CountingOutputStream(digestStream);

      // construct a gzip stream with sync_flush mode
      GZIPOutputStream gzipStream;

      gzipStream = new GZIPOutputStream(countingStream, true);

      IOUtils.copy(inputStream, gzipStream);

      inputStream.close();

      gzipStream.finish();
      gzipStream.flush();

      countingStream.flush();

      return new InputStreamWithMetadata(countingStream.getCount(),
                                         Base64.encodeAsString(digestStream.getMessageDigest().digest()),
                                         tempStream);

    }
    catch (IOException | NoSuchAlgorithmException ex)
    {
      logger.error("Exception compressing input stream", ex);

      throw new SnowflakeSQLException(ex, SqlState.INTERNAL_ERROR,
                                      ErrorCode.INTERNAL_ERROR.getMessageCode(),
                                      "error encountered for compression");
    }

  }
793155
static byte[] asByteArray(final InputStream in) throws IllegalArgumentException {
        // Precondition check
        if (in == null) {
            throw new IllegalArgumentException("stream must be specified");
        }

        // Get content as an array of bytes
        final ByteArrayOutputStream out = new ByteArrayOutputStream(8192);
        final int len = 4096;
        final byte[] buffer = new byte[len];
        int read = 0;
        try {
            while (((read = in.read(buffer)) != -1)) {
                out.write(buffer, 0, read);
            }
        } catch (final IOException ioe) {
            throw new RuntimeException("Error in obtainting bytes from " + in, ioe);
        } finally {
            try {
                in.close();
            } catch (final IOException ignore) {
                if (log.isLoggable(Level.FINER)) {
                    log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring");
                }
            }
            // We don't need to close the outstream, it's a byte array out
        }

        // Represent as byte array
        final byte[] content = out.toByteArray();

        // Return
        return content;
    }
225645
public static byte[] toByteArray(InputStream in) throws IOException {
    checkNotNull(in, "in");
    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      copy(in, out);
      return out.toByteArray();
    } finally {
      ensureClosed(in);
    }
  }
68969
@SuppressWarnings("unchecked")
	@Override
	public InputStream getInputStream(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		final String sourceMethod = "getInputStream"; //$NON-NLS-1$
		CacheEntry entry = null;
		String key = null;
		IAggregator aggr = (IAggregator)request.getAttribute(IAggregator.AGGREGATOR_REQATTRNAME);
		List<String> cacheInfoReport = null;
		ModuleList moduleList = null;
		boolean cacheKeyGenMutexAcquired = false;

		if (_isReportCacheInfo) {
			cacheInfoReport = (List<String>)request.getAttribute(LAYERCACHEINFO_PROPNAME);
			if (cacheInfoReport != null) {
				cacheInfoReport.clear();
			}
		}
		if (log.isLoggable(Level.FINEST) && cacheInfoReport == null) {
			cacheInfoReport = new LinkedList<String>();
		}
		try {
			IOptions options = aggr.getOptions();
			ICacheManager mgr = aggr.getCacheManager();
			boolean ignoreCached = RequestUtil.isIgnoreCached(request);
			boolean isSourceMapsEnabled = RequestUtil.isSourceMapsEnabled(request);
			InputStream result;
			long lastModified = getLastModified(request);
			CacheEntry newEntry = new CacheEntry(_id, _cacheKey, lastModified);
			CacheEntry existingEntry = null;

			if (ignoreCached) {
				request.setAttribute(NOCACHE_RESPONSE_REQATTRNAME, Boolean.TRUE);
			}
			if (options.isDevelopmentMode()) {
				synchronized(this) {
					// See if we need to discard previously built LayerBuilds
					if (lastModified > _lastModified) {
						if (cacheInfoReport != null) {
							cacheInfoReport.add("update_lastmod2"); //$NON-NLS-1$
						}
						_lastModified = lastModified;
						_cacheKeyGenerators = null;
					}
				}
			}

			if (_cacheKeyGenerators == null) {
				// The cache key generator for this layer hasn't been created yet.  Attempt
				// to grab the mutex.
				try {
					SignalUtil.aquire(_cacheKeyGenMutex, sourceClass, sourceMethod);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
				if (_cacheKeyGenerators == null) {
					// We're the first thread for this layer.  Set flag and proceed to build the
					// layer.  We'll release the mutex and let other thread through once we have
					// a cache key generator.
					cacheKeyGenMutexAcquired = true;
				} else {
					// Another thread already created the cache key generator.  Just release the
					// mutex and continue.
					_cacheKeyGenMutex.release();
				}
			}
			Map<String, ICacheKeyGenerator> cacheKeyGenerators = _cacheKeyGenerators;

			// Creata a cache key.
			key = generateCacheKey(request, cacheKeyGenerators);

			if (!ignoreCached && key != null) {
				int loopGuard = 5;
				do {
					// Try to retrieve an existing layer build using the blocking putIfAbsent.  If the return
					// value is null, then the newEntry was successfully added to the map, otherwise the
					// existing entry is returned in the buildReader and newEntry was not added.
					existingEntry = _layerBuilds.putIfAbsent(key, newEntry, options.isDevelopmentMode());
					if (cacheInfoReport != null) {
						cacheInfoReport.add(existingEntry != null ? "hit_1" : "added"); //$NON-NLS-1$ //$NON-NLS-2$
					}
					if (existingEntry != null) {
						if ((result = trySetResponse(request, response, existingEntry)) != null) {
							if (log.isLoggable(Level.FINEST)) {
								log.finest(cacheInfoReport.toString() + "\n" +  //$NON-NLS-1$
										"key:" + key +  //$NON-NLS-1$
										"\n" + existingEntry.toString()); //$NON-NLS-1$
							}
							if (_isReportCacheInfo) {
								request.setAttribute(LAYERBUILDCACHEKEY_PROPNAME, key);
							}
							return result;
						} else if (existingEntry.isDeleted()) {
							if (_layerBuilds.replace(key, existingEntry, newEntry)) {
								// entry was replaced, use newEntry
								if (cacheInfoReport != null) {
									cacheInfoReport.add("replace_1"); //$NON-NLS-1$
								}
								existingEntry = null;
							} else {
								// Existing entry was removed from the cache by another thread
								// between the time we retrieved it and the time we tried to
								// replace it.  Try to add the new entry again.
								if (cacheInfoReport != null) {
									cacheInfoReport.add("retry_add"); //$NON-NLS-1$
								}
								if (--loopGuard == 0) {
									// Should never happen, but just in case
									throw new IllegalStateException();
								}
								continue;
							}
						}
					}
					break;
				} while (true);
			}

			// putIfAbsent() succeeded and the new entry was added to the cache
			entry = (existingEntry != null) ? existingEntry : newEntry;

			LayerBuilder layerBuilder = null;

			// List of Future<IModule.ModuleReader> objects that will be used to read the module
			// data from
			List<ICacheKeyGenerator> moduleKeyGens = null;

			// Synchronize on the LayerBuild object for the build.  This will prevent multiple
			// threads from building the same output.  If more than one thread requests the same
			// output (same cache key), then the first one to grab the sync object will win and
			// the rest will wait for the first thread to finish building and then just return
			// the output from the first thread when they wake.
			synchronized(entry) {

				// Check to see if data is available one more time in case a different thread finished
				// building the output while we were blocked on the sync object.
				if (!ignoreCached && key != null && (result = trySetResponse(request, response, entry)) != null) {
					if (cacheInfoReport != null) {
						cacheInfoReport.add("hit_2"); //$NON-NLS-1$
					}
					if (log.isLoggable(Level.FINEST)) {
						log.finest(cacheInfoReport.toString() + "\n" + //$NON-NLS-1$
								"key:" + key +  //$NON-NLS-1$
								"\n" + entry.toString()); //$NON-NLS-1$
					}
					if (_isReportCacheInfo) {
						request.setAttribute(LAYERBUILDCACHEKEY_PROPNAME, key);
					}
					return result;
				}

				boolean isGzip = RequestUtil.isGzipEncoding(request);
				byte[] bytes, smbytes = null;


				// See if we already have a cached response that uses a different gzip
				// encoding option.  If we do, then just zip (or unzip) the cached
				// response
				CacheEntry otherEntry = null;
				if (key != null) {
					StringBuffer sb = new StringBuffer();
					Matcher m = GZIPFLAG_KEY_PATTERN.matcher(key);
					m.find();
					m.appendReplacement(sb,
							new StringBuffer(s_layerCacheKeyGenerators.get(0).toString())
					.append(":") //$NON-NLS-1$
					.append("1".equals(m.group(1)) ? "0" : "1") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					.append(":").toString() //$NON-NLS-1$
							).appendTail(sb);
					otherEntry = _layerBuilds.get(sb.toString());
				}
				if (otherEntry != null) {
					MutableObject<byte[]> sourceMapRef = isSourceMapsEnabled ? new MutableObject<byte[]>() : null;
					if (isGzip) {
						if (cacheInfoReport != null) {
							cacheInfoReport.add("zip_unzipped"); //$NON-NLS-1$
						}
						// We need gzipped and the cached entry is unzipped
						// Create the compression stream for the output
						bytes = ZipUtil.zip(otherEntry.getInputStream(request, sourceMapRef));
						if (isSourceMapsEnabled && sourceMapRef.getValue() != null) {
							smbytes = ZipUtil.zip(new ByteArrayInputStream(sourceMapRef.getValue()));
						}
					} else {
						if (cacheInfoReport != null) {
							cacheInfoReport.add("unzip_zipped"); //$NON-NLS-1$
						}
						// We need unzipped and the cached entry is zipped.  Just unzip it
						bytes = ZipUtil.unzip(otherEntry.getInputStream(request, sourceMapRef));
						if (isSourceMapsEnabled && sourceMapRef.getValue() != null) {
							// unzip the source map too
							smbytes = ZipUtil.unzip(new ByteArrayInputStream(sourceMapRef.getValue()));
						}
					}
					// Set the buildReader to the LayerBuild and release the lock by exiting the sync block
					entry.setData(bytes, smbytes);
					if (!ignoreCached) {
						_layerBuilds.replace(key, entry, entry);	// updates entry weight in map
						if (cacheInfoReport != null) {
							cacheInfoReport.add("update_weights_1"); //$NON-NLS-1$
						}
						entry.persist(mgr);
					}
				} else {
					moduleKeyGens = new LinkedList<ICacheKeyGenerator>();

					moduleList = getModules(request);

					// Remove the module list from the request to safe-guard it now that we don't
					// need it there anymore
					request.removeAttribute(MODULE_FILES_PROPNAME);

					// Create a BuildListReader from the list of Futures.  This reader will obtain a
					// ModuleReader from each of the Futures in the list and read data from each one in
					// succession until all the data has been read, blocking on each Future until the
					// reader becomes available.
					layerBuilder = new LayerBuilder(request, moduleKeyGens, moduleList);
					String layer = layerBuilder.build();
					String sourceMap = layerBuilder.getSourceMap();

					if (isGzip) {
						if (cacheInfoReport != null) {
							cacheInfoReport.add("zip"); //$NON-NLS-1$
						}
						bytes = ZipUtil.zip(new ReaderInputStream(new StringReader(layer)));
						// now compress the source map if there is one
						if (sourceMap != null) {
							smbytes = ZipUtil.zip(new ReaderInputStream(new StringReader(sourceMap)));
						}
					} else {
						bytes = layer.getBytes();
						smbytes = (sourceMap != null) ? sourceMap.getBytes() : null;
					}
					// Set the buildReader to the LayerBuild and release the lock by exiting the sync block
					entry.setData(bytes, smbytes);
					// entry will be persisted below after we determine if cache key
					// generator needs to be updated
				}
			}

			// if any of the readers included an error response, then don't cache the layer.
			if (layerBuilder != null && layerBuilder.hasErrors()) {
				request.setAttribute(NOCACHE_RESPONSE_REQATTRNAME, Boolean.TRUE);
				if (cacheInfoReport != null) {
					cacheInfoReport.add(key == null ? "error_noaction" : "error_remove"); //$NON-NLS-1$ //$NON-NLS-2$
				}
				if (key != null) {
					_layerBuilds.remove(key, entry);
				}
			} else if (layerBuilder != null) {
				if (!ignoreCached) {
					// See if we need to create or update the cache key generators
					Map<String, ICacheKeyGenerator> newKeyGens = new HashMap<String, ICacheKeyGenerator>();
					Set<String> requiredModuleListDeps = moduleList.getDependentFeatures();
					addCacheKeyGenerators(newKeyGens, s_layerCacheKeyGenerators);
					addCacheKeyGenerators(newKeyGens, aggr.getTransport().getCacheKeyGenerators());
					addCacheKeyGenerators(newKeyGens, Arrays.asList(new ICacheKeyGenerator[]{new FeatureSetCacheKeyGenerator(requiredModuleListDeps, false)}));
					addCacheKeyGenerators(newKeyGens, moduleKeyGens);

					boolean cacheKeyGeneratorsUpdated = false;
					if (!newKeyGens.equals(cacheKeyGenerators)) {
						// If we don't yet have a cache key for this layer, then get one
						// from the cache key generators, and then update the cache key for this
						// cache entry.

						synchronized(this) {
							if (_cacheKeyGenerators != null) {
								addCacheKeyGenerators(newKeyGens, _cacheKeyGenerators.values());
							}
							_cacheKeyGenerators = Collections.unmodifiableMap(newKeyGens);
						}
						if (cacheKeyGenMutexAcquired) {
							cacheKeyGenMutexAcquired = false;
							_cacheKeyGenMutex.release();
						}
						if (cacheInfoReport != null) {
							cacheInfoReport.add("update_keygen"); //$NON-NLS-1$
						}
						cacheKeyGeneratorsUpdated = true;
					}
					final String originalKey = key;
					if (key == null || cacheKeyGeneratorsUpdated) {
						if (cacheInfoReport != null) {
							cacheInfoReport.add("update_key"); //$NON-NLS-1$
						}
						key = generateCacheKey(request, newKeyGens);
					}
					if (originalKey == null || !originalKey.equals(key)) {
						/*
						 * The cache key has changed from what was originally used to put the
						 * un-built entry into the cache.  Add the LayerBuild to the cache
						 * using the new key.
						 */
						if (log.isLoggable(Level.FINE)) {
							log.fine("Key changed!  Adding layer to cache with key: " + key); //$NON-NLS-1$
						}
						final CacheEntry originalEntry = entry;
						CacheEntry updateEntry = (originalKey == null) ? entry : new CacheEntry(entry);
						CacheEntry previousEntry = _layerBuilds.putIfAbsent(key, updateEntry, options.isDevelopmentMode());
						if (cacheInfoReport != null) {
							cacheInfoReport.add(previousEntry == null ? "update_add" : "update_hit"); //$NON-NLS-1$ //$NON-NLS-2$
						}
						// Write the file to disk only if the LayerBuild was successfully added to the cache
						if (previousEntry == null) {
							// Updated entry was added to the cache.
							entry = updateEntry;
							entry.persist(mgr);
						}
						// If the key changed, then remove the entry under the old key.  Use a
						// delay to give other threads a chance to start using the new cache
						// key generator.  No need to update entry weight in map
						if (originalKey != null) {
							aggr.getExecutors().getScheduledExecutor().schedule(new Runnable() {
								public void run() {
									_layerBuilds.remove(originalKey, originalEntry);
								}
							}, LAYERBUILD_REMOVE_DELAY_SECONDS, TimeUnit.SECONDS);
						}
					} else {
						if (cacheInfoReport != null) {
							cacheInfoReport.add("update_weights_2"); //$NON-NLS-1$
						}
						_layerBuilds.replace(key, entry, entry);	// updates entry weight in map
						entry.persist(mgr);
					}
				}
			}
			result = setResponse(request, response, entry);
			// return the input stream to the LayerBuild
			if (log.isLoggable(Level.FINEST)) {
				log.finest(cacheInfoReport.toString() + "\n" + //$NON-NLS-1$
						"key:" + key +  //$NON-NLS-1$
						"\n" + entry.toString()); //$NON-NLS-1$
			}
			if (_isReportCacheInfo) {
				request.setAttribute(LAYERBUILDCACHEKEY_PROPNAME, key);
			}
			return result;
		} catch (IOException e) {
			_layerBuilds.remove(key, entry);
			throw e;
		} catch (RuntimeException e) {
			_layerBuilds.remove(key, entry);
			throw e;
		} finally {
			if (cacheKeyGenMutexAcquired) {
				_cacheKeyGenMutex.release();
			}
			if (_layerBuilds.isLayerEvicted()) {
				_layerBuilds.removeLayerFromCache(this);
			}
			// Help out the GC by clearing the module list
			if (moduleList != null) {
				request.removeAttribute(MODULE_FILES_PROPNAME);
				moduleList.clear();
			}
		}
	}
730118
public static FixedBucketsHistogram fromBytes(byte[] bytes)
  {
    ByteBuffer buf = ByteBuffer.wrap(bytes);
    return fromByteBuffer(buf);
  }
588945
private byte[] serializeAppData(BackedSession d2) {
        if (com.ibm.websphere.ras.TraceComponent.isAnyTracingEnabled() && LoggingUtil.SESSION_LOGGER_WAS.isLoggable(Level.FINE)) {
            LoggingUtil.SESSION_LOGGER_WAS.entering(methodClassName, methodNames[SERIALIZE_APP_DATA]);
        }
        // return either the byte array input stream for the app
        // data or a vector of byte array input streams if their is a
        // row for each piece of app data
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        byte[] objbuf = null;

        try {
            Map<Object, Object> ht = null;
            if (com.ibm.websphere.ras.TraceComponent.isAnyTracingEnabled() && LoggingUtil.SESSION_LOGGER_WAS.isLoggable(Level.FINE)) {
                LoggingUtil.SESSION_LOGGER_WAS.logp(Level.FINE, methodClassName, methodNames[SERIALIZE_APP_DATA], "get swappableData and convert to byte array");
            }
            synchronized (d2) {
                ht = d2.getSwappableData();
            }

            // serialize session (app data only) into byte array buffer
            baos = new ByteArrayOutputStream();
            oos = createObjectOutputStream(baos);
            oos.writeObject(ht);
            oos.flush();
            objbuf = baos.toByteArray();
            if (com.ibm.websphere.ras.TraceComponent.isAnyTracingEnabled() && LoggingUtil.SESSION_LOGGER_WAS.isLoggable(Level.FINE)) {
                LoggingUtil.SESSION_LOGGER_WAS.logp(Level.FINE, methodClassName, methodNames[SERIALIZE_APP_DATA], "success - size of byte array is " + objbuf.length);
            }

            oos.close();
            baos.close();
        } catch (ConcurrentModificationException cme) {
            LoggingUtil.SESSION_LOGGER_WAS.logp(Level.INFO, methodClassName, methodNames[SERIALIZE_APP_DATA], "DatabaseHashMap.deferWrite", /* + d2.deferWriteUntilNextTick */
                                                d2.getId());
        } catch (Exception e) {
            com.ibm.ws.ffdc.FFDCFilter.processException(e, "com.ibm.ws.session.store.db.DatabaseHashMap.commonSetup", "2052", d2);
            LoggingUtil.SESSION_LOGGER_WAS.logp(Level.SEVERE, methodClassName, methodNames[SERIALIZE_APP_DATA], "DatabaseHashMap.commonSetupError");
            LoggingUtil.SESSION_LOGGER_WAS.logp(Level.SEVERE, methodClassName, methodNames[SERIALIZE_APP_DATA], "CommonMessage.exception", e);
        }
        if (com.ibm.websphere.ras.TraceComponent.isAnyTracingEnabled() && LoggingUtil.SESSION_LOGGER_WAS.isLoggable(Level.FINER)) {
            LoggingUtil.SESSION_LOGGER_WAS.exiting(methodClassName, methodNames[SERIALIZE_APP_DATA]);
        }
        return objbuf;
    }
1084608
private void reconstituteTargetStreams(ProducerInputHandler inputHandler)
    throws MessageStoreException,
    SIException,
    SIResourceException   
  {
    if (tc.isEntryEnabled())
      SibTr.entry(tc, "reconstituteTargetStreams");

    /*
     * Iterate through all contained Protocol Items, rebuilding
     * associated target streams for each.
     */ 
    NonLockingCursor cursor = null;
    
    try
    {
      cursor = newNonLockingItemCursor(new ClassEqualsFilter(StreamSet.class));
           
	    AbstractItem item = null;
	    while (null != (item = cursor.next()))
	    {
	      StreamSet streamSet = (StreamSet)item;
	      inputHandler.reconstituteTargetStreams(streamSet);
	    }
    }
    finally
    {
      if (cursor != null)
        cursor.finished();
    }

    if (tc.isEntryEnabled())  
      SibTr.exit(tc, "reconstituteTargetStreams");      
  }
98394
@GwtIncompatible("InputStream")
  static InputStream asInputStream(final ByteInput input) {
    checkNotNull(input);
    return new InputStream() {
      @Override
      public int read() throws IOException {
        return input.read();
      }

      @Override
      public int read(byte[] b, int off, int len) throws IOException {
        checkNotNull(b);
        checkPositionIndexes(off, off + len, b.length);
        if (len == 0) {
          return 0;
        }
        int firstByte = read();
        if (firstByte == -1) {
          return -1;
        }
        b[off] = (byte) firstByte;
        for (int dst = 1; dst < len; dst++) {
          int readByte = read();
          if (readByte == -1) {
            return dst;
          }
          b[off + dst] = (byte) readByte;
        }
        return len;
      }

      @Override
      public void close() throws IOException {
        input.close();
      }
    };
  }
285409
private void createStreams(SecretKey encryptionKey, SecretKey hmacKey,
      byte[] iv, OutputStream out) throws CryptorException {

    this.iv = iv;

    try {
      Cipher cipher = Cipher.getInstance(AES256JNCryptor.AES_CIPHER_ALGORITHM);
      cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(iv));

      try {
        Mac mac = Mac.getInstance(AES256JNCryptor.HMAC_ALGORITHM);
        mac.init(hmacKey);

        macOutputStream = new MacOutputStream(out, mac);
        cipherStream = new CipherOutputStream(macOutputStream, cipher);

      } catch (GeneralSecurityException e) {
        throw new CryptorException("Failed to initialize HMac", e);
      }

    } catch (GeneralSecurityException e) {
      throw new CryptorException("Failed to initialize AES cipher", e);
    }
  }
803183
@Override
	public InputStream getInputStream(final String key, final URI source, final MutableInt retLength)
			throws IOException {
		final String sourceMethod = "getInputStream"; //$NON-NLS-1$
		final boolean isTraceLogging = log.isLoggable(Level.FINER);
		if (isTraceLogging) {
			log.entering(sourceClass, sourceMethod, new Object[] { key, source, retLength });
		}

		InputStream in = null, result = null;
		CacheEntry tryCacheEntry = (CacheEntry) super.get(key);
		URLConnection connection = source.toURL().openConnection();

		try {
			long lastModified = connection.getLastModified();
			if (tryCacheEntry != null) {
				// Make local copies of volatile CacheEntry fields
				byte[] bytes = tryCacheEntry.bytes;
				File file = tryCacheEntry.file;
				if (bytes != null) {
					// Important - CacheEntry.lastModified is set before CacheEntry.bytes so we can
					// safely
					// check CacheEntry.lastModified here even though we're not synchronized.
					if (lastModified != tryCacheEntry.lastModified) {
						// stale cache entry. Remove it and create a new one below
						cacheMap.remove(key, tryCacheEntry);
					} else {
						retLength.setValue(tryCacheEntry.bytes.length);
						result = new ByteArrayInputStream(tryCacheEntry.bytes);
					}
				} else if (file != null) {
					// Some platforms round file last modified times to nearest second.
					if (Math.abs(lastModified - file.lastModified()) > 1000) {
						// Stale cache entry, remove it and create a new one below
						cacheMap.remove(key, tryCacheEntry);
						// also delete the associated cache file asynchronously.
						cacheManager.deleteFileDelayed(file.getName());
					} else {
						try {
							retLength.setValue(file.length());
							result = new FileInputStream(file);
						} catch (FileNotFoundException ex) {
							// File doesn't exist (was probably deleted outside this program)
							// Not fatal, just fall through and create it again.
							cacheMap.remove(key, tryCacheEntry);
						}
					}
				}
				if (result != null) {
					// found result in cache. Return it.
					log.exiting(sourceClass, sourceMethod, result);
					return result;
				}
			}

			// Result not in cache (or we removed it). Try to create a new cache entry.
			CacheEntry newCacheEntry = new CacheEntry();
			CacheEntry oldCacheEntry = (CacheEntry) cacheMap.putIfAbsent(key, newCacheEntry);
			final CacheEntry cacheEntry = oldCacheEntry != null ? oldCacheEntry : newCacheEntry;

			// Synchronize on the cache entry so that more than one thread won't try to create the
			// zipped content.
			synchronized (cacheEntry) {
				if (cacheEntry.ex != null) {
					// An exception occurred trying to create the gzip response in another thread.
					// Re-throw the exception here.
					throw cacheEntry.ex;
				}
				// First, check to make sure that another thread didn't beat us to the punch.
				// Even though we're synchronized on the cacheEntry object, cacheEntry.bytes can be
				// cleared by the createCacheFileAsync callback, so we need to copy this volatile
				// field
				// to a local variable and access it from there.
				byte[] bytes = cacheEntry.bytes;
				if (bytes != null) {
					retLength.setValue(bytes.length);
					result = new ByteArrayInputStream(bytes);
				} else if (cacheEntry.file != null) { // once set, cacheEntry.file does not change
														// by convention
					retLength.setValue(cacheEntry.file.length());
					result = new FileInputStream(cacheEntry.file);
				} else {
					// Gzip encode the resource and save the result in the cache entry until the
					// cache
					// file is written asynchronously.
					try {
						in = connection.getInputStream();
						ByteArrayOutputStream bos = new ByteArrayOutputStream();
						VariableGZIPOutputStream compress = new VariableGZIPOutputStream(bos, 10240);
						compress.setLevel(Deflater.BEST_COMPRESSION);
						CopyUtil.copy(in, compress);

						// Important - CacheEntry.lastModified must be set before cacheEntry.bytes
						cacheEntry.lastModified = lastModified;
						cacheEntry.bytes = bos.toByteArray();
						result = new ByteArrayInputStream(cacheEntry.bytes);
						retLength.setValue(cacheEntry.bytes.length);

						// Call the cache manager to asynchronously save the gzipped response to
						// disk
						// Include the filename part of the source URI in the cached filename
						String path = source.getPath();
						int idx = path.lastIndexOf("/"); //$NON-NLS-1$
						String fname = (idx != -1) ? path.substring(idx + 1) : path;
						cacheManager.createCacheFileAsync(
								fname + ".gzip.", //$NON-NLS-1$
								new ByteArrayInputStream(cacheEntry.bytes),
								new ICacheManager.CreateCompletionCallback() {
									@Override
									public void completed(String filename, Exception e) {
										if (e != null && log.isLoggable(Level.SEVERE)) {
											// Exception occurred saving file. Not much we can do
											// except log the error
											log.logp(Level.SEVERE, sourceClass, sourceMethod,
													e.getMessage(), e);
											return;
										}
										File cacheFile = new File(cacheManager.getCacheDir(),
												filename);
										cacheFile.setLastModified(cacheEntry.lastModified);
										// Important - cacheEntry.file must be set before clearing
										// cacheEntry.bytes
										cacheEntry.file = cacheFile;
										cacheEntry.bytes = null;
									}
								});
					} catch (Throwable t) {
						cacheEntry.ex = (t instanceof IOException) ? (IOException) t
								: new IOException(t);
						cacheMap.remove(key, cacheEntry);
						throw cacheEntry.ex;
					}
				}
			}
		} finally {
			// URLConnection doesn't have a close method. The only way to make sure a connection is
			// closed is to close the input or output stream which is obtained from the connection.
			if (in != null) {
				in.close();
			} else {
				connection.getInputStream().close();
			}
		}
		if (isTraceLogging) {
			log.exiting(sourceClass, sourceClass, result);
		}
		return result;
	}
1359889
def FromStream(cls, stream, mime_type, total_size=None, auto_transfer=True,
                   gzip_encoded=False, **kwds):
        """"""
        if mime_type is None:
            raise exceptions.InvalidUserInputError(
                'No mime_type specified for stream')
        return cls(stream, mime_type, total_size=total_size,
                   close_stream=False, auto_transfer=auto_transfer,
                   gzip_encoded=gzip_encoded, **kwds)
69096
private static byte[] toBytes(Blob b) {
        try {
            InputStream is = b.getBinaryStream();
            byte[] result = IOUtils.toByteArray(is);
            is.close();
            b.free();
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new SQLRuntimeException(e);
        }

    }
531725
public byte[] getStreamAsBytearray() throws IOException {
		byte[] result = null;
		try {
			result = IOUtils.toByteArray(getStream());
		}
		catch (NullPointerException npex) {
			throw new IOException("Timeout has been forced by CommonHttpClient", npex);
		}
		return result;
	}
367626
def _BuildStreamingResponse(self, binary_stream, method_name=None):
    """"""
    precondition.AssertType(method_name, Text)

    # We get a first chunk of the output stream. This way the likelihood
    # of catching an exception that may happen during response generation
    # is much higher.
    content = binary_stream.GenerateContent()
    try:
      peek = content.next()
      stream = itertools.chain([peek], content)
    except StopIteration:
      stream = []

    response = werkzeug_wrappers.Response(
        response=stream,
        content_type="binary/octet-stream",
        direct_passthrough=True)
    response.headers["Content-Disposition"] = ((
        "attachment; filename=%s" % binary_stream.filename).encode("utf-8"))
    if method_name:
      response.headers["X-API-Method"] = method_name.encode("utf-8")

    if binary_stream.content_length:
      response.content_length = binary_stream.content_length

    return response
214393
public static byte[] unGzip(InputStream in, int length) throws UtilException {
		GZIPInputStream gzi = null;
		FastByteArrayOutputStream bos = null;
		try {
			gzi = (in instanceof GZIPInputStream) ? (GZIPInputStream)in : new GZIPInputStream(in);
			bos = new FastByteArrayOutputStream(length);
			IoUtil.copy(gzi, bos);
		} catch (IOException e) {
			throw new UtilException(e);
		} finally {
			IoUtil.close(gzi);
		}
		//返回必须在关闭gos后进行，因为关闭时会自动执行finish()方法，保证数据全部写出
		return bos.toByteArray();
	}