List of utility methods to do RandomAccessFile Read
void | appendFile(RandomAccessFile main, RandomAccessFile extra) Append the contents of extra file to main. byte[] buf = new byte[1024 * 1024]; main.seek(main.length()); extra.seek(0); while (true) { int count = extra.read(buf); if (count == -1) break; main.write(buf, 0, count); ... |
byte[] | load(String f) Loads an entire file from the filesystem. return load(new File(f)); |
double[][] | load(String fn, int type, int m, int n) read a matrix from a binary file. if (m <= 0 && n <= 0) return null; RandomAccessFile file = new RandomAccessFile(fn, "r"); long l = file.length(); if (l < 0) { file.close(); return null; l /= dt_size[type]; if (m <= 0) m = (int) (l / n); else if (n <= 0) n = (int) (l / m); if (m <= 0 || n <= 0) { file.close(); return null; double[][] x = new double[m][n]; switch (type) { case DT_BYTE: for (int j = 0; j < n; j++) for (int i = 0; i < m; i++) x[i][j] = (double) (0xff & (int) file.readByte()); break; case DT_SHORT: for (int j = 0; j < n; j++) for (int i = 0; i < m; i++) x[i][j] = (double) file.readShort(); break; case DT_INT: for (int j = 0; j < n; j++) for (int i = 0; i < m; i++) x[i][j] = (double) file.readInt(); break; case DT_FLOAT: for (int j = 0; j < n; j++) for (int i = 0; i < m; i++) x[i][j] = (double) file.readFloat(); break; case DT_DOUBLE: for (int j = 0; j < n; j++) for (int i = 0; i < m; i++) x[i][j] = file.readDouble(); break; default: file.close(); throw new IllegalArgumentException("Illegal data type"); file.close(); return x; |
double[] | LoadDspaceMapFile(String filename) Get the array of diffractometer constants to map from time-of-flight to d-spacing, for each DAS ID. int bytes_per_record = 8; CheckFile(filename); File map_file = new File(filename); long file_size = map_file.length(); if (file_size % bytes_per_record != 0) throw new IllegalArgumentException(filename + " is not a d-space map."); long n_ids = file_size / bytes_per_record; byte[] buffer = new byte[(int) file_size]; ... |
float[] | LoadFloatFile(String filename) Get an array of float values from a file of bytes, in PC order. try { RandomAccessFile r_file = new RandomAccessFile(filename, "r"); long num_bytes = r_file.length(); if (num_bytes >= 4l * (long) (Integer.MAX_VALUE)) throw new IllegalArgumentException("File has more than " + Integer.MAX_VALUE + " float values and can't be loaded into an array"); int num_floats = (int) (num_bytes / 4); float[] list = new float[num_floats]; ... |
Vector | LoadGhostMapFile(String filename, int n_ids, int n_ghosts) Get the table of (id,weight) pairs from the specified file. int bytes_per_record = n_ghosts * 12; CheckFile(filename); File ghost_file = new File(filename); long file_size = ghost_file.length(); if (file_size % bytes_per_record != 0) throw new IllegalArgumentException(filename + " is not a ghost map."); if (file_size < bytes_per_record * n_ghosts) throw new IllegalArgumentException(filename + " only has records for " + file_size / bytes_per_record ... |
HashMap | read_arraydesc(final RandomAccessFile raf) reaarraydesc HashMap<String, Object> arraydesc = new HashMap<>(); arraydesc.put("arrstart", read_long(raf)); int arrStart = (int) arraydesc.get("arrstart"); switch (arrStart) { case 8: skip_bytes(raf, 4); arraydesc.put("nbytes", read_long(raf)); arraydesc.put("nelements", read_long(raf)); ... |
int | read_int32(RandomAccessFile raf) Reads an Integer (-2147483648 to 2147483647). byte[] data = new byte[4]; int intData = -1; try { raf.read(data); intData = ByteBuffer.wrap(data).getInt(); } catch (IOException e) { return intData; ... |
int | read_long(final RandomAccessFile raf) realong byte[] data = new byte[4]; raf.read(data); ByteBuffer bb = ByteBuffer.allocate(data.length); bb.put(data); return bb.getInt(0); |
String | read_string(final RandomAccessFile raf) reastring int length = read_long(raf); String result; if (length > 0) { byte[] data = new byte[length]; raf.read(data); align_32(raf); result = new String(data, StandardCharsets.UTF_8); } else { ... |