code: private static final Scan truncateScan(Scan scan, byte[] rangeStart, byte[] rangeEnd) {
          byte[] scanStart = scan.getStartRow();
          byte[] scanEnd = scan.getStopRow();
  
          if (scanEnd.length > 0 && bytesCompare(scanEnd, rangeStart) <= 0) {
              // The entire scan range is before the entire cube key range
              return null;
          } else if (scanStart.length > 0 && bytesCompare(scanStart, rangeEnd) >= 0) {
              // The entire scan range is after the entire cube key range
              return null;
          } else {
              // Now we now that the scan range at least partially overlaps the cube key range.
              Scan truncated;
              try {
                  truncated = new Scan(scan); // make a copy, don't modify input scan
              } catch (IOException e) {
                  throw new RuntimeException(); // This is not plausible
              }
  
              if (scanStart.length == 0 || bytesCompare(rangeStart, scanStart) > 0) {
                  // The scan includes extra keys at the beginning that are not part of the cube. Move 
                  // the scan start point so that it only touches keys belonging to the cube.
                  truncated.setStartRow(rangeStart);
              }
              if (scanEnd.length == 0 || bytesCompare(rangeEnd, scanEnd) < 0) {
                  // The scan includes extra keys at the end that are not part of the cube. Move the
                  // scan end point so it only touches keys belonging to the cube.
                  truncated.setStopRow(rangeEnd);
              }
              return truncated;
          }
      }
docs: Given a scan and a key range, return a new Scan whose range is truncated to only include keys in
  that range. Returns null if the Scan does not overlap the given range.