• 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

StringBuffer size

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I predict the size of a StringBuffer Object based on the number of characters that it contains? Thanks a lot.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ibrahim
The StringBuffer has a length and a capacity method. Are those what you're looking for? Or, if you need a specific capacity you can use the ensureCapacity method to set a minmum capacity for it.
hope that helps
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bascially I'm trying to ensure that the amount that it consumes in memory doesn't exceed a certain amount. Do you know if this is possible? Besides using the capacity method, because when if the capacity is > than MAX_MEMORY_AMOUNT, then it will already have exceeded that amount and I want to prevent that. Thanks in advance.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you just need to write the proper code to do the checking and flushing that you want to do. If you keep adding characters to a StringBuffer, it keeps growing (until the JVM runs out of memory).
I'm not aware of a way to limit the size of a StringBuffer that is built into the standard API. If such a thing were available, what should it do when you try to add data past the limit? Throw a StringBufferOutOfMemeoryException? You could probably code such a thing for yourself.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic