Skip to content
This repository
Browse code

adding utility classe to completely hide Thrift

  • Loading branch information...
commit 10169144fbae98264f67b551793a514005107d0e 1 parent fa82762
Julien Le Dem julienledem authored

Showing 1 changed file with 61 additions and 0 deletions. Show diff stats Hide diff stats

  1. +61 0 src/main/java/parquet/format/Util.java
61 src/main/java/parquet/format/Util.java
... ... @@ -0,0 +1,61 @@
  1 +package parquet.format;
  2 +
  3 +import java.io.IOException;
  4 +import java.io.InputStream;
  5 +import java.io.OutputStream;
  6 +
  7 +import org.apache.thrift.TBase;
  8 +import org.apache.thrift.TException;
  9 +import org.apache.thrift.protocol.TCompactProtocol;
  10 +import org.apache.thrift.transport.TIOStreamTransport;
  11 +
  12 +/**
  13 + * Utility to read/write metadata
  14 + * We use the TCompactProtocol to serialize metadata
  15 + *
  16 + * @author Julien Le Dem
  17 + *
  18 + */
  19 +public class Util {
  20 +
  21 + public static void writePageHeader(PageHeader pageHeader, OutputStream to) throws IOException {
  22 + write(pageHeader, to);
  23 + }
  24 +
  25 + public static PageHeader readPageHeader(InputStream from) throws IOException {
  26 + return read(from, new PageHeader());
  27 + }
  28 +
  29 + public static void writeFileMetaData(parquet.format.FileMetaData fileMetadata, OutputStream to) throws IOException {
  30 + write(fileMetadata, to);
  31 + }
  32 +
  33 + public static FileMetaData readFileMetaData(InputStream from) throws IOException {
  34 + return read(from, new FileMetaData());
  35 + }
  36 +
  37 + private static TCompactProtocol protocol(OutputStream to) {
  38 + return new TCompactProtocol(new TIOStreamTransport(to));
  39 + }
  40 +
  41 + private static TCompactProtocol protocol(InputStream from) {
  42 + return new TCompactProtocol(new TIOStreamTransport(from));
  43 + }
  44 +
  45 + private static <T extends TBase<?,?>> T read(InputStream from, T tbase) throws IOException {
  46 + try {
  47 + tbase.read(protocol(from));
  48 + return tbase;
  49 + } catch (TException e) {
  50 + throw new IOException("can not read " + tbase.getClass() + ": " + e.getMessage(), e);
  51 + }
  52 + }
  53 +
  54 + private static void write(TBase<?, ?> tbase, OutputStream to) throws IOException {
  55 + try {
  56 + tbase.write(protocol(to));
  57 + } catch (TException e) {
  58 + throw new IOException("can not write " + tbase, e);
  59 + }
  60 + }
  61 +}

0 comments on commit 1016914

Please sign in to comment.
Something went wrong with that request. Please try again.