コード例 #1
0
  /**
   * Method used to rewrap a GTSWrapper, typically to change the compression settings.
   *
   * @param wrapper
   * @param compress
   * @param compratio
   * @return
   */
  public static GTSWrapper rewrap(GTSWrapper wrapper, boolean compress, double compratio) {
    byte[] unwrapped = unwrapEncoded(wrapper);

    GTSEncoder encoder = new GTSEncoder(wrapper.getBase(), null, unwrapped);

    GTSWrapper tmp = fromGTSEncoderToGTSWrapper(encoder, compress, compratio);

    GTSWrapper rewrapped = new GTSWrapper(wrapper);
    rewrapped.setCompressed(tmp.isCompressed());
    rewrapped.setCompressionPasses(tmp.getCompressionPasses());
    rewrapped.setEncoded(tmp.getEncoded());

    return rewrapped;
  }
コード例 #2
0
  public static GTSWrapper fromGTSEncoderToGTSWrapper(
      GTSEncoder encoder, boolean compress, double compratio) {

    if (compratio < 1.0D) {
      compratio = 1.0D;
    }

    GTSWrapper wrapper = new GTSWrapper();

    try {
      if (!compress) {
        wrapper.setEncoded(encoder.getBytes());
      } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = encoder.getBytes();

        double ratio = 0.0D;

        int pass = 0;

        //
        // We compress the data once, if the compression ratio is greater
        // than 'compratio', we consider that the resulting compressed data will
        // probably still have lots of repetitions since we will have
        // overflowed the gzip sliding window several times, we therefore
        // enter a loop to compress the data until the compression ratio
        // falls below 'compratio'
        // We then store the number of compression passes in the GTSWrapper
        // so we can apply a matching number of decompression ops
        //
        // For ultimate compression, set 'compratio' to 1.0
        //

        byte[] encoded = null;

        do {
          encoded = bytes;
          ratio = bytes.length;
          GZIPOutputStream gzos = new GZIPOutputStream(baos);
          gzos.write(bytes);
          gzos.close();
          bytes = baos.toByteArray();
          baos.reset();
          ratio = ratio / bytes.length;
          pass++;
        } while (ratio > compratio);

        if (ratio > 1.0D) {
          // The last compression pass improved the footprint, so use the compressed data
          wrapper.setEncoded(bytes);
        } else {
          // The last pass added some overhead, ignore it
          pass = pass - 1;
          wrapper.setEncoded(encoded);
        }

        if (pass > 0) {
          wrapper.setCompressed(true);
          if (pass > 1) {
            // Only store number of passes if it is > 1 as 1 is the default value
            wrapper.setCompressionPasses(pass);
          }
        } else {
          wrapper.setCompressed(false);
        }
      }

      wrapper.setBase(encoder.getBaseTimestamp());
      wrapper.setCount(encoder.getCount());
      wrapper.setMetadata(encoder.getMetadata());

    } catch (IOException e) {
      e.printStackTrace();
    }

    return wrapper;
  }