javaperformancechroniclelow-level-iochronicle-bytes

Chronicle Bytes from InputStream


I'm trying to use saxophone for parsing json to protobuf message on the fly, and want to avoid creating string instances for each response.

For that i need to create Bytes instance from InputStream (that is provided from apache http entity).

I'm digging sources for a while but cant find way to do that... any suggestions?


Solution

  • There is two ways you can do this.

    // reuse a string builder if the String cannot be pooled easily
    stringBuilder.setLength(0);
    bytes.parseUTF(stringBuilder, StopCharTesters.ALL);
    

    or you can use the built in String pool

    String s = bytes.parseUTF(StopCharTesters.ALL);
    

    This will work well if there is a relative small number of possible Strings (at least most of the time)