java - Avoid creating 'new' String objects when converting a byte[] to String using a specific charset -


I am reading from a binary file and want to convert the byte to a US ASCII wire. Is there a way to do this without calling new on new to avoid becoming more economically identical string objects in string string literally pool is? I am thinking that probably is not possible because it is not possible to use double code for the string object. Is it true?

  Private string throws the next string (datainputstream disk, int size) IOException {byte [] byteholder = new byte [size]; Dis.read (bytesHolder); Return the new string (bytesholder, charset forename ("US-ASCII")). Trim ();  

You must keep caching mapping byte arrays in strings, find again a new string Cache for any similar values ​​before making it

You can do the existing string of interns with intern () as Ishai Posted by - which will not prevent you from more strings , But it will do all this but for the first time (for any four sequence) it was very short, on the other hand, all this specific string would actually live for a very long time.

You can add a map & lt; String, string & gt;

tmp = new string (byte holder, charset name ("US-ASCII")). Trim (); letter cache = Cache.get (tmp); if (cached == blank) {cached = tmp; cache. Input (tmp, tmp);} return cached;

And can end with an LRU cache so that it will keep the most recently received wires, when others will be required.

Any of these initiatives It reduces the number of strings created in place, as I say - but is it likely to be a problem in your situation? GC was tuned to make it very cheap to make short-term objects


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -