| package parquet.format; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.OutputStream; |
| |
| import org.apache.thrift.TBase; |
| import org.apache.thrift.TException; |
| import org.apache.thrift.protocol.TCompactProtocol; |
| import org.apache.thrift.transport.TIOStreamTransport; |
| |
| /** |
| * Utility to read/write metadata |
| * We use the TCompactProtocol to serialize metadata |
| * |
| * @author Julien Le Dem |
| * |
| */ |
| public class Util { |
| |
| public static void writePageHeader(PageHeader pageHeader, OutputStream to) throws IOException { |
| write(pageHeader, to); |
| } |
| |
| public static PageHeader readPageHeader(InputStream from) throws IOException { |
| return read(from, new PageHeader()); |
| } |
| |
| public static void writeFileMetaData(parquet.format.FileMetaData fileMetadata, OutputStream to) throws IOException { |
| write(fileMetadata, to); |
| } |
| |
| public static FileMetaData readFileMetaData(InputStream from) throws IOException { |
| return read(from, new FileMetaData()); |
| } |
| |
| private static TCompactProtocol protocol(OutputStream to) { |
| return new TCompactProtocol(new TIOStreamTransport(to)); |
| } |
| |
| private static TCompactProtocol protocol(InputStream from) { |
| return new TCompactProtocol(new TIOStreamTransport(from)); |
| } |
| |
| private static <T extends TBase<?,?>> T read(InputStream from, T tbase) throws IOException { |
| try { |
| tbase.read(protocol(from)); |
| return tbase; |
| } catch (TException e) { |
| throw new IOException("can not read " + tbase.getClass() + ": " + e.getMessage(), e); |
| } |
| } |
| |
| private static void write(TBase<?, ?> tbase, OutputStream to) throws IOException { |
| try { |
| tbase.write(protocol(to)); |
| } catch (TException e) { |
| throw new IOException("can not write " + tbase, e); |
| } |
| } |
| } |