All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ucar.nc2.stream.NcStreamCompression Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
 * See LICENSE for license information.
 */

package ucar.nc2.stream;

import java.io.IOException;
import java.io.OutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by rmay on 8/10/15.
 */
public class NcStreamCompression {
  private static Logger logger = LoggerFactory.getLogger(NcStreamCompression.class);
  NcStreamProto.Compress type;
  Object compressInfo;

  private NcStreamCompression(NcStreamProto.Compress type, Object info) {
    this.type = type;
    this.compressInfo = info;
  }

  private NcStreamCompression(NcStreamProto.Compress type) {
    this(type, null);
  }

  public static NcStreamCompression none() {
    return new NcStreamCompression(NcStreamProto.Compress.NONE);
  }

  public static NcStreamCompression deflate() {
    return deflate(-1);
  }

  public static NcStreamCompression deflate(int level) {
    return new NcStreamCompression(NcStreamProto.Compress.DEFLATE, level);
  }

  public OutputStream setupStream(OutputStream out, int size) throws IOException {
    switch (type) {
      // For compression (currently deflate) we compress the data, then
      // will write the block size, and then data, when the stream is closed.
      case DEFLATE:
        // limit level to range [-1, 9], where -1 is default deflate setting.
        int level = Math.min(Math.max((Integer) compressInfo, -1), 9);
        int bufferSize = Math.min(size / 2, 512 * 1024 * 1024);
        return new NcStreamCompressedOutputStream(out, bufferSize, level);

      default:
        logger.info(" Unknown compression type {}. Defaulting to none", type);

        // In the case of no compression, go ahead and write the block
        // size so that the stream is ready for data
      case NONE:
        NcStream.writeVInt(out, size);
        return out;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy