4

Here is the code:

import java.io.*;

public class Bed {
    private String bedfn;
    private int bytes_snp;
    private int nindiv;
    private long bedsize;
    private int nsnp;
    private byte[] magicBytes;

    public Bed(String bedfn, int bytes_snp, int nindiv) {
        this.bedfn = bedfn;
        this.bytes_snp = bytes_snp;
        this.nindiv = nindiv;
        bedsize = (new File(bedfn)).length();
        nsnp = (int) ((bedsize - 3) / bytes_snp);
        ///////////////////////////////////////
        magicBytes = new byte[] {0x6c, 0x1b, 0x01};
    }

    //...

    public OutputStream writeBytes(OutputStream fileStream, byte[] bytes) {
        try{
            fileStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileStream;
    }

    public OutputStream writeBytes(OutputStream fileStream) {
        return writeBytes(fileStream, magicBytes);
    }

    public OutputStream writeBytes(String filename, byte[] bytes) throws FileNotFoundException {
        FileOutputStream fileOutputStream = new FileOutputStream(filename);
        BufferedOutputStream fileBufferedOutput = new BufferedOutputStream(fileOutputStream);
        return writeBytes(fileBufferedOutput, bytes);
    }


    public OutputStream writeBytes(String filename) throws FileNotFoundException {
        return writeBytes(filename, magicBytes);
    }




    public static void main(String[] args) throws IOException {
        String filename = "/Users/kaiyin/personal_config_bin_files/workspace/rbed2/inst/extdata/test.bed";
        Bed bedobj = new Bed(filename, 2, 6);

        OutputStream outputStream = bedobj.writeBytes("/tmp/x.bed");
        try{
            ///////////////////////////////////////
            outputStream.write(new byte[] {(byte) 0xff, (byte) 0xff});
        } finally {
            outputStream.close();
        }

    }
}

There are two byte array initializations (marked with ///////), in the first I didn't cast, in the second I have to, otherwise I get this error:

Error:(194, 45) java: incompatible types: possible lossy conversion from int to byte

Why?

M A
  • 65,721
  • 13
  • 123
  • 159
qed
  • 19,750
  • 16
  • 99
  • 168

3 Answers3

6

0xff does not fit in the range of representable numbers in the byte type. The maximum value that fits in a byte is 127 where as 0xff represents 255. Therefore you need a cast in this case.

When the number is representable, for instance in the first case where you assign 0x6c the Java compiler accepts it without a cast after applying a narrowing conversion from int to byte.

When the integer value is narrowed to a byte, all bits except the lowest representable by the target type are discarded. This means that for the integer 0xff which is 00000000000000000000000011111111, the lowest 8 bits are taken, resuling in the byte 11111111 which is -1. Therefore, the conversion changed the value and the sign of the initial number.

M A
  • 65,721
  • 13
  • 123
  • 159
2

That's because 0x01 is a Hex number that fits into a byte already, but 0xFF doesn't, so the compiler is telling you that you may lose information. So you need to explicitely cast it from int, to byte.

jotadepicas
  • 2,139
  • 2
  • 23
  • 44
1

The range of the type byte is from -128 to 127. So a number like 0x6c (which is 108 in decimal) is range. But 0xff (which is 255) exceeds that range and it needs to be casted to get the corresponding byte value: -1.

Community
  • 1
  • 1
Tom
  • 14,120
  • 16
  • 41
  • 47