@Override
  public void write(DataOutput out) throws IOException {
    Buffer buffer = page.buffer();
    out.writeInt(buffer.limit());
    out.write(buffer.array(), buffer.offset(), buffer.limit());

    List<String> stringReferences = page.getStringReferences();
    WritableUtils.writeVInt(out, stringReferences.size());
    for (String s : stringReferences) {
      out.writeUTF(s);
    }
  }
  @Override
  public void readFields(DataInput in) throws IOException {
    int bufferSize = in.readInt();
    byte[] bytes = new byte[bufferSize]; // TODO usa buffer allocator?
    in.readFully(bytes, 0, bufferSize);
    Buffer buffer = Buffer.wrap(bytes);

    int stringCount = WritableUtils.readVInt(in);
    List<String> strings = new ArrayList<String>(stringCount);
    for (int i = 0; i < stringCount; i++) {
      strings.add(in.readUTF());
    }

    Page newPage = Page.wrap(buffer);
    newPage.setStringReferences(strings);
    if (page != null) {
      page.release();
    }
    page = newPage;
  }
예제 #3
0
 public Buffer poll() {
   if (current == null) {
     throw new IllegalStateException("nextFile() must be called before poll()");
   }
   Buffer buffer = allocator.allocate();
   try {
     int n = current.read(buffer.array(), buffer.offset(), buffer.capacity());
     if (n < 0) {
       return null;
     }
     buffer.limit(n);
     Buffer b = buffer;
     buffer = null;
     return b;
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   } finally {
     if (buffer != null) {
       buffer.release();
       buffer = null;
     }
   }
 }