private void pageToFileStream() throws IOException {
    flush();

    ByteArrayOutputStream bout = (ByteArrayOutputStream) currentStream;
    tempFile = FileUtil.createTempFile("cos", ".tmp", strategy.getSpoolDirectory());

    LOG.trace("Creating temporary stream cache file: {}", tempFile);

    try {
      currentStream = createOutputStream(tempFile);
      bout.writeTo(currentStream);
    } finally {
      // ensure flag is flipped to file based
      inMemory = false;
    }
  }
  /** Creates a new {@link StreamCache} from the data cached in this {@link OutputStream}. */
  public StreamCache newStreamCache() throws IOException {
    flush();

    if (inMemory) {
      if (currentStream instanceof CachedByteArrayOutputStream) {
        return ((CachedByteArrayOutputStream) currentStream).newInputStreamCache();
      } else {
        throw new IllegalStateException(
            "CurrentStream should be an instance of CachedByteArrayOutputStream but is: "
                + currentStream.getClass().getName());
      }
    } else {
      try {
        if (fileInputStreamCache == null) {
          fileInputStreamCache = new FileInputStreamCache(tempFile, ciphers);
        }
        return fileInputStreamCache;
      } catch (FileNotFoundException e) {
        throw new IOException("Cached file " + tempFile + " not found", e);
      }
    }
  }