[ I/O ] Growing buffer

  • Thread starter Philippe Poulard
  • Start date
P

Philippe Poulard

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 |
-----------------------
 
A

Andrew Thompson

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.
 
H

Harald

Philippe Poulard said:
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.
 
P

Philippe Poulard

Harald said:
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 |
-----------------------
 
C

Chris Head

-----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-----
 
T

Thomas Hawtin

Chris said:
-
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
 
C

Chris Head

-----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-----
 
P

Philippe Poulard

Chris said:
-----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 |
-----------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top