package org.broad.igv.util.stream;


import org.broad.tribble.util.SeekableHTTPStream;

import java.io.IOException;
import java.net.URL;

/**
 * Unfortunately the seekable stream classes exist for both Tribble and Picard, and we need both.  This class
 * is for use with Picard (SAM) and delegates all the work to a helper.
 *
 * @author jrobinso
 * @date Dec 23, 2011
 */

public class SeekablePicardStream extends net.sf.samtools.util.SeekableStream  {

    org.broad.tribble.util.SeekableStream tribbleStream;
    String source;

    public SeekablePicardStream(org.broad.tribble.util.SeekableStream tribbleStream, String source) {
        this.tribbleStream = tribbleStream;
        this.source = source;
    }

    @Override
    public long length() {
       return tribbleStream.length();
    }

    @Override
    public void seek(long l) throws IOException {
        tribbleStream.seek(l);
    }

    @Override
    public int read() throws IOException {
        return tribbleStream.read();
    }

    @Override
    public int read(byte[] bytes, int offset, int length) throws IOException {
        return tribbleStream.read(bytes,  offset,  length);
    }

    @Override
    public void close() throws IOException {
        tribbleStream.close();
    }

    @Override
    public boolean eof() throws IOException {
        return tribbleStream.eof();
    }

    @Override
    public String getSource() {
        return source;
    }
}

@@ -37,33 +37,31 @@
 %0Aimport 
-org.broad.tribble
+net.sf.samtools
 .util.Se
@@ -70,12 +70,8 @@
 able
-HTTP
 Stre
@@ -75,16 +75,52 @@
 tream;%0A%0A
+import java.io.BufferedInputStream;%0A
 import j
@@ -142,29 +142,8 @@
 ion;
-%0Aimport java.net.URL;
 %0A%0A/*
@@ -454,19 +454,112 @@
 eStream 
- 
 %7B%0A
+%0A    public static final int DEFAULT_BUFFER_SIZE = 1024000;%0A%0A    final private int bufferSize;
 %0A    org
@@ -589,31 +589,31 @@
 kableStream 
-tribble
+wrapped
 Stream;%0A    
@@ -627,16 +627,76 @@
 source;%0A
+    BufferedInputStream bufferedStream;%0A    long position;%0A%0A
 %0A    pub
@@ -786,24 +786,25 @@
 g source) %7B%0A
+%0A
         this
@@ -804,23 +804,23 @@
    this.
-tribble
+wrapped
 Stream =
@@ -861,24 +861,175 @@
 e = source;%0A
+        this.position = 0;%0A        this.bufferSize = DEFAULT_BUFFER_SIZE;%0A        bufferedStream = new BufferedInputStream(wrappedStream, bufferSize);%0A
     %7D%0A%0A    @
@@ -1067,38 +1067,39 @@
 () %7B%0A       
+ 
 return 
-tribble
+wrapped
 Stream.lengt
@@ -1150,17 +1150,24 @@
 ek(long 
-l
+position
 ) throws
@@ -1194,27 +1194,145 @@
    t
-ribbleStream.seek(l
+his.position = position;%0A        wrappedStream.seek(position);%0A        bufferedStream = new BufferedInputStream(wrappedStream, bufferSize
 );%0A 
@@ -1406,35 +1406,75 @@
     
-return tribbleStream.read()
+int b = bufferedStream.read();%0A        position++;%0A        return b
 ;%0A  
@@ -1470,32 +1470,33 @@
 eturn b;%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -1521,20 +1521,21 @@
 byte%5B%5D b
-ytes
+uffer
 , int of
@@ -1577,38 +1577,49 @@
 n %7B%0A        
-return tribble
+int nBytesRead = buffered
 Stream.read(
@@ -1623,22 +1623,22 @@
 ad(b
-ytes, 
+uffer,
  offset,
   le
@@ -1633,17 +1633,16 @@
  offset,
- 
  length)
@@ -1643,23 +1643,127 @@
 ength);%0A
+        if (nBytesRead %3E 0) %7B%0A            position += nBytesRead;%0A        %7D%0A        return nBytesRead;%0A
     %7D%0A%0A
+%0A
     @Ove
@@ -1821,23 +1821,23 @@
         
-tribble
+wrapped
 Stream.c
@@ -1926,23 +1926,23 @@
  return 
-tribble
+wrapped
 Stream.e

Implement buffering for SAM (Picard) access