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?