Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > [ I/O ] Growing buffer

Reply
Thread Tools

[ I/O ] Growing buffer

 
 
Philippe Poulard
Guest
Posts: n/a
 
      08-01-2005
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 |
-----------------------
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      08-01-2005
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
 
Reply With Quote
 
 
 
 
Harald
Guest
Posts: n/a
 
      08-02-2005
Philippe Poulard <(E-Mail Removed)> 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
 
Reply With Quote
 
Philippe Poulard
Guest
Posts: n/a
 
      08-03-2005
Harald wrote:
> Philippe Poulard <(E-Mail Removed)> 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 |
-----------------------
 
Reply With Quote
 
Chris Head
Guest
Posts: n/a
 
      08-04-2005
-----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)

iD8DBQFC8pXl6ZGQ8LKA8nwRAvFRAJ9y5i6yXraOyPiiZ9Kl8w vYdTv5IQCfa6F9
RX9EPfP5FTxeFLt2RTgtOhk=
=h4Y7
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      08-04-2005
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/
 
Reply With Quote
 
Chris Head
Guest
Posts: n/a
 
      08-04-2005
-----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+SVOIpG ZGMMNZ0ACaA/Y2
BSriRMQQ3trud+K5aTbo1RM=
=Zgz6
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Philippe Poulard
Guest
Posts: n/a
 
      08-05-2005
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 |
-----------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Buffer growing strategy? Jef Driesen C Programming 29 09-30-2009 08:29 AM
buffer creates only read-only buffer? Neal Becker Python 0 01-08-2009 01:58 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM



Advertisments