read a row of data from a compressed integer point map

@param rn
@param adrows
@param outFile
@return the ByteBuffer containing the data
@throws IOException
328590
private void readCompressedIntegerRowByNumber( ByteBuffer rowdata, int rn, long[] adrows, RandomAccessFile thefile )
            throws IOException, DataFormatException {
        int offset = (int) (adrows[rn + 1] - adrows[rn]);

        thefile.seek(adrows[rn]);
        /*
         * Read how many bytes the values are ex 1 => if encoded: 1 byte for the value and one byte
         * for the count = 2 2 => if encoded: 2 bytes for the value and one byte for the count = 3
         * etc... etc
         */
        int bytespervalue = (thefile.read() & 0xff);
        ByteBuffer cell = ByteBuffer.allocate(bytespervalue);
        int cellValue = 0;

        /* Create the buffer in which read the compressed row */
        byte[] tmp = new byte[offset - 1];
        thefile.read(tmp);
        ByteBuffer tmpBuffer = ByteBuffer.wrap(tmp);
        tmpBuffer.order(ByteOrder.nativeOrder());

        /*
         * Create the buffer in which read the decompressed row. The final decompressed row will
         * always contain 4-byte integer values
         */
        if ((offset - 1) == (bytespervalue * fileWindow.getCols())) {
            /* There is no compression in this row */
            for( int i = 0; i < offset - 1; i = i + bytespervalue ) {
                /* Read the value */
                tmpBuffer.get(cell.array());

                /*
                 * Integers can be of 1, 2, or 4 bytes. As rasterBuffer expects 4 byte integers we
                 * need to pad them with 0's. The order of the padding is determined by the
                 * ByteOrder of the buffer.
                 */
                if (bytespervalue == 1) {
                    cellValue = (cell.get(0) & 0xff);
                } else if (bytespervalue == 2) {
                    cellValue = cell.getShort(0);
                } else if (bytespervalue == 4) {
                    cellValue = cell.getInt(0);
                }
                // if (logger.isDebugEnabled()) logger.debug("tmpint=" + tmpint
                // );
                rowdata.putInt(cellValue);
            }
        } else {
            /*
             * If the row is compressed, then the values appear in pairs (like couples a party). The
             * couple is composed of the count and the value value (WARNING: this can be more than
             * one byte). Therefore, knowing the length of the compressed row we can calculate the
             * number of couples.
             */
            int couples = (offset - 1) / (1 + bytespervalue);

            for( int i = 0; i < couples; i++ ) {
                /* Read the count of values */
                int count = (tmpBuffer.get() & 0xff);

                /* Read the value */
                tmpBuffer.get(cell.array());

                /*
                 * Integers can be of 1, 2, or 4 bytes. As rasterBuffer expects 4 byte integers we
                 * need to pad them with 0's. The order of the padding is determined by the
                 * ByteOrder of the buffer.
                 */
                if (bytespervalue == 1) {
                    cellValue = (cell.get(0) & 0xff);
                } else if (bytespervalue == 2) {
                    cellValue = cell.getShort(0);
                } else if (bytespervalue == 4) {
                    cellValue = cell.getInt(0);
                }
                /*
                 * Now write the cell value the required number of times to the raster row data
                 * buffer.
                 */
                for( int j = 0; j < count; j++ ) {
                    // // if (logger.isDebugEnabled()) logger.debug(" " +
                    // tmpint);
                    rowdata.putInt(cellValue);
                }
            }
        }
    }
