2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
5 package com
.yahoo
.pig
.builtin
;
7 import java
.io
.BufferedInputStream
;
8 import java
.io
.BufferedOutputStream
;
9 import java
.io
.DataInputStream
;
10 import java
.io
.DataOutputStream
;
11 import java
.io
.IOException
;
12 import java
.io
.InputStream
;
13 import java
.io
.OutputStream
;
14 import java
.util
.Iterator
;
16 import com
.yahoo
.pig
.StorageFunc
;
17 import com
.yahoo
.pig
.PigServer
;
18 import com
.yahoo
.pig
.data
.Tuple
;
19 import com
.yahoo
.pig
.impl
.io
.FileLocalizer
;
20 import com
.yahoo
.pig
.impl
.io
.InputStreamPosition
;
22 public class BinStorage
extends StorageFunc
{
23 Iterator
<Tuple
> i
= null;
24 DataInputStream in
= null;
25 InputStreamPosition posInputStream
= null;
26 InputStream fsis
= null;
27 long end
= Long
.MAX_VALUE
;
30 * Simple binary nested reader format
35 public Tuple
getNext() throws IOException
{
36 if (fsis
== null || (posInputStream
!= null && posInputStream
.getPosition() > end
)) {
40 // skip to next record
43 if(b
!= Tuple
.RECORD_1
&& b
!= -1) {
46 if(b
== -1) return null;
48 if(b
!= Tuple
.RECORD_2
&& b
!= -1) {
51 if(b
== -1) return null;
53 if(b
!= Tuple
.RECORD_3
&& b
!= -1) {
56 if(b
== -1) return null;
59 Tuple t
= new Tuple();
64 public void bindTo(InputStream is
, long offset
, long end
) throws IOException
{
65 this.posInputStream
= new InputStreamPosition(is
, offset
);
66 this.in
= new DataInputStream(new BufferedInputStream(posInputStream
));
70 // Since we are not block aligned we throw away the first
71 // record and count on a different instance to read it
78 DataOutputStream out
= null;
81 public void bindTo(OutputStream os
) throws IOException
{
82 this.out
= new DataOutputStream(new BufferedOutputStream(os
));
86 public void done() throws IOException
{
91 public void putNext(Tuple t
) throws IOException
{
92 out
.write(Tuple
.RECORD_1
);
93 out
.write(Tuple
.RECORD_2
);
94 out
.write(Tuple
.RECORD_3
);