Java RandomAccessFile Read read_tagdesc(RandomAccessFile raf)

Here you can find the source of read_tagdesc(RandomAccessFile raf)

Description

reatagdesc

License

Open Source License

Declaration

private static HashMap<String, Object> read_tagdesc(RandomAccessFile raf) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright 2016 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
*
* This file is part of JSave.//from  w w w.  j  a v a  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.util.HashMap;

public class Main {
    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"
        }
    };

    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 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);
    }

    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_arraydesc(final RandomAccessFile raf)
  2. read_int32(RandomAccessFile raf)
  3. read_long(final RandomAccessFile raf)
  4. read_string(final RandomAccessFile raf)
  5. read_structdesc(final RandomAccessFile raf)
  6. read_typedesc(final RandomAccessFile raf)
  7. read_UnsignedByte(RandomAccessFile raf)
  8. readAByte(ReadableByteChannel readChannel)
  9. readFile(File f)