• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mark the current position in Output Stream

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a requirement in which I have to go back in the Output Stream to a particular position and then write some data there. Input Stream have mark() and reset() methods but I did't find anything like this in Output Stream. Can anyone help?

Regards,
Anuj
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't anything like that in the API. However, you can always write it yourself. Here is one approach
- extend FilterOutputStream; that way your class will act as a wrapper around any other OutputStream.
- keep a buffer of bytes written to your stream but not to the wrapped OutputStream.
- when calling flush() you clear the buffer and write its contents to the wrapped OutputStream.
- when setting the "savepoint" you first flush() your OutputStream so the buffer is cleared.
- when resetting to the savepoint you simply clear the buffer

You can perhaps optimize the code by only using the buffer if you have a savepoint set. flush() removes the savepoint. I'd use a ByteArrayOutputStream or a List<Byte> for the buffer.
 
I don't even know how to spell CIA. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic