All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.splout.db.common.ThriftReader Maven / Gradle / Ivy

Go to download

Splout SQL is a read only, horizontally scalable and partitioned SQL database that plays well with Hadoop.

There is a newer version: 0.3.0
Show newest version
package com.splout.db.common;

/*
 * #%L
 * Splout SQL Server
 * %%
 * Copyright (C) 2012 Datasalt Systems S.L.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 * #L%
 */

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TIOStreamTransport;

/**
 * A simple class for reading Thrift objects (of a single type) from a file.
 * 
 * @author Joel Meyer - simplified & modified by us
 */
public class ThriftReader {

	/** File containing the objects. */
	protected final File file;

	/** For reading the file. */
	private BufferedInputStream bufferedIn;

	/** For reading the binary thrift objects. */
	private TBinaryProtocol binaryIn;

	/**
	 * Constructor.
	 * 
	 * @throws FileNotFoundException
	 */
	public ThriftReader(File file) throws FileNotFoundException {
		this.file = file;
		open();
	}

	/**
	 * Opens the file for reading. Must be called before {@link read()}.
	 */
	private void open() throws FileNotFoundException {
		bufferedIn = new BufferedInputStream(new FileInputStream(file), 2048);
		binaryIn = new TBinaryProtocol(new TIOStreamTransport(bufferedIn));
	}

	/**
	 * Checks if another objects is available by attempting to read another byte from the stream.
	 */
	public boolean hasNext() throws IOException {
		bufferedIn.mark(1);
		int val = bufferedIn.read();
		bufferedIn.reset();
		return val != -1;
	}

	/**
	 * Reads the next object from the file.
	 */
	@SuppressWarnings("rawtypes")
	public  T read(T t) throws IOException {
		try {
			t.read(binaryIn);
		} catch(TException e) {
			throw new IOException(e);
		}
		return (T) t;
	}

	/**
	 * Close the file.
	 */
	public ThriftReader close() throws IOException {
		bufferedIn.close();
		return this;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy