Java RandomAccessFile Read read_typedesc(final RandomAccessFile raf)

Here you can find the source of read_typedesc(final RandomAccessFile raf)

Description

reatypedesc

License

Open Source License

Declaration

public static HashMap<String, Object> read_typedesc(final RandomAccessFile raf) throws IOException, Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright 2016 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
*
* This file is part of JSave.//from  www. j a  va  2  s .com
*
* JSave is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JSave is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JSave.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

public class Main {
    public static final HashMap STRUCT_DICT = new HashMap();
    private static final HashMap<Integer, String> DTYPE_DICT = new HashMap<Integer, String>() {
        {
            put(1, ">u1"); //1-byte unsigned integer, "U1 0"
            put(2, ">i2"); //2-byte signed integer, "I2 99",  "I2 15 -7 99"
            put(3, ">i4"); //4-byte integer signed, "I4 -5"
            put(4, ">f4"); //4-byte floating point, "F4 1.0"
            put(5, ">f8"); //8-byte floating point, "F8 6.02e23", "F8 0.1"
            put(6, ">c8");
            put(7, "|O");
            put(8, "|O");
            put(9, ">c16"); //128-bit complex floating-point number
            put(10, "|O");
            put(11, "|O");
            put(12, ">u2"); //2-byte unsigned integer, "U2 512"
            put(13, ">u4"); //4-byte unsigned integer, "U2 979"
            put(14, ">i8"); //8-byte signed integer, use hex notation for the value, "I8 0x123456789abcdf01"
            put(15, ">u8"); //8-byte unsigned integer, use hex notation for the value, "U8 0x7fffffffffffffff"
        }
    };

    public static HashMap<String, Object> read_typedesc(final RandomAccessFile raf) throws IOException, Exception {
        HashMap<String, Object> typedesc = new HashMap<>();
        typedesc.put("typecode", read_long(raf));
        typedesc.put("varflags", read_long(raf));
        if (2 == ((int) typedesc.get("varflags") & 2)) {
            throw new Exception("System variables not implemented");
        }
        typedesc.put("array", ((int) typedesc.get("varflags") & 4) == 4);
        typedesc.put("structure", ((int) typedesc.get("varflags") & 32) == 32);
        if ((boolean) typedesc.get("structure")) {
            typedesc.put("array_desc", read_arraydesc(raf));
            typedesc.put("struct_desc", read_structdesc(raf));
        } else if ((boolean) typedesc.get("array")) {
            typedesc.put("array_desc", read_arraydesc(raf));
        }
        return typedesc;
    }

    public static int read_long(final RandomAccessFile raf) throws IOException {
        byte[] data = new byte[4];
        raf.read(data);
        ByteBuffer bb = ByteBuffer.allocate(data.length);
        bb.put(data);
        return bb.getInt(0);
    }

    public static HashMap<String, Object> read_arraydesc(final RandomAccessFile raf) throws IOException, Exception {
        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));
            arraydesc.put("ndims", read_long(raf));
            skip_bytes(raf, 8);
            arraydesc.put("nmax", read_long(raf));
            int length = (int) arraydesc.get("nmax");
            int[] dims = new int[length];
            for (int i = 0; i < length; i++) {
                dims[i] = read_long(raf);
            }
            arraydesc.put("dims", dims);
            break;
        case 18:
            throw new UnsupportedOperationException("arrstart=18 is not supported");
            //warnings.warn("Using experimental 64-bit array read")
            //_skip_bytes(f, 8)

            //arraydesc['nbytes'] = _read_uint64(f)
            //arraydesc['nelements'] = _read_uint64(f)
            //arraydesc['ndims'] = _read_long(f)
            //_skip_bytes(f, 8)
            //arraydesc['nmax'] = 8
            //arraydesc['dims'] = []
            //for d in range(arraydesc['nmax']):
            //    v = _read_long(f)
            //    if v != 0:
            //        raise Exception("Expected a zero in ARRAY_DESC")
            //    arraydesc['dims'].append(_read_long(f))                
            //break;
        default:
            throw new Exception("Unknown ARRSTART: " + arraydesc.get("arrstart"));
        }

        return arraydesc;
    }

    public static HashMap<String, Object> read_structdesc(final RandomAccessFile raf)
            throws IOException, Exception {
        HashMap<String, Object> structdesc = new HashMap<>();
        int structstart = read_long(raf);
        if (structstart != 9) {
            throw new Exception("STRUCTSTART should be 9");
        }
        structdesc.put("name", read_string(raf));
        int predef = read_long(raf);
        structdesc.put("ntags", read_long(raf));
        structdesc.put("nbytes", read_long(raf));
        structdesc.put("predef", predef & 1);
        structdesc.put("inherits", predef & 2);
        structdesc.put("is_super", predef & 4);
        if ((int) structdesc.get("predef") != 1) {
            int length = (int) structdesc.get("ntags");
            HashMap[] tagtable = new HashMap[length];
            String[] name = new String[length];
            for (int i = 0; i < length; i++) {
                tagtable[i] = read_tagdesc(raf);
            }
            structdesc.put("tagtable", tagtable);
            for (HashMap tag : (HashMap[]) structdesc.get("tagtable")) {
                tag.put("name", read_string(raf));
            }
            HashMap<String, Object> arrtable = new HashMap<>();
            for (HashMap tag : (HashMap[]) structdesc.get("tagtable")) {
                if ((int) tag.get("array") == 1) {
                    arrtable.put((String) tag.get("name"), read_arraydesc(raf));
                }
            }
            structdesc.put("arrtable", arrtable);

            HashMap<String, Object> structtable = new HashMap<>();
            for (HashMap tag : (HashMap[]) structdesc.get("tagtable")) {
                if ((int) tag.get("structure") == 1) {
                    structtable.put((String) tag.get("name"), read_structdesc(raf));
                }
            }
            structdesc.put("structtable", structtable);

            if ((int) structdesc.get("inherits") == 1 || (int) structdesc.get("is_super") == 1) {
                structdesc.put("classname", read_string(raf));
                structdesc.put("nsupclasses", read_long(raf));
                int lengthSupclassnames = (int) structdesc.get("nsupclasses");
                String[] supclassnames = new String[lengthSupclassnames];
                for (int i = 0; i < lengthSupclassnames; i++) {
                    supclassnames[i] = read_string(raf);
                }
                structdesc.put("supclassnames", supclassnames);

                HashMap[] supclasstable = new HashMap[((HashMap[]) structdesc.get("nsupclasses")).length];
                for (int i = 0; i < ((HashMap[]) structdesc.get("nsupclasses")).length; i++) {
                    supclasstable[i] = read_structdesc(raf);
                }
                structdesc.put("supclasstable", supclasstable);
            }
            STRUCT_DICT.put(structdesc.get("name"), structdesc);

        } else {
            if (!STRUCT_DICT.containsKey(structdesc.get("name"))) {
                throw new Exception("PREDEF=1 but can't find definition");
            }

            structdesc = (HashMap<String, Object>) STRUCT_DICT.get(structdesc.get("name"));
        }
        return structdesc;
    }

    /**
     * Skip length bytes.
     *
     * @param raf the file where the bytes are read
     * @param length the length to skip
     * @throws IOException
     */
    public static void skip_bytes(final RandomAccessFile raf, int length) throws IOException {
        byte[] data = new byte[length];
        raf.read(data);
    }

    public static String read_string(final RandomAccessFile raf) throws IOException {
        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 {
            result = "";
        }
        return result;
    }

    private static HashMap<String, Object> read_tagdesc(RandomAccessFile raf) throws IOException {
        HashMap<String, Object> tagdesc = new HashMap<>();
        tagdesc.put("offset", read_long(raf));
        if ((int) tagdesc.get("offset") == -1) {
            tagdesc.put("offset", read_uint64(raf));
        }
        tagdesc.put("typecode", read_long(raf));
        int tagflags = read_long(raf);
        tagdesc.put("array", (tagflags & 4) == 4);
        tagdesc.put("structure", (tagflags & 32) == 32);
        tagdesc.put("scalar", DTYPE_DICT.containsKey(tagdesc.get("typecode")));
        // Assume '10'x is scalar
        return tagdesc;
    }

    public static final void align_32(RandomAccessFile raf) throws IOException {
        long pos = raf.getFilePointer();
        if (pos % 4 != 0) {
            raf.seek(pos + 4 - pos % 4);
        }
    }

    private static BigInteger read_uint64(final RandomAccessFile raf) throws IOException {
        byte[] data = new byte[8];
        raf.read(data);
        return new BigInteger(1, data);
    }
}

Related

  1. read_int32(RandomAccessFile raf)
  2. read_long(final RandomAccessFile raf)
  3. read_string(final RandomAccessFile raf)
  4. read_structdesc(final RandomAccessFile raf)
  5. read_tagdesc(RandomAccessFile raf)
  6. read_UnsignedByte(RandomAccessFile raf)
  7. readAByte(ReadableByteChannel readChannel)
  8. readFile(File f)
  9. readFile(File file)