How to avoid individual casting to byte during initialization of byte array in Java

I am using a byte array to store a text file outside of the filesystem.

It looks like this:

private static final byte[] CDRIVES = new byte[] {
        (byte)0xe0, 0x4f, (byte)0xd0, 0x20, (byte)0xea, 0x3a, 0x69, 0x10,
        (byte)0xa2, (byte)0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte)0x9d,
        (byte)0xba, (byte)0x8a, 0x0d, 0x45, 0x25, (byte)0xad, (byte)0xd0, 0x11,
        (byte)0x98, (byte)0xa8, 0x08, 0x00, 0x36, 0x1b, 0x11, 0x03,
        (byte)0x80, 0x53, 0x1c, (byte)0x87, (byte)0xa0, 0x42, 0x69, 0x10,
        (byte)0xa2, (byte)0xea, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte)0x9d
        ...
        ...
        ...
        };

Is there a way to avoid casting to (byte) for better visual interpretation?

I don't mind using other data type, but I need to be able build an InputStream out of it and do it the fastest way if possible. (for example storing a text file into a String variable is not the best way...)

Jon Skeet
people
quotationmark

Well one simple approach would be to use base64 - but perform the conversion on class initialization, so you only take the performance hit once:

private static final byte[] CDRIVES = Base64.decode("YOURBASE64HERE");

Or if it's genuinely text, perform that encoding once in a similar way:

private static final byte[] CDRIVES =
    "Your constant text here".getBytes(StandardCharsets.UTF_8);

Again, the performance hit occurs exactly once, and then you can use the bytes multiple times. I would be very surprised if the cost of encoding text into bytes at class initialization time is genuinely a bottleneck for you.

people

See more on this question at Stackoverflow