• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to manipulate InputStream and extract content

 
Ranch Hand
Posts: 69
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have an io InputStream containing a very very long string and I want to extract the middle of it and put in another InputStream.  I don't want to convert the whole thing into a string since it takes too much memory..  Suppose the original stream contains stuff like "<start>VERY VERY LONG CONTENT<end>" and I want to extract "VERY VERY LONG CONTENT" from this InputStream (and by end I want a InputStream not a string).  How should I do without utilizing too much memory ?
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading this post under the assumption that you wanted to write the chosen data into an OutputStream. But then I read more carefully. Really, you want it to be in another InputStream? I don't understand that because it's already in an InputStream. Maybe you could clarify your requirements a bit and explain what they are for?
 
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Paul. In most cases, piping data from one InputStream to another isn't needed or sensible.

Just read the data from the original stream, and simply stop reading when you reach the end tag. You can easily do that using a Scanner.

Depending on the data format, you could also used specialized APIs (such as a SAX or StAX parser if the data is XML).
 
David Mutansan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I will try Scanner.
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider using a regular expression to match the text you want to read. Scanners can use such regular expressions with methods like findAll(), findInLine(), and findWithinHorizon()
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but keep in mind that it only works for input that is truly regular.

If you are trying to find nested tags, as is often the case with XML, you can not generally use regular expressions.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . If you are trying to find nested tags . . . you can not generally use regular expressions.

Good point. No, regular expressions will not find nested tags correctly. That is not a regular grammar.
 
Paul Clapham
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this process is all in aid of "not using too much memory", I would recommend doing something ordinary and see if it works. Java allows you to use 4 gigabytes of memory, or maybe more, and chances are that a computer has that much usable memory these days. Let's not go down the premature optimization road.
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic