Ignore:
Timestamp:
2015-05-06T00:23:51+02:00 (4 years ago)
Author:
Don-vip
Message:

fix various issues:

  • performance - inefficient use of keySet iterator instead of entrySet iterator
  • squid:S2674: The value returned from a stream read should be checked
  • remove unused imports
  • improve javadoc
Location:
trunk/src/org/openstreetmap/josm/data/projection/datum
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFile.java

    r8308 r8332  
    2727import java.util.HashMap;
    2828import java.util.List;
     29import java.util.Map;
     30
     31import org.openstreetmap.josm.Main;
    2932
    3033/**
     
    8689    }
    8790
     91    private void readBytes(InputStream in, byte[] b) throws IOException {
     92        if (in.read(b) < b.length) {
     93            Main.error("Failed to read expected amount of bytes ("+ b.length +") from stream");
     94        }
     95    }
     96
    8897    /**
    8998     * Load a Grid Shift File from an InputStream. The Grid Shift node
     
    98107     * @throws IOException
    99108     */
    100     public void loadGridShiftFile(InputStream in, boolean loadAccuracy ) throws IOException {
     109    public void loadGridShiftFile(InputStream in, boolean loadAccuracy) throws IOException {
    101110        byte[] b8 = new byte[8];
    102111        boolean bigEndian = true;
     
    104113        toEllipsoid = "";
    105114        topLevelSubGrid = null;
    106         in.read(b8);
     115        readBytes(in, b8);
    107116        String overviewHeaderCountId = new String(b8, StandardCharsets.UTF_8);
    108117        if (!"NUM_OREC".equals(overviewHeaderCountId))
    109118            throw new IllegalArgumentException("Input file is not an NTv2 grid shift file");
    110         in.read(b8);
     119        readBytes(in, b8);
    111120        overviewHeaderCount = NTV2Util.getIntBE(b8, 0);
    112121        if (overviewHeaderCount == 11) {
     
    119128                throw new IllegalArgumentException("Input file is not an NTv2 grid shift file");
    120129        }
    121         in.read(b8);
    122         in.read(b8);
     130        readBytes(in, b8);
     131        readBytes(in, b8);
    123132        subGridHeaderCount = NTV2Util.getInt(b8, bigEndian);
    124         in.read(b8);
    125         in.read(b8);
     133        readBytes(in, b8);
     134        readBytes(in, b8);
    126135        subGridCount = NTV2Util.getInt(b8, bigEndian);
    127136        NTV2SubGrid[] subGrid = new NTV2SubGrid[subGridCount];
    128         in.read(b8);
    129         in.read(b8);
     137        readBytes(in, b8);
     138        readBytes(in, b8);
    130139        shiftType = new String(b8, StandardCharsets.UTF_8);
    131         in.read(b8);
    132         in.read(b8);
     140        readBytes(in, b8);
     141        readBytes(in, b8);
    133142        version = new String(b8, StandardCharsets.UTF_8);
    134         in.read(b8);
    135         in.read(b8);
     143        readBytes(in, b8);
     144        readBytes(in, b8);
    136145        fromEllipsoid = new String(b8, StandardCharsets.UTF_8);
    137         in.read(b8);
    138         in.read(b8);
     146        readBytes(in, b8);
     147        readBytes(in, b8);
    139148        toEllipsoid = new String(b8, StandardCharsets.UTF_8);
    140         in.read(b8);
    141         in.read(b8);
     149        readBytes(in, b8);
     150        readBytes(in, b8);
    142151        fromSemiMajorAxis = NTV2Util.getDouble(b8, bigEndian);
    143         in.read(b8);
    144         in.read(b8);
     152        readBytes(in, b8);
     153        readBytes(in, b8);
    145154        fromSemiMinorAxis = NTV2Util.getDouble(b8, bigEndian);
    146         in.read(b8);
    147         in.read(b8);
     155        readBytes(in, b8);
     156        readBytes(in, b8);
    148157        toSemiMajorAxis = NTV2Util.getDouble(b8, bigEndian);
    149         in.read(b8);
    150         in.read(b8);
     158        readBytes(in, b8);
     159        readBytes(in, b8);
    151160        toSemiMinorAxis = NTV2Util.getDouble(b8, bigEndian);
    152161
     
    166175    private NTV2SubGrid[] createSubGridTree(NTV2SubGrid[] subGrid) {
    167176        int topLevelCount = 0;
    168         HashMap<String, List<NTV2SubGrid>> subGridMap = new HashMap<>();
     177        Map<String, List<NTV2SubGrid>> subGridMap = new HashMap<>();
    169178        for (int i = 0; i < subGrid.length; i++) {
    170179            if ("NONE".equalsIgnoreCase(subGrid[i].getParentSubGridName())) {
     
    321330        return toEllipsoid;
    322331    }
    323 
    324332}
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2SubGrid.java

    r8308 r8332  
    7575        byte[] b4 = new byte[4];
    7676        byte[] b1 = new byte[1];
    77         in.read(b8);
    78         in.read(b8);
     77        readBytes(in, b8);
     78        readBytes(in, b8);
    7979        subGridName = new String(b8, StandardCharsets.UTF_8).trim();
    80         in.read(b8);
    81         in.read(b8);
     80        readBytes(in, b8);
     81        readBytes(in, b8);
    8282        parentSubGridName = new String(b8, StandardCharsets.UTF_8).trim();
    83         in.read(b8);
    84         in.read(b8);
     83        readBytes(in, b8);
     84        readBytes(in, b8);
    8585        created = new String(b8, StandardCharsets.UTF_8);
    86         in.read(b8);
    87         in.read(b8);
     86        readBytes(in, b8);
     87        readBytes(in, b8);
    8888        updated = new String(b8, StandardCharsets.UTF_8);
    89         in.read(b8);
    90         in.read(b8);
     89        readBytes(in, b8);
     90        readBytes(in, b8);
    9191        minLat = NTV2Util.getDouble(b8, bigEndian);
    92         in.read(b8);
    93         in.read(b8);
     92        readBytes(in, b8);
     93        readBytes(in, b8);
    9494        maxLat = NTV2Util.getDouble(b8, bigEndian);
    95         in.read(b8);
    96         in.read(b8);
     95        readBytes(in, b8);
     96        readBytes(in, b8);
    9797        minLon = NTV2Util.getDouble(b8, bigEndian);
    98         in.read(b8);
    99         in.read(b8);
     98        readBytes(in, b8);
     99        readBytes(in, b8);
    100100        maxLon = NTV2Util.getDouble(b8, bigEndian);
    101         in.read(b8);
    102         in.read(b8);
     101        readBytes(in, b8);
     102        readBytes(in, b8);
    103103        latInterval = NTV2Util.getDouble(b8, bigEndian);
    104         in.read(b8);
    105         in.read(b8);
     104        readBytes(in, b8);
     105        readBytes(in, b8);
    106106        lonInterval = NTV2Util.getDouble(b8, bigEndian);
    107107        lonColumnCount = 1 + (int)((maxLon - minLon) / lonInterval);
    108108        latRowCount = 1 + (int)((maxLat - minLat) / latInterval);
    109         in.read(b8);
    110         in.read(b8);
     109        readBytes(in, b8);
     110        readBytes(in, b8);
    111111        nodeCount = NTV2Util.getInt(b8, bigEndian);
    112112        if (nodeCount != lonColumnCount * latRowCount)
     
    123123            // certain VM which are not able to read byte blocks when the resource file is
    124124            // in a .jar file (Pieren)
    125             in.read(b1); b4[0] = b1[0];
    126             in.read(b1); b4[1] = b1[0];
    127             in.read(b1); b4[2] = b1[0];
    128             in.read(b1); b4[3] = b1[0];
     125            readBytes(in, b1); b4[0] = b1[0];
     126            readBytes(in, b1); b4[1] = b1[0];
     127            readBytes(in, b1); b4[2] = b1[0];
     128            readBytes(in, b1); b4[3] = b1[0];
    129129            latShift[i] = NTV2Util.getFloat(b4, bigEndian);
    130             in.read(b1); b4[0] = b1[0];
    131             in.read(b1); b4[1] = b1[0];
    132             in.read(b1); b4[2] = b1[0];
    133             in.read(b1); b4[3] = b1[0];
     130            readBytes(in, b1); b4[0] = b1[0];
     131            readBytes(in, b1); b4[1] = b1[0];
     132            readBytes(in, b1); b4[2] = b1[0];
     133            readBytes(in, b1); b4[3] = b1[0];
    134134            lonShift[i] = NTV2Util.getFloat(b4, bigEndian);
    135             in.read(b1); b4[0] = b1[0];
    136             in.read(b1); b4[1] = b1[0];
    137             in.read(b1); b4[2] = b1[0];
    138             in.read(b1); b4[3] = b1[0];
     135            readBytes(in, b1); b4[0] = b1[0];
     136            readBytes(in, b1); b4[1] = b1[0];
     137            readBytes(in, b1); b4[2] = b1[0];
     138            readBytes(in, b1); b4[3] = b1[0];
    139139            if (loadAccuracy) {
    140140                latAccuracy[i] = NTV2Util.getFloat(b4, bigEndian);
    141141            }
    142             in.read(b1); b4[0] = b1[0];
    143             in.read(b1); b4[1] = b1[0];
    144             in.read(b1); b4[2] = b1[0];
    145             in.read(b1); b4[3] = b1[0];
     142            readBytes(in, b1); b4[0] = b1[0];
     143            readBytes(in, b1); b4[1] = b1[0];
     144            readBytes(in, b1); b4[2] = b1[0];
     145            readBytes(in, b1); b4[3] = b1[0];
    146146            if (loadAccuracy) {
    147147                lonAccuracy[i] = NTV2Util.getFloat(b4, bigEndian);
    148148            }
     149        }
     150    }
     151
     152    private void readBytes(InputStream in, byte[] b) throws IOException {
     153        if (in.read(b) < b.length) {
     154            Main.error("Failed to read expected amount of bytes ("+ b.length +") from stream");
    149155        }
    150156    }
     
    194200     * Bi-Linear interpolation of four nearest node values as described in
    195201     * 'GDAit Software Architecture Manual' produced by the <a
    196      * href='http://www.sli.unimelb.edu.au/gda94'>Geomatics
    197      * Department of the University of Melbourne</a>
     202     * href='http://www.dtpli.vic.gov.au/property-and-land-titles/geodesy/geocentric-datum-of-australia-1994-gda94/gda94-useful-tools'>
     203     * Geomatics Department of the University of Melbourne</a>
    198204     * @param a value at the A node
    199205     * @param b value at the B node
    200206     * @param c value at the C node
    201207     * @param d value at the D node
    202      * @param X Longitude factor
    203      * @param Y Latitude factor
     208     * @param x Longitude factor
     209     * @param y Latitude factor
    204210     * @return interpolated value
    205211     */
    206     private final double interpolate(float a, float b, float c, float d, double X, double Y) {
    207         return a + (((double)b - (double)a) * X) + (((double)c - (double)a) * Y) +
    208         (((double)a + (double)d - b - c) * X * Y);
     212    private final double interpolate(float a, float b, float c, float d, double x, double y) {
     213        return a + (((double)b - (double)a) * x) + (((double)c - (double)a) * y) +
     214        (((double)a + (double)d - b - c) * x * y);
    209215    }
    210216
     
    213219     * of the GridShiftFile. The algorithm is described in
    214220     * 'GDAit Software Architecture Manual' produced by the <a
    215      * href='http://www.sli.unimelb.edu.au/gda94'>Geomatics
    216      * Department of the University of Melbourne</a>
     221     * href='http://www.dtpli.vic.gov.au/property-and-land-titles/geodesy/geocentric-datum-of-australia-1994-gda94/gda94-useful-tools'>
     222     * Geomatics Department of the University of Melbourne</a>
    217223     * <p>This method is thread safe for both memory based and file based node data.
    218224     * @param gs GridShift object containing the coordinate to shift and the shift values
Note: See TracChangeset for help on using the changeset viewer.