619868
private static Matrix readSparseSVDLIBCbinary(File matrix, Type matrixType,
                                                  boolean transposeOnRead) 
            throws IOException {
        DataInputStream dis = new DataInputStream(
            new BufferedInputStream(new FileInputStream(matrix)));

        int rows = dis.readInt();
        int cols = dis.readInt();
        int nz = dis.readInt();
        MATRIX_IO_LOGGER.fine(
            String.format("Creating %s matrix %d rows, %d cols, %d nz%n",
                          ((transposeOnRead) ? "transposed" : ""),
                          rows, cols, nz));
        Matrix m = null;

        // Special case for reading transposed data.  This avoids the log(n)
        // overhead from resorting the row data for the matrix, which can be
        // significant in large matrices.
        if (transposeOnRead) {
            SparseDoubleVector[] rowArr = new SparseDoubleVector[cols];
            int entriesSeen = 0;
            int col = 0;
            int curRow = 0;
            for (; entriesSeen < nz; ++col) {
                int nzInCol = dis.readInt();
                int[] indices = new int[nzInCol];
                double[] vals = new double[nzInCol];
                for (int i = 0; i < nzInCol; ++i, ++entriesSeen) {
                    indices[i] = dis.readInt();
                    vals[i] = dis.readFloat();
                }
                SparseDoubleVector rowVec = 
                    new CompactSparseVector(indices, vals, rows);
                rowArr[curRow] = rowVec;
                ++curRow;                
            }
            m = Matrices.asSparseMatrix(Arrays.asList(rowArr));
        }
        else {
            m = new SparseHashMatrix(rows, cols);
            int entriesSeen = 0;
            int col = 0;
            for (; entriesSeen < nz; ++col) {
                int nzInCol = dis.readInt();
                for (int i = 0; i < nzInCol; ++i, ++entriesSeen) {
                    m.set(dis.readInt(), col, dis.readFloat());
                }
            }
        }
        dis.close();

        MATRIX_IO_LOGGER.fine("Completed loading matrix");
        return m;
    }
