[ I/O ] Growing buffer

Discussion in 'Java' started by Philippe Poulard, Aug 1, 2005.

  1. hi,

    I'm looking for a snippet code that works almost like
    java.io.BufferedInputStream

    The difference is that I want to mark() the buffer without specifying
    the amount of data to keep : the internal buffer should grow if needed ;
    then, once reset() is invoked, I don't need to bufferize anymore once
    joining the end of the internal buffer.

    I'm sure somebody already did something like this :)
    --
    Cordialement,

    ///
    (. .)
    -----ooO--(_)--Ooo-----
    | Philippe Poulard |
    -----------------------
     
    Philippe Poulard, Aug 1, 2005
    #1
    1. Advertising

  2. On Mon, 01 Aug 2005 18:56:49 +0200, Philippe Poulard wrote:

    > I'm looking for a snippet code that works almost like
    > java.io.BufferedInputStream

    ....
    > I'm sure somebody already did something like this :)


    Good luck with that search. Report back what you find out,
    I'm sure others will be interested.

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Where No Fan Has Gone Before
     
    Andrew Thompson, Aug 1, 2005
    #2
    1. Advertising

  3. Philippe Poulard

    Harald Guest

    Philippe Poulard <> writes:

    > hi,
    >
    > I'm looking for a snippet code that works almost like
    > java.io.BufferedInputStream
    >
    > The difference is that I want to mark() the buffer without specifying
    > the amount of data to keep : the internal buffer should grow if needed
    > ;
    > then, once reset() is invoked, I don't need to bufferize anymore once
    > joining the end of the internal buffer.


    Out of the top of my head:

    private byte[] buffer = new byte[MIN_BYTES_TO_FETCH];
    private int start = 0; // start of valid data in buffer
    private int limit = 0; // end of valid data in buffer
    private boolean markAtive = false;

    // fill up buffer if nothing immediately available
    private void getSomeBytes(int maxCount) {
    if( !markActive ) start = 0;
    if( start+maxCount>buffer.length ) resizeBuffer(start+maxCount);
    int gotten = input.read(buffer, start, maxCount);
    if( gotten<0 ) return; // EOF
    limit = start+gotten;
    }
    // the one char version, everything else may follow from that
    public int read() {
    if( start>=limit ) getSomeBytes(MIN_BYTES_TO_FETCH);
    if( start>=limit ) return -1; // EOF
    return buffer[start++];
    }
    public void mark() {
    // the following gives us as much room as possible and we
    // don't need to store the mark, it is always 0
    System.arraycopy( start..limit ---> 0..(start-limit));
    markActive=true;
    }
    public void reset() {
    markActive = false;
    start = 0;
    }

    No guarantees, but feel free to transfer ¤1000 to my account when it
    works:)

    Harald.

    --
    ---------------------+---------------------------------------------
    Harald Kirsch (@home)|
    Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
     
    Harald, Aug 2, 2005
    #3
  4. Harald wrote:
    > Philippe Poulard <> writes:
    >
    >
    >>hi,
    >>
    >>I'm looking for a snippet code that works almost like
    >>java.io.BufferedInputStream
    >>
    >>The difference is that I want to mark() the buffer without specifying
    >>the amount of data to keep : the internal buffer should grow if needed
    >>;
    >>then, once reset() is invoked, I don't need to bufferize anymore once
    >>joining the end of the internal buffer.

    >
    >
    > Out of the top of my head:
    >
    > private byte[] buffer = new byte[MIN_BYTES_TO_FETCH];
    > private int start = 0; // start of valid data in buffer
    > private int limit = 0; // end of valid data in buffer
    > private boolean markAtive = false;
    >
    > // fill up buffer if nothing immediately available
    > private void getSomeBytes(int maxCount) {
    > if( !markActive ) start = 0;
    > if( start+maxCount>buffer.length ) resizeBuffer(start+maxCount);
    > int gotten = input.read(buffer, start, maxCount);
    > if( gotten<0 ) return; // EOF
    > limit = start+gotten;
    > }
    > // the one char version, everything else may follow from that
    > public int read() {
    > if( start>=limit ) getSomeBytes(MIN_BYTES_TO_FETCH);
    > if( start>=limit ) return -1; // EOF
    > return buffer[start++];
    > }
    > public void mark() {
    > // the following gives us as much room as possible and we
    > // don't need to store the mark, it is always 0
    > System.arraycopy( start..limit ---> 0..(start-limit));
    > markActive=true;
    > }
    > public void reset() {
    > markActive = false;
    > start = 0;
    > }
    >
    > No guarantees, but feel free to transfer ¤1000 to my account when it
    > works:)
    >
    > Harald.
    >


    thanks, but I just have did it !

    the entire code is too long to be displayed here, but when I will
    release my software, you'll may have a look at the code
    http://disc.inria.fr/perso/philippe.poulard/xml/reflex/

    in fact, I have had needed another I/O utility, an
    InputStreamAggregator, that merges several input streams, and I have
    used it for my ExtensibleBufferedInputStream, which is a FilterInputStream :
    when no mark is expected, the input is read as is, and as soon as a mark
    is expected (no matter the amount needed), I create a list where I put a
    buffer ; once the buffer is full, I replace it whith a
    ByteArrayInputStream and append a new buffer to the list ; when reset()
    is invoked, I merge all the ByteArrayInputStream and the current buffer
    in a single InputStreamAggregator that replace the input to read.

    Simple.

    --
    Cordialement,

    ///
    (. .)
    -----ooO--(_)--Ooo-----
    | Philippe Poulard |
    -----------------------
     
    Philippe Poulard, Aug 3, 2005
    #4
  5. Philippe Poulard

    Chris Head Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Philippe Poulard wrote:
    [snip]
    > in fact, I have had needed another I/O utility, an
    > InputStreamAggregator, that merges several input streams, and I have

    [snip]

    Hi,
    Try java.io.SequenceInputStream. No need to have written your own if Sun
    gives you one that works! The only thing that SequenceInputStream
    doesn't appear to support (which you might or might not need) is adding
    streams to the sequence once it's created. I guess you could work around
    this by replacing an existing SequenceInputStream with a new
    SequenceInputStream which wraps first the old SequenceInputStream and
    then the newly appended stream.

    Chris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (MingW32)

    iD8DBQFC8pXl6ZGQ8LKA8nwRAvFRAJ9y5i6yXraOyPiiZ9Kl8wvYdTv5IQCfa6F9
    RX9EPfP5FTxeFLt2RTgtOhk=
    =h4Y7
    -----END PGP SIGNATURE-----
     
    Chris Head, Aug 4, 2005
    #5
  6. Chris Head wrote:
    > -
    > Try java.io.SequenceInputStream. No need to have written your own if Sun
    > gives you one that works! The only thing that SequenceInputStream
    > doesn't appear to support (which you might or might not need) is adding
    > streams to the sequence once it's created. I guess you could work around
    > this by replacing an existing SequenceInputStream with a new
    > SequenceInputStream which wraps first the old SequenceInputStream and
    > then the newly appended stream.


    You can supply an Enumeration that lazily creates the sequence. One of
    the audio classes does that.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 5, 2005
    #6
  7. Philippe Poulard

    Chris Head Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Thomas Hawtin wrote:
    [snip]
    > You can supply an Enumeration that lazily creates the sequence. One of
    > the audio classes does that.
    >
    > Tom Hawtin


    Hey,
    Bl**dy brilliant. I never thought of building your own Enumeration that
    returns more things later than it would now...

    Chris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (MingW32)

    iD8DBQFC8qZP6ZGQ8LKA8nwRAryoAKDAJlQHHmpyqxY+SVOIpGZGMMNZ0ACaA/Y2
    BSriRMQQ3trud+K5aTbo1RM=
    =Zgz6
    -----END PGP SIGNATURE-----
     
    Chris Head, Aug 5, 2005
    #7
  8. Chris Head wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Philippe Poulard wrote:
    > [snip]
    >
    >>in fact, I have had needed another I/O utility, an
    >>InputStreamAggregator, that merges several input streams, and I have

    >
    > [snip]
    >
    > Hi,
    > Try java.io.SequenceInputStream. No need to have written your own if Sun
    > gives you one that works!


    Right, this is the same class :(
    I waste my (precious) time.

    Thanks for the tip.

    The only thing that SequenceInputStream
    > doesn't appear to support (which you might or might not need) is adding
    > streams to the sequence once it's created. I guess you could work around
    > this by replacing an existing SequenceInputStream with a new
    > SequenceInputStream which wraps first the old SequenceInputStream and
    > then the newly appended stream.


    As Thomas replied, it is not a problem.

    --
    Cordialement,

    ///
    (. .)
    -----ooO--(_)--Ooo-----
    | Philippe Poulard |
    -----------------------
     
    Philippe Poulard, Aug 5, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,412
    John Harrison
    Jun 21, 2004
  2. Replies:
    2
    Views:
    607
    sergejusz
    Mar 26, 2007
  3. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    415
    Neal Becker
    Jan 8, 2009
  4. Jef Driesen

    Buffer growing strategy?

    Jef Driesen, Sep 17, 2009, in forum: C Programming
    Replies:
    29
    Views:
    862
    Tim Rentsch
    Sep 30, 2009
  5. xingye
    Replies:
    9
    Views:
    276
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page