Search code examples
javaarraysstringbytebytebuffer

Converting a String to ByteBuffer in Java


I want to convert an ASCII string to a ByteBuffer and I came across these two approaches:

ByteBuffer.wrap(str.getBytes(StandardCharsets.US_ASCII));

and

StandardCharsets.US_ASCII.encode(str);

What is their difference (maybe in terms of performance too)? Would these produce the same result?


Solution

  • getBytes uses the platforms default charset, not necessarily ASCII.

    Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

    StandardCharsets.US_ASCII.encode actually uses ASCII.

    If you use str.getBytes(StandardCharsets.US_ASCII), however, then they will be do the same thing on a high level.

    After a quick look into their implementations, getBytes seems to do a very different thing from encode, so to find out which one is quicker in terms of performance, you'd have to do a benchmark.

    EDIT:

    I wrote a JMH benchmark:

    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    @Warmup(iterations = 10)
    @Fork(value = 1)
    public class Main {
    
        static final String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    
        public static void main (String args[]) throws IOException, RunnerException {
            org.openjdk.jmh.Main.main(args);
        }
    
        @Benchmark
        public void wrap(Blackhole bh) {
    
            bh.consume(ByteBuffer.wrap(s.getBytes(StandardCharsets.US_ASCII)));
        }
    
        @Benchmark
        public void encode(Blackhole bh) {
            bh.consume(StandardCharsets.US_ASCII.encode(s));
        }
    }
    

    This is the result:

    Benchmark    Mode  Cnt     Score    Error  Units
    Main.encode  avgt   20  2407.242 ± 28.147  ns/op
    Main.wrap    avgt   20   199.227 ±  4.093  ns/op
    

    So wrap is a lot faster.