// Path: hive-dwrf/src/main/java/com/facebook/hive/orc/compression/CompressionCodec.java
// public interface CompressionCodec {
//   /**
//    * Compress the in buffer to the out buffer.
//    * @param in the bytes to compress
//    * @param out the uncompressed bytes
//    * @param overflow put any additional bytes here
//    * @return true if the output is smaller than input
//    * @throws IOException
//    */
//   boolean compress(ByteBuffer in, ByteBuffer out, ByteBuffer overflow
//                   ) throws IOException;
// 
//   /**
//    * Decompress the in buffer to the out buffer.
//    * @param in the bytes to decompress
//    * @param out the decompressed bytes
//    * @throws IOException
//    */
//   void decompress(ByteBuffer in, ByteBuffer out) throws IOException;
// 
//   void reloadConfigurations(Configuration conf);
// }
// 
// Path: hive-dwrf/src/main/java/com/facebook/hive/orc/compression/ZlibCodec.java
// public class ZlibCodec implements CompressionCodec {
// 
//   private int compressionLevel;
// 
//   public ZlibCodec() {
//     compressionLevel = Deflater.DEFAULT_COMPRESSION;
//   }
// 
//   public ZlibCodec(Configuration conf) {
//     if (conf == null) {
//       compressionLevel = Deflater.DEFAULT_COMPRESSION;
//     } else {
//       compressionLevel = OrcConf.getIntVar(conf, OrcConf.ConfVars.HIVE_ORC_ZLIB_COMPRESSION_LEVEL);
//     }
//   }
// 
//   public void reloadConfigurations(Configuration conf) {
//     compressionLevel = OrcConf.getIntVar(conf, OrcConf.ConfVars.HIVE_ORC_ZLIB_COMPRESSION_LEVEL);
//   }
// 
//   @Override
//   public boolean compress(ByteBuffer in, ByteBuffer out,
//                           ByteBuffer overflow) throws IOException {
//     Deflater deflater = new Deflater(compressionLevel, true);
//     int length = in.remaining();
//     deflater.setInput(in.array(), in.arrayOffset() + in.position(), length);
//     deflater.finish();
//     int outSize = 0;
//     int offset = out.arrayOffset() + out.position();
//     while (!deflater.finished() && (length > outSize)) {
//       int size = deflater.deflate(out.array(), offset, out.remaining());
//       out.position(size + out.position());
//       outSize += size;
//       offset += size;
//       // if we run out of space in the out buffer, use the overflow
//       if (out.remaining() == 0) {
//         if (overflow == null) {
//           deflater.end();
//           return false;
//         }
//         out = overflow;
//         offset = out.arrayOffset() + out.position();
//       }
//     }
//     deflater.end();
//     return length > outSize;
//   }
// 
//   @Override
//   public void decompress(ByteBuffer in, ByteBuffer out) throws IOException {
//     Inflater inflater = new Inflater(true);
//     inflater.setInput(in.array(), in.arrayOffset() + in.position(),
//                       in.remaining());
//     while (!(inflater.finished() || inflater.needsDictionary() ||
//              inflater.needsInput())) {
//       try {
//         int count = inflater.inflate(out.array(),
//                                      out.arrayOffset() + out.position(),
//                                      out.remaining());
//         out.position(count + out.position());
//       } catch (DataFormatException dfe) {
//         throw new IOException("Bad compression data", dfe);
//       }
//     }
//     out.flip();
//     inflater.end();
//     in.position(in.limit());
//   }
// 
// }
// 
// Path: hive-dwrf/src/main/java/com/facebook/hive/orc/WriterImpl.java
// static class RowIndexPositionRecorder implements PositionRecorder {
//   private final OrcProto.RowIndexEntry.Builder builder;
// 
//   RowIndexPositionRecorder(OrcProto.RowIndexEntry.Builder builder) {
//     this.builder = builder;
//   }
// 
//   @Override
//   public void addPosition(long position) {
//     builder.addPositions(position);
//   }
// }


