Ejemplo n.º 1
0
 /**
  * @return Byte array read from the stream.
  * @throws IOException
  */
 private byte[] readByteArray(final InputStream in) throws IOException {
   byte[] intArray = new byte[Bytes.SIZEOF_INT];
   IOUtils.readFully(in, intArray);
   int length = Bytes.toInt(intArray);
   byte[] bytes = new byte[length];
   IOUtils.readFully(in, bytes);
   return bytes;
 }
Ejemplo n.º 2
0
 protected Cell parseCell() throws IOException {
   byte[] row = readByteArray(this.in);
   byte[] family = readByteArray(in);
   byte[] qualifier = readByteArray(in);
   byte[] longArray = new byte[Bytes.SIZEOF_LONG];
   IOUtils.readFully(this.in, longArray);
   long timestamp = Bytes.toLong(longArray);
   byte type = (byte) this.in.read();
   byte[] value = readByteArray(in);
   byte[] tags = readByteArray(in);
   // Read memstore version
   byte[] memstoreTSArray = new byte[Bytes.SIZEOF_LONG];
   IOUtils.readFully(this.in, memstoreTSArray);
   long memstoreTS = Bytes.toLong(memstoreTSArray);
   return CellUtil.createCell(row, family, qualifier, timestamp, type, value, tags, memstoreTS);
 }
Ejemplo n.º 3
0
 @SuppressWarnings("deprecation")
 public void writeLogs(String taskId, OutputStream output) {
   try (FileInputStream input = new FileInputStream(getLogFile(taskId))) {
     DataInputStream dataInput = new DataInputStream(input);
     output.write("[\n".getBytes());
     byte[] buffer = new byte[4 * 1024];
     boolean needComma = false;
     try {
       while (true) {
         IOUtils.skip(input, 1);
         String line = dataInput.readLine();
         if (line == null || line.isEmpty()) {
           break;
         }
         int length = Integer.parseInt(line);
         if (length > buffer.length) {
           buffer = new byte[length];
         }
         IOUtils.readFully(input, buffer, 0, length);
         if (needComma) {
           output.write(",\n".getBytes());
         } else {
           needComma = true;
         }
         output.write(buffer, 0, length);
       }
     } catch (EOFException ignored) {
       // EOF reached
     }
     output.write("]\n".getBytes());
   } catch (Throwable throwable) {
     Throwables.propagate(throwable);
   }
 }
Ejemplo n.º 4
0
 public Parser(InputStream in) throws IOException {
   if (in instanceof FileInputStream) {
     // fast shorthand for normal files
     FileChannel channel = ((FileInputStream) in).getChannel();
     buf =
         channel
             .map(FileChannel.MapMode.READ_ONLY, 0, channel.size())
             .order(ByteOrder.LITTLE_ENDIAN);
   } else {
     // slower default for others
     IOUtils.readFully(in, tag);
     byte[] tmp = new byte[4];
     IOUtils.readFully(in, tmp);
     int l = ByteBuffer.wrap(tmp).order(ByteOrder.LITTLE_ENDIAN).getInt();
     if (l < 0 || l + 8 < 0) throw new IOException("File is too large");
     buf = ByteBuffer.allocate(l + 8).order(ByteOrder.LITTLE_ENDIAN);
     buf.clear();
     buf.put(tag);
     buf.put(tmp);
     buf.put(IOUtils.toByteArray(in, l));
     buf.flip();
   }
 }
Ejemplo n.º 5
0
    @Override
    protected Cell parseCell() throws IOException {
      if (this.decryptor == null) {
        return super.parseCell();
      }
      int ivLength = 0;

      ivLength = StreamUtils.readRawVarint32(in);

      // TODO: An IV length of 0 could signify an unwrapped cell, when the
      // encoder supports that just read the remainder in directly

      if (ivLength != this.iv.length) {
        throw new IOException("Incorrect IV length: expected=" + iv.length + " have=" + ivLength);
      }
      IOUtils.readFully(in, this.iv);

      int codedLength = StreamUtils.readRawVarint32(in);
      byte[] codedBytes = new byte[codedLength];
      IOUtils.readFully(in, codedBytes);

      decryptor.setIv(iv);
      decryptor.reset();

      InputStream cin = decryptor.createDecryptionStream(new ByteArrayInputStream(codedBytes));

      // TODO: Add support for WAL compression

      int keylength = StreamUtils.readRawVarint32(cin);
      int vlength = StreamUtils.readRawVarint32(cin);
      int tagsLength = StreamUtils.readRawVarint32(cin);
      int length = 0;
      if (tagsLength == 0) {
        length = KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE + keylength + vlength;
      } else {
        length = KeyValue.KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE + keylength + vlength + tagsLength;
      }

      byte[] backingArray = new byte[length];
      int pos = 0;
      pos = Bytes.putInt(backingArray, pos, keylength);
      pos = Bytes.putInt(backingArray, pos, vlength);

      // Row
      int elemLen = StreamUtils.readRawVarint32(cin);
      pos = Bytes.putShort(backingArray, pos, (short) elemLen);
      IOUtils.readFully(cin, backingArray, pos, elemLen);
      pos += elemLen;
      // Family
      elemLen = StreamUtils.readRawVarint32(cin);
      pos = Bytes.putByte(backingArray, pos, (byte) elemLen);
      IOUtils.readFully(cin, backingArray, pos, elemLen);
      pos += elemLen;
      // Qualifier
      elemLen = StreamUtils.readRawVarint32(cin);
      IOUtils.readFully(cin, backingArray, pos, elemLen);
      pos += elemLen;
      // Remainder
      IOUtils.readFully(cin, backingArray, pos, length - pos);
      return new KeyValue(backingArray, 0, length);
    }