| 1 | |
|
| 2 | |
package org.galagosearch.tupleflow; |
| 3 | |
|
| 4 | |
import java.io.DataOutputStream; |
| 5 | |
import java.io.File; |
| 6 | |
import java.io.IOException; |
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
public class FileOrderedWriter<T> implements Processor<T> { |
| 13 | |
String filename; |
| 14 | |
Order<T> order; |
| 15 | |
DataOutputStream dataStream; |
| 16 | |
ArrayOutput stream; |
| 17 | |
Processor<T> orderedWriter; |
| 18 | |
|
| 19 | 0 | public FileOrderedWriter(String filename, Order<T> order, boolean compressed) throws IOException { |
| 20 | 0 | this.filename = filename; |
| 21 | 0 | this.order = order; |
| 22 | |
|
| 23 | 0 | dataStream = StreamCreator.realOutputStream(filename); |
| 24 | 0 | if (compressed) { |
| 25 | 0 | stream = new ArrayOutput(new VByteOutput(dataStream)); |
| 26 | |
} else { |
| 27 | 0 | stream = new ArrayOutput(dataStream); |
| 28 | |
} |
| 29 | 0 | stream.writeString(order.getOrderedClass().getName()); |
| 30 | 0 | stream.writeStrings(order.getOrderSpec()); |
| 31 | 0 | this.orderedWriter = order.orderedWriter(stream); |
| 32 | 0 | } |
| 33 | |
|
| 34 | |
public FileOrderedWriter(String filename, Order<T> order) throws IOException { |
| 35 | 0 | this(filename, order, true); |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
public FileOrderedWriter(File file, Order<T> order) throws IOException { |
| 39 | 0 | this(file.getPath(), order, true); |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public Class<T> getInputClass() { |
| 43 | 0 | return order.getOrderedClass(); |
| 44 | |
} |
| 45 | |
|
| 46 | |
public void process(T object) throws IOException { |
| 47 | 0 | orderedWriter.process(object); |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
public void close() throws IOException { |
| 51 | 0 | orderedWriter.close(); |
| 52 | 0 | dataStream.close(); |
| 53 | 0 | } |
| 54 | |
} |