Java RandomAccessFile Read read_arraydesc(final RandomAccessFile raf)

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

Description

reaarraydesc

License

Open Source License

Declaration

public static HashMap<String, Object> read_arraydesc(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 w w w  . j a va 2s. co m*/
*
* 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.nio.ByteBuffer;

import java.util.HashMap;

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

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

Related

  1. load(String f)
  2. load(String fn, int type, int m, int n)
  3. LoadDspaceMapFile(String filename)
  4. LoadFloatFile(String filename)
  5. LoadGhostMapFile(String filename, int n_ids, int n_ghosts)
  6. read_int32(RandomAccessFile raf)
  7. read_long(final RandomAccessFile raf)
  8. read_string(final RandomAccessFile raf)
  9. read_structdesc(final RandomAccessFile raf)