1323544
private int readCorner3(int numRows, int numColumns) {
    int currentByte = 0;
    if (readModule(numRows - 1, 0, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(numRows - 1, numColumns - 1, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(0, numColumns - 3, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(0, numColumns - 2, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(0, numColumns - 1, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(1, numColumns - 3, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(1, numColumns - 2, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(1, numColumns - 1, numRows, numColumns)) {
      currentByte |= 1;
    }
    return currentByte;
  }
795212
public void dumpRow(PrintStream out) {
        for (int i = 0; i < columnNames.size(); i++) {
            out.print("Column ");
            out.print(i);
            out.print(" ");
            out.print(columnNames.get(columnIndex));
            out.print(" : ");
            try {
                out.println(line[i]);
            } catch (ArrayIndexOutOfBoundsException aioobe) {
                out.println("Unable to get cell " + i + " of this line");
            }
        }
    }
1206401
public static ExportRow decodeRow(ExportRow previous, int partition, long startTS, byte[] rowData) throws IOException {
        ByteBuffer bb = ByteBuffer.wrap(rowData);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        return decodeRow(previous, partition, startTS, bb);
    }
785154
public static void getScreenShotABGR(ByteBuffer bgraBuf, BufferedImage out) {
        WritableRaster wr = out.getRaster();
        DataBufferByte db = (DataBufferByte) wr.getDataBuffer();

        byte[] cpuArray = db.getData();

        // copy native memory to java memory
        bgraBuf.clear();
        bgraBuf.get(cpuArray);
        bgraBuf.clear();

        int width = wr.getWidth();
        int height = wr.getHeight();

        // flip the components the way AWT likes them
        for (int y = 0; y < height / 2; y++) {
            for (int x = 0; x < width; x++) {
                int inPtr = (y * width + x) * 4;
                int outPtr = ((height - y - 1) * width + x) * 4;

                byte b1 = cpuArray[inPtr + 0];
                byte g1 = cpuArray[inPtr + 1];
                byte r1 = cpuArray[inPtr + 2];
                byte a1 = cpuArray[inPtr + 3];

                byte b2 = cpuArray[outPtr + 0];
                byte g2 = cpuArray[outPtr + 1];
                byte r2 = cpuArray[outPtr + 2];
                byte a2 = cpuArray[outPtr + 3];

                cpuArray[outPtr + 0] = a1;
                cpuArray[outPtr + 1] = b1;
                cpuArray[outPtr + 2] = g1;
                cpuArray[outPtr + 3] = r1;

                cpuArray[inPtr + 0] = a2;
                cpuArray[inPtr + 1] = b2;
                cpuArray[inPtr + 2] = g2;
                cpuArray[inPtr + 3] = r2;
            }
        }
    }
358093
public ZMatrixRMaj readZDRM(int numRows, int numCols) throws IOException {

        ZMatrixRMaj A = new ZMatrixRMaj(numRows,numCols);

        int wordsCol = numCols*2;

        for( int i = 0; i < numRows; i++ ) {
            List<String> words = extractWords();
            if( words == null )
                throw new IOException("Too few rows found. expected "+numRows+" actual "+i);

            if( words.size() != wordsCol )
                throw new IOException("Unexpected number of words in column. Found "+words.size()+" expected "+wordsCol);
            for( int j = 0; j < wordsCol; j += 2 ) {

                double real = Double.parseDouble(words.get(j));
                double imaginary = Double.parseDouble(words.get(j+1));

                A.set(i, j, real, imaginary);
            }
        }

        return A;
    }
1351647
private void readUncompressedIntegerRowByNumber( ByteBuffer rowdata, int rn, RandomAccessFile thefile ) throws IOException,
            DataFormatException {
        int cellValue = 0;
        ByteBuffer cell = ByteBuffer.allocate(rasterMapType);

        /* The number of bytes that are inside a row in the file. */
        int filerowsize = fileWindow.getCols() * rasterMapType;

        /* Position the file pointer to read the row */
        thefile.seek((rn * filerowsize));

        /* Read the row of data from the file */
        ByteBuffer tmpBuffer = ByteBuffer.allocate(filerowsize);
        thefile.read(tmpBuffer.array());

        /*
         * Transform the rasterMapType-size-values to a standard 4 bytes integer value
         */
        while( tmpBuffer.hasRemaining() ) {
            // read the value
            tmpBuffer.get(cell.array());

            /*
             * Integers can be of 1, 2, or 4 bytes. As rasterBuffer expects 4 byte integers we need
             * to pad them with 0's. The order of the padding is determined by the ByteOrder of the
             * buffer.
             */
            if (rasterMapType == 1) {
                cellValue = (cell.get(0) & 0xff);
            } else if (rasterMapType == 2) {
                cellValue = cell.getShort(0);
            } else if (rasterMapType == 4) {
                cellValue = cell.getInt(0);
            }
            // if (logger.isDebugEnabled()) logger.debug("tmpint=" + cellValue
            // );
            rowdata.putInt(cellValue);
        }
    }
5012
public List<MapRow> readTable(TableReader reader) throws IOException
   {
      reader.read();
      return reader.getRows();
   }
450271
public TableReader read() throws IOException
   {
      int tableHeader = m_stream.readInt();
      if (tableHeader != 0x39AF547A)
      {
         throw new IllegalArgumentException("Unexpected file format");
      }

      int recordCount = m_stream.readInt();
      for (int loop = 0; loop < recordCount; loop++)
      {
         int rowMagicNumber = m_stream.readInt();
         if (rowMagicNumber != rowMagicNumber())
         {
            throw new IllegalArgumentException("Unexpected file format");
         }

         // We use a LinkedHashMap to preserve insertion order in iteration
         // Useful when debugging the file format.
         Map<String, Object> map = new LinkedHashMap<String, Object>();

         if (hasUUID())
         {
            readUUID(m_stream, map);
         }

         readRow(m_stream, map);

         SynchroLogger.log("READER", getClass(), map);

         m_rows.add(new MapRow(map));
      }

      int tableTrailer = m_stream.readInt();
      if (tableTrailer != 0x6F99E416)
      {
         throw new IllegalArgumentException("Unexpected file format");
      }

      postTrailer(m_stream);

      return this;
   }
410337
public static void writeBuffer(DataOutput out, ByteBuffer buffer) throws IOException {
        if (buffer.hasArray()) {
            out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
            buffer.position(buffer.limit());
        } else {
            final byte[] array = new byte[buffer.remaining()];
            buffer.get(array);
            assert buffer.remaining() == 0 && buffer.position() == buffer.limit();
            out.write(array);
        }
    }
205726
public static Output<CharBuffer>
            from(String data, boolean endOfRecord) {
        return new Output<>(ManagedBuffer.wrap(
            CharBuffer.wrap(data)), false, endOfRecord);
    }
92888
function readNextRow()
  {
    // if we have a row buffer, process it
    if (rowBuffer)
    {
      processRowBuffer();
    }
    else
    {
      // subscribe to the result stream's 'data' and 'close' events
      resultStream.on('data', onResultStreamData);
      resultStream.on('close', onResultStreamClose);

      // issue a request to fetch the next chunk in the result stream
      resultStream.read();
    }
  }
389684
private int readCorner2(int numRows, int numColumns) {
    int currentByte = 0;
    if (readModule(numRows - 3, 0, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(numRows - 2, 0, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(numRows - 1, 0, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(0, numColumns - 4, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(0, numColumns - 3, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(0, numColumns - 2, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(0, numColumns - 1, numRows, numColumns)) {
      currentByte |= 1;
    }
    currentByte <<= 1;
    if (readModule(1, numColumns - 1, numRows, numColumns)) {
      currentByte |= 1;
    }
    return currentByte;
  }
1219257
public int read(long position, byte[] buffer, int offset, int length,
      ReadOptions options)
    throws IOException {      
    // sanity checks
    dfsClient.checkOpen();
    if (closed) {
      throw new IOException("Stream closed");
    }
    DFSClient.dfsInputStreamfailures.set(0);
			long start = System.currentTimeMillis();
    long filelen = getFileLength();
    if ((position < 0) || (position >= filelen)) {
      return -1;
    }
    int realLen = length;
    if ((position + length) > filelen) {
      realLen = (int)(filelen - position);
    }
    
    DFSReadProfilingData pData = DFSClient.getDFSReadProfilingData();

    // determine the block and byte range within the block
    // corresponding to position and realLen
    List<LocatedBlock> blockRange = getBlockRange(position, realLen);
    
    if (!tryPreadFromLocal(blockRange, position, buffer, offset, length,
        realLen, start)) {
      // non-local or multiple block read.
      int remaining = realLen;
      for (LocatedBlock blk : blockRange) {
        long targetStart = position - blk.getStartOffset();
        long bytesToRead = Math.min(remaining, blk.getBlockSize() - targetStart);
        if (dfsClient.allowParallelReads && dfsClient.parallelReadsThreadPool != null) {
          fetchBlockByteRangeSpeculative(blk, targetStart,
                              targetStart + bytesToRead - 1, buffer, offset,
                              options);
        } else {
          if (pData != null) {
            cliData = new FSClientReadProfilingData();
            pData.addDFSClientReadProfilingData(cliData);
            cliData.startRead();
          }
          
          fetchBlockByteRange(blk, targetStart,
                              targetStart + bytesToRead - 1, buffer, offset,
                              options);
          
          if (pData != null) {
            cliData.endRead();
          }
        }
        remaining -= bytesToRead;
        position += bytesToRead;
        offset += bytesToRead;
      }
      assert remaining == 0 : "Wrong number of bytes read.";
    }
    
    if (dfsClient.stats != null) {
      dfsClient.stats.incrementBytesRead(realLen);
    }
    long timeval = System.currentTimeMillis() - start;
    dfsClient.metrics.incPreadTime(timeval);
    dfsClient.metrics.incPreadSize(realLen);
    dfsClient.metrics.incPreadOps();
    return realLen;
  }
313192
@Override
	public String readRecord(String reusable, byte[] bytes, int offset, int numBytes) throws IOException {
		//Check if \n is used as delimiter and the end of this line is a \r, then remove \r from the line
		if (this.getDelimiter() != null && this.getDelimiter().length == 1
				&& this.getDelimiter()[0] == NEW_LINE && offset + numBytes >= 1
				&& bytes[offset + numBytes - 1] == CARRIAGE_RETURN){
			numBytes -= 1;
		}

		return new String(bytes, offset, numBytes, this.charsetName);
	}
285483
public IoBuffer fetchOutNetBuffer() {
        IoBufferEx answer = outNetBuffer;
        if (answer == null) {
            return (IoBuffer) emptyBuffer;
        }

        outNetBuffer = null;
        return (IoBuffer) answer.shrink(allocator);
    }