import static junit.framework.Assert.assertEquals;
import java.nio.ByteBuffer;
import com.facebook.hive.orc.compression.CompressionCodec;
import com.facebook.hive.orc.compression.ZlibCodec;
import org.apache.hadoop.hive.serde2.ReaderWriterProfiler;
import org.junit.Test;
import com.facebook.hive.orc.OrcProto.RowIndex;
import com.facebook.hive.orc.OrcProto.RowIndexEntry;
import com.facebook.hive.orc.WriterImpl.RowIndexPositionRecorder;
      out.getPosition(rowIndexPosition);
      rowIndex.addEntry(rowIndexEntry.build());
      rowIndexEntry.clear();
      // test runs, non-runs
      if (i < COUNT / 2) {
        out.write(i & 1);
      } else {
        out.write((i/3) & 1);
      }
    }
    out.flush();
    ByteBuffer inBuf = ByteBuffer.allocate(collect.buffer.size());
    collect.buffer.setByteBuffer(inBuf, 0, collect.buffer.size());
    inBuf.flip();
    BitFieldReader in = new BitFieldReader(InStream.create("test", inBuf, codec, 500));
    for(int i=0; i < COUNT; ++i) {
      int x = in.next();
      if (i < COUNT / 2) {
        assertEquals(i & 1, x);
      } else {
        assertEquals((i/3) & 1, x);
      }
    }
    in.loadIndeces(rowIndex.build().getEntryList(), 0);
    for(int i=COUNT-1; i >= 0; --i) {
      in.seek(i);
      int x = in.next();
      if (i < COUNT / 2) {
        assertEquals(i & 1, x);
      } else {
// Path: hive-dwrf/src/main/java/com/facebook/hive/orc/compression/CompressionCodec.java
// public interface CompressionCodec {
//   /**
//    * Compress the in buffer to the out buffer.
//    * @param in the bytes to compress
//    * @param out the uncompressed bytes
//    * @param overflow put any additional bytes here
//    * @return true if the output is smaller than input
//    * @throws IOException
//    */
//   boolean compress(ByteBuffer in, ByteBuffer out, ByteBuffer overflow
//                   ) throws IOException;
// 
//   /**
//    * Decompress the in buffer to the out buffer.
//    * @param in the bytes to decompress
//    * @param out the decompressed bytes
//    * @throws IOException
//    */
//   void decompress(ByteBuffer in, ByteBuffer out) throws IOException;
// 
//   void reloadConfigurations(Configuration conf);
// }
// 
// Path: hive-dwrf/src/main/java/com/facebook/hive/orc/compression/ZlibCodec.java
// public class ZlibCodec implements CompressionCodec {
// 
//   private int compressionLevel;
// 
//   public ZlibCodec() {
//     compressionLevel = Deflater.DEFAULT_COMPRESSION;
//   }
// 
//   public ZlibCodec(Configuration conf) {
//     if (conf == null) {
//       compressionLevel = Deflater.DEFAULT_COMPRESSION;
//     } else {
//       compressionLevel = OrcConf.getIntVar(conf, OrcConf.ConfVars.HIVE_ORC_ZLIB_COMPRESSION_LEVEL);
//     }
//   }
// 
//   public void reloadConfigurations(Configuration conf) {
//     compressionLevel = OrcConf.getIntVar(conf, OrcConf.ConfVars.HIVE_ORC_ZLIB_COMPRESSION_LEVEL);
//   }
// 
//   @Override
//   public boolean compress(ByteBuffer in, ByteBuffer out,
//                           ByteBuffer overflow) throws IOException {
//     Deflater deflater = new Deflater(compressionLevel, true);
//     int length = in.remaining();
//     deflater.setInput(in.array(), in.arrayOffset() + in.position(), length);
//     deflater.finish();
//     int outSize = 0;
//     int offset = out.arrayOffset() + out.position();
//     while (!deflater.finished() && (length > outSize)) {
//       int size = deflater.deflate(out.array(), offset, out.remaining());
//       out.position(size + out.position());
//       outSize += size;
//       offset += size;
//       // if we run out of space in the out buffer, use the overflow
//       if (out.remaining() == 0) {
//         if (overflow == null) {
//           deflater.end();
//           return false;
//         }
//         out = overflow;
//         offset = out.arrayOffset() + out.position();
//       }
//     }
//     deflater.end();
//     return length > outSize;
//   }
// 
//   @Override
//   public void decompress(ByteBuffer in, ByteBuffer out) throws IOException {
//     Inflater inflater = new Inflater(true);
//     inflater.setInput(in.array(), in.arrayOffset() + in.position(),
//                       in.remaining());
//     while (!(inflater.finished() || inflater.needsDictionary() ||
//              inflater.needsInput())) {
//       try {
//         int count = inflater.inflate(out.array(),
//                                      out.arrayOffset() + out.position(),
//                                      out.remaining());
//         out.position(count + out.position());
//       } catch (DataFormatException dfe) {
//         throw new IOException("Bad compression data", dfe);
//       }
//     }
//     out.flip();
//     inflater.end();
//     in.position(in.limit());
//   }
// 
// }
// 
// Path: hive-dwrf/src/main/java/com/facebook/hive/orc/WriterImpl.java
// static class RowIndexPositionRecorder implements PositionRecorder {
//   private final OrcProto.RowIndexEntry.Builder builder;
// 
//   RowIndexPositionRecorder(OrcProto.RowIndexEntry.Builder builder) {
//     this.builder = builder;
//   }
// 
//   @Override
//   public void addPosition(long position) {
//     builder.addPositions(position);
//   }
// }


// Path: hive-dwrf/src/test/java/com/facebook/hive/orc/TestBitFieldReader.java
import static junit.framework.Assert.assertEquals;
import java.nio.ByteBuffer;
import com.facebook.hive.orc.compression.CompressionCodec;
import com.facebook.hive.orc.compression.ZlibCodec;
import org.apache.hadoop.hive.serde2.ReaderWriterProfiler;
import org.junit.Test;
import com.facebook.hive.orc.OrcProto.RowIndex;
import com.facebook.hive.orc.OrcProto.RowIndexEntry;
import com.facebook.hive.orc.WriterImpl.RowIndexPositionRecorder;

      out.getPosition(rowIndexPosition);
      rowIndex.addEntry(rowIndexEntry.build());
      rowIndexEntry.clear();
      // test runs, non-runs
      if (i < COUNT / 2) {
        out.write(i & 1);
      } else {
        out.write((i/3) & 1);
      }
    }
    out.flush();
    ByteBuffer inBuf = ByteBuffer.allocate(collect.buffer.size());
    collect.buffer.setByteBuffer(inBuf, 0, collect.buffer.size());
    inBuf.flip();
    BitFieldReader in = new BitFieldReader(InStream.create("test", inBuf, codec, 500));
    for(int i=0; i < COUNT; ++i) {
      int x = in.next();
      if (i < COUNT / 2) {
        assertEquals(i & 1, x);
      } else {
        assertEquals((i/3) & 1, x);
      }
    }
    in.loadIndeces(rowIndex.build().getEntryList(), 0);
    for(int i=COUNT-1; i >= 0; --i) {
      in.seek(i);
      int x = in.next();
      if (i < COUNT / 2) {
        assertEquals(i & 1, x);
      } else {
        assertEquals((i/3) & 1, x);