use of java.nio.ByteOrder in project ffx by mjschnie.
the class MTZFilter method readFile.
/**
* {@inheritDoc}
*/
@Override
public boolean readFile(File mtzFile, ReflectionList reflectionList, DiffractionRefinementData refinementData, CompositeConfiguration properties) {
int nRead, nIgnore, nRes, nFriedel, nCut;
ByteOrder byteOrder = ByteOrder.nativeOrder();
FileInputStream fileInputStream;
DataInputStream dataInputStream;
boolean transpose = false;
StringBuilder sb = new StringBuilder();
try {
fileInputStream = new FileInputStream(mtzFile);
dataInputStream = new DataInputStream(fileInputStream);
byte[] headerOffset = new byte[4];
byte[] bytes = new byte[80];
int offset = 0;
// Eat "MTZ" title.
dataInputStream.read(bytes, offset, 4);
String mtzstr = null;
// Header offset.
dataInputStream.read(headerOffset, offset, 4);
// Machine stamp.
dataInputStream.read(bytes, offset, 4);
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
int stamp = byteBuffer.order(ByteOrder.BIG_ENDIAN).getInt();
String stampString = Integer.toHexString(stamp);
switch(stampString.charAt(0)) {
case '1':
case '3':
if (byteOrder.equals(ByteOrder.LITTLE_ENDIAN)) {
byteOrder = ByteOrder.BIG_ENDIAN;
}
break;
case '4':
if (byteOrder.equals(ByteOrder.BIG_ENDIAN)) {
byteOrder = ByteOrder.LITTLE_ENDIAN;
}
break;
}
byteBuffer = ByteBuffer.wrap(headerOffset);
int headerOffsetI = byteBuffer.order(byteOrder).getInt();
// skip to header and parse
dataInputStream.skipBytes((headerOffsetI - 4) * 4);
for (Boolean parsing = true; parsing; dataInputStream.read(bytes, offset, 80)) {
mtzstr = new String(bytes);
parsing = parseHeader(mtzstr);
}
// column identifiers
foString = sigFoString = rFreeString = null;
if (properties != null) {
foString = properties.getString("fostring", null);
sigFoString = properties.getString("sigfostring", null);
rFreeString = properties.getString("rfreestring", null);
}
h = k = l = fo = sigFo = rFree = -1;
fPlus = sigFPlus = fMinus = sigFMinus = rFreePlus = rFreeMinus = -1;
boolean print = true;
parseColumns(print);
if (h < 0 || k < 0 || l < 0) {
String message = "Fatal error in MTZ file - no H K L indexes?\n";
logger.log(Level.SEVERE, message);
return false;
}
// Reopen to start at beginning.
fileInputStream = new FileInputStream(mtzFile);
dataInputStream = new DataInputStream(fileInputStream);
// Skip initial header.
dataInputStream.skipBytes(80);
// Check if HKLs need to be transposed or not.
float[] data = new float[nColumns];
HKL mate = new HKL();
int nPosIgnore = 0;
int nTransIgnore = 0;
int nZero = 0;
int none = 0;
for (int i = 0; i < nReflections; i++) {
for (int j = 0; j < nColumns; j++) {
dataInputStream.read(bytes, offset, 4);
byteBuffer = ByteBuffer.wrap(bytes);
data[j] = byteBuffer.order(byteOrder).getFloat();
}
int ih = (int) data[h];
int ik = (int) data[k];
int il = (int) data[l];
boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, false);
HKL hklpos = reflectionList.getHKL(mate);
if (hklpos == null) {
nPosIgnore++;
}
friedel = reflectionList.findSymHKL(ih, ik, il, mate, true);
HKL hkltrans = reflectionList.getHKL(mate);
if (hkltrans == null) {
nTransIgnore++;
}
if (rFree > 0) {
if (((int) data[rFree]) == 0) {
nZero++;
} else if (((int) data[rFree]) == 1) {
none++;
}
}
if (rFreePlus > 0) {
if (((int) data[rFreePlus]) == 0) {
nZero++;
} else if (((int) data[rFreePlus]) == 1) {
none++;
}
}
if (rFreeMinus > 0) {
if (((int) data[rFreeMinus]) == 0) {
nZero++;
} else if (((int) data[rFreeMinus]) == 1) {
none++;
}
}
}
if (nPosIgnore > nTransIgnore) {
transpose = true;
}
if (none > (nZero * 2) && refinementData.rfreeflag < 0) {
refinementData.setFreeRFlag(0);
sb.append(format(" Setting R free flag to %d based on MTZ file data.\n", refinementData.rfreeflag));
} else if (nZero > (none * 2) && refinementData.rfreeflag < 0) {
refinementData.setFreeRFlag(1);
sb.append(format(" Setting R free flag to %d based on MTZ file data.\n", refinementData.rfreeflag));
} else if (refinementData.rfreeflag < 0) {
refinementData.setFreeRFlag(0);
sb.append(format(" Setting R free flag to MTZ default: %d\n", refinementData.rfreeflag));
}
// reopen to start at beginning
fileInputStream = new FileInputStream(mtzFile);
dataInputStream = new DataInputStream(fileInputStream);
// skip initial header
dataInputStream.skipBytes(80);
// read in data
double[][] anofSigF = new double[refinementData.n][4];
for (int i = 0; i < refinementData.n; i++) {
anofSigF[i][0] = anofSigF[i][1] = anofSigF[i][2] = anofSigF[i][3] = Double.NaN;
}
nRead = nIgnore = nRes = nFriedel = nCut = 0;
for (int i = 0; i < nReflections; i++) {
for (int j = 0; j < nColumns; j++) {
dataInputStream.read(bytes, offset, 4);
byteBuffer = ByteBuffer.wrap(bytes);
data[j] = byteBuffer.order(byteOrder).getFloat();
}
int ih = (int) data[h];
int ik = (int) data[k];
int il = (int) data[l];
boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, transpose);
HKL hkl = reflectionList.getHKL(mate);
if (hkl != null) {
if (fo > 0 && sigFo > 0) {
if (refinementData.fsigfcutoff > 0.0) {
if ((data[fo] / data[sigFo]) < refinementData.fsigfcutoff) {
nCut++;
continue;
}
}
if (friedel) {
anofSigF[hkl.index()][2] = data[fo];
anofSigF[hkl.index()][3] = data[sigFo];
nFriedel++;
} else {
anofSigF[hkl.index()][0] = data[fo];
anofSigF[hkl.index()][1] = data[sigFo];
}
} else {
if (fPlus > 0 && sigFPlus > 0) {
if (refinementData.fsigfcutoff > 0.0) {
if ((data[fPlus] / data[sigFPlus]) < refinementData.fsigfcutoff) {
nCut++;
continue;
}
}
anofSigF[hkl.index()][0] = data[fPlus];
anofSigF[hkl.index()][1] = data[sigFPlus];
}
if (fMinus > 0 && sigFMinus > 0) {
if (refinementData.fsigfcutoff > 0.0) {
if ((data[fMinus] / data[sigFMinus]) < refinementData.fsigfcutoff) {
nCut++;
continue;
}
}
anofSigF[hkl.index()][2] = data[fMinus];
anofSigF[hkl.index()][3] = data[sigFMinus];
}
}
if (rFree > 0) {
refinementData.setFreeR(hkl.index(), (int) data[rFree]);
} else {
if (rFreePlus > 0 && rFreeMinus > 0) {
// not sure what the correct thing to do here is?
refinementData.setFreeR(hkl.index(), (int) data[rFreePlus]);
} else if (rFreePlus > 0) {
refinementData.setFreeR(hkl.index(), (int) data[rFreePlus]);
} else if (rFreeMinus > 0) {
refinementData.setFreeR(hkl.index(), (int) data[rFreeMinus]);
}
}
nRead++;
} else {
HKL tmp = new HKL(ih, ik, il);
if (!reflectionList.resolution.inInverseResSqRange(Crystal.invressq(reflectionList.crystal, tmp))) {
nRes++;
} else {
nIgnore++;
}
}
}
// Set up fsigf from F+ and F-.
refinementData.generate_fsigf_from_anofsigf(anofSigF);
// Log results.
if (logger.isLoggable(Level.INFO)) {
sb.append(format(" MTZ file type (machine stamp): %s\n", stampString));
sb.append(format(" HKL data is %s\n", transpose ? "transposed" : "not transposed"));
sb.append(format(" HKL read in: %d\n", nRead));
sb.append(format(" HKL read as friedel mates: %d\n", nFriedel));
sb.append(format(" HKL NOT read in (too high resolution): %d\n", nRes));
sb.append(format(" HKL NOT read in (not in internal list?): %d\n", nIgnore));
sb.append(format(" HKL NOT read in (F/sigF cutoff): %d\n", nCut));
sb.append(format(" HKL in internal list: %d\n", reflectionList.hkllist.size()));
logger.info(sb.toString());
}
if (rFree < 0 && rFreePlus < 0 && rFreeMinus < 0) {
refinementData.generateRFree();
}
} catch (EOFException e) {
String message = " MTZ end of file reached.";
logger.log(Level.WARNING, message, e);
return false;
} catch (IOException e) {
String message = " MTZ IO Exception.";
logger.log(Level.WARNING, message, e);
return false;
}
return true;
}
use of java.nio.ByteOrder in project openj9 by eclipse.
the class ELFFileReader method getELFFileReaderWithOffset.
public static ELFFileReader getELFFileReaderWithOffset(ImageInputStream in, long offset) throws IOException, InvalidDumpFormatException {
// mark the stream as the validation and bitsize determinations move the underlying pointer
in.mark();
in.seek(offset);
if (!isFormatValid(in)) {
throw new InvalidDumpFormatException("The input stream is not an ELF file");
}
int bitness = in.read();
ByteOrder byteOrder = getByteOrder(in);
in.setByteOrder(byteOrder);
// reset the stream so that the reader reads from the start
in.reset();
if (ELFCLASS64 == bitness) {
return new ELF64FileReader(in, offset);
} else {
return new ELF32FileReader(in, offset);
}
}
use of java.nio.ByteOrder in project openj9 by eclipse.
the class ELFFileReader method getELFFileReaderWithOffset.
// ELF files can be either Big Endian (for example on Linux/PPC) or Little
// Endian (Linux/IA).
// Let's check whether it's actually an ELF file. We'll sort out the byte
// order later.
public static ELFFileReader getELFFileReaderWithOffset(File f, long offset) throws IOException, InvalidDumpFormatException {
// Figure out which combination of bitness and architecture we are
ImageInputStream in = new FileImageInputStream(f);
if (!isFormatValid(in)) {
throw new InvalidDumpFormatException("File " + f.getAbsolutePath() + " is not an ELF file");
}
int bitness = in.read();
ByteOrder byteOrder = getByteOrder(in);
// no longer need the input stream as passing the File descriptor into the reader
in.close();
if (ELFCLASS64 == bitness) {
return new ELF64FileReader(f, byteOrder, offset);
} else {
return new ELF32FileReader(f, byteOrder, offset);
}
}
use of java.nio.ByteOrder in project openj9 by eclipse.
the class ELFFileReader method getByteOrder.
private static ByteOrder getByteOrder(ImageInputStream in) throws IOException {
int endian = in.read();
ByteOrder byteOrder = null;
if (ELFDATA2MSB == endian) {
byteOrder = ByteOrder.BIG_ENDIAN;
} else {
byteOrder = ByteOrder.LITTLE_ENDIAN;
}
return byteOrder;
}
use of java.nio.ByteOrder in project openj9 by eclipse.
the class FileSniffer method isZipFile.
/**
* Checks to see if the input stream is for a zip file, assumes that the stream is correctly positioned.
* This method will reset the stream if possible to it's starting position. The reason for using this
* method over just trying java.util.zip.ZipFile(File) and catching errors is that on some platforms
* the zip support is provided by native libraries. It is possible to cause the native library to
* crash when speculatively passing files into it.
*
* @param in stream to analyze
* @return true if it is a zip file
* @throws IOException
*/
/*
* zip format
*
A. Local file header:
local file header signature 4 bytes (0x04034b50)
version needed to extract 2 bytes
general purpose bit flag 2 bytes
compression method 2 bytes
last mod file time 2 bytes
last mod file date 2 bytes
*/
public static boolean isZipFile(ImageInputStream iis) throws IOException {
ByteOrder byteOrder = iis.getByteOrder();
try {
// zip file format is in little endian format
iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
iis.mark();
int sig = iis.readInt();
if (sig != ZIP_ID) {
// header doesn't match
return false;
}
// date time starts at the 10th byte
iis.seek(10);
// validate the time stamp for the first local header entry
// read an MS-DOS date time format
int time = iis.readUnsignedShort();
int seconds = (time & 0x1F) / 2;
if ((seconds < 0) || (seconds > 60)) {
return false;
}
int minutes = (time & 0x7E0) >> 5;
if ((minutes < 0) || (minutes > 60)) {
return false;
}
int hours = (time & 0xF800) >> 11;
if ((hours < 0) || (hours > 23)) {
return false;
}
// validate the date stamp for the first local header entry
// read an MS-DOS date time format
int date = iis.readUnsignedShort();
int day = date & 0x1F;
if ((day < 1) || (day > 31)) {
return false;
}
int month = (date & 0x1E0) >> 5;
if ((month < 1) || (month > 12)) {
return false;
}
int year = ((date & 0xFE00) >> 9) + 1980;
if ((year < 1980) || (year > 2500)) {
return false;
}
} finally {
// restore the byte order
iis.setByteOrder(byteOrder);
// reset the stream position
iis.reset();
}
return true;
}
Aggregations