1 | /* Copyright 2014 Malcolm Herring
|
---|
2 | *
|
---|
3 | * This is free software: you can redistribute it and/or modify
|
---|
4 | * it under the terms of the GNU General Public License as published by
|
---|
5 | * the Free Software Foundation, version 3 of the License.
|
---|
6 | *
|
---|
7 | * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
|
---|
8 | */
|
---|
9 |
|
---|
10 | package s57;
|
---|
11 |
|
---|
12 | import java.io.UnsupportedEncodingException;
|
---|
13 | import java.util.*;
|
---|
14 |
|
---|
15 | public class S57dat { // S57 ENC file fields lookup tables & methods
|
---|
16 |
|
---|
17 | public static class S57conv {
|
---|
18 | int asc; // 0=A(), 1+=A(n)
|
---|
19 | int bin; // 0=ASCII, +ve=b1n(unsigned LE), -ve=b2n(signed LE), n>8=b1(n/8)(unsigned BE)
|
---|
20 | S57conv(int a, int b) {
|
---|
21 | asc = a; bin = b;
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | public enum S57subf { I8RN, RCNM, RCID, EXPP, INTU, DSNM, EDTN, UPDN, UADT, ISDT, STED, PRSP, PSDN, PRED, PROF, AGEN, COMT, DSTR, AALL, NALL, NOMR, NOCR, NOGR, NOLR, NOIN, NOCN,
|
---|
26 | NOED, NOFA, HDAT, VDAT, SDAT, CSCL, DUNI, HUNI, PUNI, COUN, COMF, SOMF, PROJ, PRP1, PRP2, PRP3, PRP4, FEAS, FNOR, FPMF, RPID, RYCO, RXCO, CURP, RXVL, RYVL, PRCO, ESDT,
|
---|
27 | LSDT, DCRT, CODT, PACC, HACC, SACC, FILE, LFIL, VOLM, IMPL, SLAT, WLON, NLAT, ELON, CRCS, NAM1, NAM2, OORA, OAAC, OACO, OALL, OATY, DEFN, AUTH, RFTP, RFVL, ATLB, ATDO,
|
---|
28 | ADMU, ADFT, RAVA, DVAL, DVSD, OBLB, ASET, PRIM, GRUP, OBJL, RVER, RUIN, FIDN, FIDS, ATTL, ATVL, FFUI, FFIX, NFPT, LNAM, RIND, FSUI, FSIX, NSPT, NAME, ORNT, USAG, MASK,
|
---|
29 | VPUI, VPIX, NVPT, TOPI, CCUI, CCIX, CCNC, YCOO, XCOO, VE3D, ATYP, SURF, ORDR, RESO, STPT, CTPT, ENPT, CDPM, CDPR }
|
---|
30 |
|
---|
31 | private static final EnumMap<S57subf, S57conv> convs = new EnumMap<S57subf, S57conv>(S57subf.class);
|
---|
32 | static {
|
---|
33 | convs.put(S57subf.I8RN, new S57conv(5,2));
|
---|
34 | convs.put(S57subf.RCNM, new S57conv(2,1)); convs.put(S57subf.RCID, new S57conv(10,4)); convs.put(S57subf.EXPP, new S57conv(1,1));
|
---|
35 | convs.put(S57subf.INTU, new S57conv(1,1)); convs.put(S57subf.DSNM, new S57conv(0,0)); convs.put(S57subf.EDTN, new S57conv(0,0));
|
---|
36 | convs.put(S57subf.UPDN, new S57conv(0,0)); convs.put(S57subf.UADT, new S57conv(8,0)); convs.put(S57subf.ISDT, new S57conv(8,0));
|
---|
37 | convs.put(S57subf.STED, new S57conv(4,0)); convs.put(S57subf.PRSP, new S57conv(3,1)); convs.put(S57subf.PSDN, new S57conv(0,0));
|
---|
38 | convs.put(S57subf.PRED, new S57conv(0,0)); convs.put(S57subf.PROF, new S57conv(2,1)); convs.put(S57subf.AGEN, new S57conv(2,2));
|
---|
39 | convs.put(S57subf.COMT, new S57conv(0,0)); convs.put(S57subf.DSTR, new S57conv(2,1)); convs.put(S57subf.AALL, new S57conv(1,1));
|
---|
40 | convs.put(S57subf.NALL, new S57conv(1,1)); convs.put(S57subf.NOMR, new S57conv(0,4)); convs.put(S57subf.NOCR, new S57conv(0,4));
|
---|
41 | convs.put(S57subf.NOGR, new S57conv(0,4)); convs.put(S57subf.NOLR, new S57conv(0,4)); convs.put(S57subf.NOIN, new S57conv(0,4));
|
---|
42 | convs.put(S57subf.NOCN, new S57conv(0,4)); convs.put(S57subf.NOED, new S57conv(0,4)); convs.put(S57subf.NOFA, new S57conv(0,4));
|
---|
43 | convs.put(S57subf.HDAT, new S57conv(3,1)); convs.put(S57subf.VDAT, new S57conv(2,1)); convs.put(S57subf.SDAT, new S57conv(2,1));
|
---|
44 | convs.put(S57subf.CSCL, new S57conv(0,4)); convs.put(S57subf.DUNI, new S57conv(2,1)); convs.put(S57subf.HUNI, new S57conv(2,1));
|
---|
45 | convs.put(S57subf.PUNI, new S57conv(2,1)); convs.put(S57subf.COUN, new S57conv(2,1)); convs.put(S57subf.COMF, new S57conv(0,4));
|
---|
46 | convs.put(S57subf.SOMF, new S57conv(0,4)); convs.put(S57subf.PROJ, new S57conv(3,1)); convs.put(S57subf.PRP1, new S57conv(0,-4));
|
---|
47 | convs.put(S57subf.PRP2, new S57conv(0,-4)); convs.put(S57subf.PRP3, new S57conv(0,-4)); convs.put(S57subf.PRP4, new S57conv(0,-4));
|
---|
48 | convs.put(S57subf.FEAS, new S57conv(0,-4)); convs.put(S57subf.FNOR, new S57conv(0,-4)); convs.put(S57subf.FPMF, new S57conv(0,4));
|
---|
49 | convs.put(S57subf.RPID, new S57conv(1,1)); convs.put(S57subf.RYCO, new S57conv(0,-4)); convs.put(S57subf.RXCO, new S57conv(0,-4));
|
---|
50 | convs.put(S57subf.CURP, new S57conv(2,1)); convs.put(S57subf.RXVL, new S57conv(0,-4)); convs.put(S57subf.RYVL, new S57conv(0,-4));
|
---|
51 | convs.put(S57subf.PRCO, new S57conv(2,2)); convs.put(S57subf.ESDT, new S57conv(8,0)); convs.put(S57subf.LSDT, new S57conv(8,0));
|
---|
52 | convs.put(S57subf.DCRT, new S57conv(0,0)); convs.put(S57subf.CODT, new S57conv(8,0)); convs.put(S57subf.PACC, new S57conv(0,4));
|
---|
53 | convs.put(S57subf.HACC, new S57conv(0,4)); convs.put(S57subf.SACC, new S57conv(0,4)); convs.put(S57subf.FILE, new S57conv(0,0));
|
---|
54 | convs.put(S57subf.LFIL, new S57conv(0,0)); convs.put(S57subf.VOLM, new S57conv(0,0)); convs.put(S57subf.IMPL, new S57conv(3,0));
|
---|
55 | convs.put(S57subf.SLAT, new S57conv(0,0)); convs.put(S57subf.WLON, new S57conv(0,0)); convs.put(S57subf.NLAT, new S57conv(0,0));
|
---|
56 | convs.put(S57subf.ELON, new S57conv(0,0)); convs.put(S57subf.CRCS, new S57conv(0,0)); convs.put(S57subf.NAM1, new S57conv(12,5));
|
---|
57 | convs.put(S57subf.NAM2, new S57conv(12,5)); convs.put(S57subf.OORA, new S57conv(1,1)); convs.put(S57subf.OAAC, new S57conv(6,0));
|
---|
58 | convs.put(S57subf.OACO, new S57conv(5,2)); convs.put(S57subf.OALL, new S57conv(0,0)); convs.put(S57subf.OATY, new S57conv(1,1));
|
---|
59 | convs.put(S57subf.DEFN, new S57conv(0,0)); convs.put(S57subf.AUTH, new S57conv(2,2)); convs.put(S57subf.RFTP, new S57conv(2,1));
|
---|
60 | convs.put(S57subf.RFVL, new S57conv(0,0)); convs.put(S57subf.ATLB, new S57conv(5,2)); convs.put(S57subf.ATDO, new S57conv(1,1));
|
---|
61 | convs.put(S57subf.ADMU, new S57conv(0,0)); convs.put(S57subf.ADFT, new S57conv(0,0)); convs.put(S57subf.RAVA, new S57conv(1,1));
|
---|
62 | convs.put(S57subf.DVAL, new S57conv(0,0)); convs.put(S57subf.DVSD, new S57conv(0,0)); convs.put(S57subf.OBLB, new S57conv(5,2));
|
---|
63 | convs.put(S57subf.ASET, new S57conv(1,1)); convs.put(S57subf.PRIM, new S57conv(1,1)); convs.put(S57subf.GRUP, new S57conv(3,1));
|
---|
64 | convs.put(S57subf.OBJL, new S57conv(5,2)); convs.put(S57subf.RVER, new S57conv(3,2)); convs.put(S57subf.RUIN, new S57conv(1,1));
|
---|
65 | convs.put(S57subf.FIDN, new S57conv(10,4)); convs.put(S57subf.FIDS, new S57conv(5,2)); convs.put(S57subf.ATTL, new S57conv(5,2));
|
---|
66 | convs.put(S57subf.ATVL, new S57conv(0,0)); convs.put(S57subf.FFUI, new S57conv(1,1)); convs.put(S57subf.FFIX, new S57conv(0,2));
|
---|
67 | convs.put(S57subf.NFPT, new S57conv(0,2)); convs.put(S57subf.LNAM, new S57conv(17,8)); convs.put(S57subf.RIND, new S57conv(0,1));
|
---|
68 | convs.put(S57subf.FSUI, new S57conv(1,1)); convs.put(S57subf.FSIX, new S57conv(0,2)); convs.put(S57subf.NSPT, new S57conv(0,2));
|
---|
69 | convs.put(S57subf.NAME, new S57conv(12,5)); convs.put(S57subf.ORNT, new S57conv(1,1)); convs.put(S57subf.USAG, new S57conv(1,1));
|
---|
70 | convs.put(S57subf.MASK, new S57conv(1,1)); convs.put(S57subf.VPUI, new S57conv(1,1)); convs.put(S57subf.VPIX, new S57conv(0,2));
|
---|
71 | convs.put(S57subf.NVPT, new S57conv(0,2)); convs.put(S57subf.TOPI, new S57conv(1,1)); convs.put(S57subf.CCUI, new S57conv(1,1));
|
---|
72 | convs.put(S57subf.CCIX, new S57conv(0,2)); convs.put(S57subf.CCNC, new S57conv(0,2)); convs.put(S57subf.YCOO, new S57conv(0,-4));
|
---|
73 | convs.put(S57subf.XCOO, new S57conv(0,-4)); convs.put(S57subf.VE3D, new S57conv(0,-4)); convs.put(S57subf.ATYP, new S57conv(1,1));
|
---|
74 | convs.put(S57subf.SURF, new S57conv(1,1)); convs.put(S57subf.ORDR, new S57conv(1,1)); convs.put(S57subf.RESO, new S57conv(0,4));
|
---|
75 | convs.put(S57subf.STPT, new S57conv(0,0)); convs.put(S57subf.CTPT, new S57conv(0,0)); convs.put(S57subf.ENPT, new S57conv(0,0));
|
---|
76 | convs.put(S57subf.CDPM, new S57conv(0,0)); convs.put(S57subf.CDPR, new S57conv(0,0));
|
---|
77 | }
|
---|
78 |
|
---|
79 | public enum S57field { I8RI, DSID, DSSI, DSPM, DSPR, DSRC, DSHT, DSAC, CATD, CATX, DDDF, DDDR, DDDI, DDOM, DDRF, DDSI, DDSC,
|
---|
80 | FRID, FOID, LNAM, ATTF, NATF, FFPC, FFPT, FSPC, FSPT, VRID, ATTV, VRPC, VRPT, SGCC, SG2D, SG3D, ARCC, AR2D, EL2D, CT2D }
|
---|
81 |
|
---|
82 | private static ArrayList<S57subf> S57i8ri = new ArrayList<S57subf>(Arrays.asList(S57subf.I8RN));
|
---|
83 | private static ArrayList<S57subf> S57dsid = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.EXPP, S57subf.INTU, S57subf.DSNM, S57subf.EDTN, S57subf.UPDN,
|
---|
84 | S57subf.UADT, S57subf.ISDT, S57subf.STED, S57subf.PRSP, S57subf.PSDN, S57subf.PRED, S57subf.PROF, S57subf.AGEN, S57subf.COMT));
|
---|
85 | private static ArrayList<S57subf> S57dssi = new ArrayList<S57subf>(Arrays.asList(S57subf.DSTR, S57subf.AALL, S57subf.NALL, S57subf.NOMR, S57subf.NOCR, S57subf.NOGR, S57subf.NOLR,
|
---|
86 | S57subf.NOIN, S57subf.NOCN, S57subf.NOED, S57subf.NOFA ));
|
---|
87 | private static ArrayList<S57subf> S57dspm = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.HDAT, S57subf.VDAT, S57subf.SDAT, S57subf.CSCL, S57subf.DUNI,
|
---|
88 | S57subf.HUNI, S57subf.PUNI, S57subf.COUN, S57subf.COMF, S57subf.SOMF, S57subf.COMT ));
|
---|
89 | private static ArrayList<S57subf> S57dspr = new ArrayList<S57subf>(Arrays.asList(S57subf.PROJ, S57subf.PRP1, S57subf.PRP2, S57subf.PRP3, S57subf.PRP4, S57subf.FEAS, S57subf.FNOR,
|
---|
90 | S57subf.FPMF, S57subf.COMT ));
|
---|
91 | private static ArrayList<S57subf> S57dsrc = new ArrayList<S57subf>(Arrays.asList(S57subf.RPID, S57subf.RYCO, S57subf.RXCO, S57subf.CURP, S57subf.FPMF, S57subf.RXVL, S57subf.RYVL,
|
---|
92 | S57subf.COMT ));
|
---|
93 | private static ArrayList<S57subf> S57dsht = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.PRCO, S57subf.ESDT, S57subf.LSDT, S57subf.DCRT, S57subf.CODT, S57subf.COMT ));
|
---|
94 | private static ArrayList<S57subf> S57dsac = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.PACC, S57subf.HACC, S57subf.SACC, S57subf.FPMF, S57subf.COMT ));
|
---|
95 | private static ArrayList<S57subf> S57catd = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.FILE, S57subf.LFIL, S57subf.VOLM, S57subf.IMPL, S57subf.SLAT,
|
---|
96 | S57subf.WLON, S57subf.NLAT, S57subf.ELON, S57subf.CRCS, S57subf.COMT ));
|
---|
97 | private static ArrayList<S57subf> S57catx = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.NAM1, S57subf.NAM2, S57subf.COMT ));
|
---|
98 | private static ArrayList<S57subf> S57dddf = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.OORA, S57subf.OAAC, S57subf.OACO, S57subf.OALL, S57subf.OATY,
|
---|
99 | S57subf.DEFN, S57subf.AUTH, S57subf.COMT ));
|
---|
100 | private static ArrayList<S57subf> S57dddr = new ArrayList<S57subf>(Arrays.asList(S57subf.RFTP, S57subf.RFVL ));
|
---|
101 | private static ArrayList<S57subf> S57dddi = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.ATLB, S57subf.ATDO, S57subf.ADMU, S57subf.ADFT, S57subf.AUTH, S57subf.COMT ));
|
---|
102 | private static ArrayList<S57subf> S57ddom = new ArrayList<S57subf>(Arrays.asList(S57subf.RAVA, S57subf.DVAL, S57subf.DVSD, S57subf.DEFN, S57subf.AUTH ));
|
---|
103 | private static ArrayList<S57subf> S57ddrf = new ArrayList<S57subf>(Arrays.asList(S57subf.RFTP, S57subf.RFVL ));
|
---|
104 | private static ArrayList<S57subf> S57ddsi = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.OBLB ));
|
---|
105 | private static ArrayList<S57subf> S57ddsc = new ArrayList<S57subf>(Arrays.asList(S57subf.ATLB, S57subf.ASET, S57subf.AUTH ));
|
---|
106 | private static ArrayList<S57subf> S57frid = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.PRIM, S57subf.GRUP, S57subf.OBJL, S57subf.RVER, S57subf.RUIN ));
|
---|
107 | private static ArrayList<S57subf> S57foid = new ArrayList<S57subf>(Arrays.asList(S57subf.AGEN, S57subf.FIDN, S57subf.FIDS ));
|
---|
108 | private static ArrayList<S57subf> S57lnam = new ArrayList<S57subf>(Arrays.asList(S57subf.LNAM));
|
---|
109 | private static ArrayList<S57subf> S57attf = new ArrayList<S57subf>(Arrays.asList(S57subf.ATTL, S57subf.ATVL ));
|
---|
110 | private static ArrayList<S57subf> S57natf = new ArrayList<S57subf>(Arrays.asList(S57subf.ATTL, S57subf.ATVL ));
|
---|
111 | private static ArrayList<S57subf> S57ffpc = new ArrayList<S57subf>(Arrays.asList(S57subf.FFUI, S57subf.FFIX, S57subf.NFPT ));
|
---|
112 | private static ArrayList<S57subf> S57ffpt = new ArrayList<S57subf>(Arrays.asList(S57subf.LNAM, S57subf.RIND, S57subf.COMT ));
|
---|
113 | private static ArrayList<S57subf> S57fspc = new ArrayList<S57subf>(Arrays.asList(S57subf.FSUI, S57subf.FSIX, S57subf.NSPT ));
|
---|
114 | private static ArrayList<S57subf> S57fspt = new ArrayList<S57subf>(Arrays.asList(S57subf.NAME, S57subf.ORNT, S57subf.USAG, S57subf.MASK ));
|
---|
115 | private static ArrayList<S57subf> S57vrid = new ArrayList<S57subf>(Arrays.asList(S57subf.RCNM, S57subf.RCID, S57subf.RVER, S57subf.RUIN ));
|
---|
116 | private static ArrayList<S57subf> S57attv = new ArrayList<S57subf>(Arrays.asList(S57subf.ATTL, S57subf.ATVL ));
|
---|
117 | private static ArrayList<S57subf> S57vrpc = new ArrayList<S57subf>(Arrays.asList(S57subf.VPUI, S57subf.VPIX, S57subf.NVPT ));
|
---|
118 | private static ArrayList<S57subf> S57vrpt = new ArrayList<S57subf>(Arrays.asList(S57subf.NAME, S57subf.ORNT, S57subf.USAG, S57subf.TOPI, S57subf.MASK ));
|
---|
119 | private static ArrayList<S57subf> S57sgcc = new ArrayList<S57subf>(Arrays.asList(S57subf.CCUI, S57subf.CCIX, S57subf.CCNC ));
|
---|
120 | private static ArrayList<S57subf> S57sg2d = new ArrayList<S57subf>(Arrays.asList(S57subf.YCOO, S57subf.XCOO ));
|
---|
121 | private static ArrayList<S57subf> S57sg3d = new ArrayList<S57subf>(Arrays.asList(S57subf.YCOO, S57subf.XCOO, S57subf.VE3D ));
|
---|
122 | private static ArrayList<S57subf> S57arcc = new ArrayList<S57subf>(Arrays.asList(S57subf.ATYP, S57subf.SURF, S57subf.ORDR, S57subf.RESO, S57subf.FPMF ));
|
---|
123 | private static ArrayList<S57subf> S57ar2d = new ArrayList<S57subf>(Arrays.asList(S57subf.STPT, S57subf.CTPT, S57subf.ENPT, S57subf.YCOO, S57subf.XCOO ));
|
---|
124 | private static ArrayList<S57subf> S57el2d = new ArrayList<S57subf>(Arrays.asList(S57subf.STPT, S57subf.CTPT, S57subf.ENPT, S57subf.CDPM, S57subf.CDPR, S57subf.YCOO, S57subf.XCOO ));
|
---|
125 | private static ArrayList<S57subf> S57ct2d = new ArrayList<S57subf>(Arrays.asList(S57subf.YCOO, S57subf.XCOO ));
|
---|
126 |
|
---|
127 | private static final EnumMap<S57field, ArrayList<S57subf>> fields = new EnumMap<S57field, ArrayList<S57subf>>(S57field.class);
|
---|
128 | static {
|
---|
129 | fields.put(S57field.I8RI, S57i8ri);
|
---|
130 | fields.put(S57field.DSID, S57dsid); fields.put(S57field.DSSI, S57dssi); fields.put(S57field.DSPM, S57dspm); fields.put(S57field.DSPR, S57dspr);
|
---|
131 | fields.put(S57field.DSRC, S57dsrc); fields.put(S57field.DSHT, S57dsht); fields.put(S57field.DSAC, S57dsac); fields.put(S57field.CATD, S57catd);
|
---|
132 | fields.put(S57field.CATX, S57catx); fields.put(S57field.DDDF, S57dddf); fields.put(S57field.DDDR, S57dddr); fields.put(S57field.DDDI, S57dddi);
|
---|
133 | fields.put(S57field.DDOM, S57ddom); fields.put(S57field.DDRF, S57ddrf); fields.put(S57field.DDSI, S57ddsi); fields.put(S57field.DDSC, S57ddsc);
|
---|
134 | fields.put(S57field.FRID, S57frid); fields.put(S57field.FOID, S57foid); fields.put(S57field.LNAM, S57lnam); fields.put(S57field.ATTF, S57attf);
|
---|
135 | fields.put(S57field.NATF, S57natf); fields.put(S57field.FFPC, S57ffpc); fields.put(S57field.FFPT, S57ffpt); fields.put(S57field.FFPC, S57fspc);
|
---|
136 | fields.put(S57field.FSPT, S57fspt); fields.put(S57field.VRID, S57vrid); fields.put(S57field.ATTV, S57attv); fields.put(S57field.VRPC, S57vrpc);
|
---|
137 | fields.put(S57field.VRPT, S57vrpt); fields.put(S57field.SGCC, S57sgcc); fields.put(S57field.SG2D, S57sg2d); fields.put(S57field.SG3D, S57sg3d);
|
---|
138 | fields.put(S57field.ARCC, S57arcc); fields.put(S57field.AR2D, S57ar2d); fields.put(S57field.EL2D, S57el2d); fields.put(S57field.CT2D, S57ct2d);
|
---|
139 | }
|
---|
140 |
|
---|
141 | private static byte[] leader = {'0', '0', '0', '0', '0', ' ', 'D', ' ', ' ', ' ', ' ', ' ', '0', '0', '0', '0', '0', ' ', ' ', ' ', '0', '0', '0', '4'};
|
---|
142 | private static byte[] buffer;
|
---|
143 | private static int offset;
|
---|
144 | private static int maxoff;
|
---|
145 | private static int index;
|
---|
146 | private static S57field field;
|
---|
147 | private static String aall = "US-ASCII";
|
---|
148 | private static String nall = "US-ASCII";
|
---|
149 | public static int rnum;
|
---|
150 |
|
---|
151 | private static S57conv findSubf(S57subf subf) {
|
---|
152 | ArrayList<S57subf> subs = fields.get(field);
|
---|
153 | boolean wrap = false;
|
---|
154 | while (true) {
|
---|
155 | if (index == subs.size()) {
|
---|
156 | if (!wrap) {
|
---|
157 | index = 0;
|
---|
158 | wrap = true;
|
---|
159 | } else {
|
---|
160 | System.out.println("ERROR: Subfield not found " + subf.name() + " in " + field.name() + " in record " + rnum);
|
---|
161 | System.exit(-1);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | S57subf sub = subs.get(index++);
|
---|
165 | S57conv conv = convs.get(sub);
|
---|
166 | if (sub == subf) {
|
---|
167 | return conv;
|
---|
168 | } else {
|
---|
169 | offset += (conv.bin != 0) ? ((conv.bin < 8) ? Math.abs(conv.bin) : conv.bin / 8) : conv.asc;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | public static void setField(byte[] buf, int off, S57field fld, int len) {
|
---|
175 | buffer = buf;
|
---|
176 | offset = off;
|
---|
177 | maxoff = off + len - 1;
|
---|
178 | field = fld;
|
---|
179 | index = 0;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public static boolean more() {
|
---|
183 | return (offset < maxoff);
|
---|
184 | }
|
---|
185 |
|
---|
186 | public static Object decSubf(byte[] buf, int off, S57field fld, S57subf subf) {
|
---|
187 | buffer = buf;
|
---|
188 | offset = off;
|
---|
189 | index = 0;
|
---|
190 | return decSubf(fld, subf);
|
---|
191 | }
|
---|
192 |
|
---|
193 | public static Object decSubf(S57field fld, S57subf subf) {
|
---|
194 | field = fld;
|
---|
195 | index = 0;
|
---|
196 | return decSubf(subf);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public static Object decSubf(S57subf subf) {
|
---|
200 | S57conv conv = findSubf(subf);
|
---|
201 | if (conv.bin == 0) {
|
---|
202 | String str = "";
|
---|
203 | int i = 0;
|
---|
204 | if (conv.asc == 0) {
|
---|
205 | for (i=0; buffer[offset+i] != 0x1f; i++) {}
|
---|
206 | try {
|
---|
207 | String charset = "";
|
---|
208 | if (field == S57field.ATTF) charset = aall;
|
---|
209 | else if (field == S57field.NATF) charset = nall;
|
---|
210 | else charset = "US-ASCII";
|
---|
211 | str = new String(buffer, offset, i, charset);
|
---|
212 | } catch (UnsupportedEncodingException e) {
|
---|
213 | e.printStackTrace();
|
---|
214 | }
|
---|
215 | offset += i + 1;
|
---|
216 | } else {
|
---|
217 | str = new String(buffer, offset, conv.asc);
|
---|
218 | offset += conv.asc;
|
---|
219 | }
|
---|
220 | return str;
|
---|
221 | } else {
|
---|
222 | int f = Math.abs(conv.bin);
|
---|
223 | if (f < 8) {
|
---|
224 | long val = buffer[offset + --f];
|
---|
225 | if (conv.bin > 0)
|
---|
226 | val &= 0xff;
|
---|
227 | while (f > 0) {
|
---|
228 | val = (val << 8) + (buffer[offset + --f] & 0xff);
|
---|
229 | }
|
---|
230 | offset += Math.abs(conv.bin);
|
---|
231 | if ((subf == S57subf.AALL) || (subf == S57subf.NALL)) {
|
---|
232 | String charset = "";
|
---|
233 | switch ((int)val) {
|
---|
234 | case 0:
|
---|
235 | charset = "US-ASCII";
|
---|
236 | break;
|
---|
237 | case 1:
|
---|
238 | charset = "ISO-8859-1";
|
---|
239 | break;
|
---|
240 | case 2:
|
---|
241 | charset = "UTF-16LE";
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | if (subf == S57subf.NALL) {
|
---|
245 | nall = charset;
|
---|
246 | } else {
|
---|
247 | aall = charset;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | return val;
|
---|
251 | } else {
|
---|
252 | f /= 8;
|
---|
253 | long val = 0;
|
---|
254 | for (int i = 0; i < f; i++) {
|
---|
255 | val = (val << 8) + (buffer[offset++] & 0xff);
|
---|
256 | }
|
---|
257 | return val;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public static byte[] encSubf(S57subf subf, Object val) {
|
---|
263 | S57conv conv = convs.get(subf);
|
---|
264 | if (conv.bin == 0) {
|
---|
265 | String sval = "";
|
---|
266 | if (val instanceof String) {
|
---|
267 | sval = (String) val;
|
---|
268 | } else if (val instanceof Long) {
|
---|
269 | sval = ((Long)val).toString();
|
---|
270 | } else if (val instanceof Double) {
|
---|
271 | sval = ((Double)val).toString();
|
---|
272 | }
|
---|
273 | index = sval.length();
|
---|
274 | try {
|
---|
275 | buffer = ((String) sval + " ").getBytes("ISO-8859-1");
|
---|
276 | } catch (Exception e) {
|
---|
277 | System.err.println(e.getMessage());
|
---|
278 | System.exit(-1);
|
---|
279 | }
|
---|
280 | if (conv.asc == 0) {
|
---|
281 | buffer[index] = 0x01f;
|
---|
282 | } else {
|
---|
283 | buffer = Arrays.copyOf(buffer, conv.asc);
|
---|
284 | while (index < buffer.length) {
|
---|
285 | buffer[index++] = ' ';
|
---|
286 | }
|
---|
287 | }
|
---|
288 | } else {
|
---|
289 | int f = Math.abs(conv.bin);
|
---|
290 | long lval;
|
---|
291 | if (val instanceof String) {
|
---|
292 | lval = Long.parseLong((String)val);
|
---|
293 | } else if (val instanceof Double) {
|
---|
294 | double dval = (double) val;
|
---|
295 | lval = (long) dval;
|
---|
296 | } else if (val instanceof Integer) {
|
---|
297 | lval = (int) val;
|
---|
298 | } else {
|
---|
299 | lval = (long) val;
|
---|
300 | }
|
---|
301 | buffer = new byte[f];
|
---|
302 | for (int i = 0; i < f; i++) {
|
---|
303 | buffer[i] = (byte) (lval & 0xff);
|
---|
304 | lval >>= 8;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | return buffer;
|
---|
308 | }
|
---|
309 |
|
---|
310 | static class Index {
|
---|
311 | byte[] field;
|
---|
312 | int length;
|
---|
313 | int offset;
|
---|
314 | Index (byte[] id, int l, int o) {
|
---|
315 | field = id;
|
---|
316 | length = l;
|
---|
317 | offset = o;
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | public static class Fparams {
|
---|
322 | public S57field field;
|
---|
323 | public Object[] params;
|
---|
324 | public Fparams(S57field f, Object[] p) {
|
---|
325 | field = f;
|
---|
326 | params = p;
|
---|
327 | }
|
---|
328 | };
|
---|
329 |
|
---|
330 | public static byte[] encRecord(int i8rn, ArrayList<Fparams> fparams) {
|
---|
331 | ArrayList<Index> index = new ArrayList<Index>();
|
---|
332 | int offset = 3;
|
---|
333 | int maxlen = 3;
|
---|
334 | byte[] buf = encSubf(S57subf.I8RN, i8rn);
|
---|
335 | buf = Arrays.copyOf(buf, 3);
|
---|
336 | buf[2] = 0x1e;
|
---|
337 | index.add(new Index("0001".getBytes(), 3, 0));
|
---|
338 | for (Fparams sfparams : fparams) {
|
---|
339 | for (int ip = 0; ip < sfparams.params.length; ) {
|
---|
340 | for (S57subf subf : fields.get(sfparams.field)) {
|
---|
341 | byte[] next = encSubf(subf, sfparams.params[ip++]);
|
---|
342 | buf = Arrays.copyOf(buf, (buf.length + next.length));
|
---|
343 | System.arraycopy(next, 0, buf, (buf.length - next.length), next.length);
|
---|
344 | }
|
---|
345 | }
|
---|
346 | buf = Arrays.copyOf(buf, (buf.length + 1));
|
---|
347 | buf[buf.length-1] = 0x1e;
|
---|
348 | int flen = buf.length - offset;
|
---|
349 | index.add(new Index(sfparams.field.toString().getBytes(), flen, offset));
|
---|
350 | maxlen = (flen > maxlen) ? flen : maxlen;
|
---|
351 | offset += flen;
|
---|
352 | }
|
---|
353 | int mlen = String.valueOf(maxlen).length();
|
---|
354 | String ffmt = "%0" + mlen + "d";
|
---|
355 | int olen = String.valueOf(offset).length();
|
---|
356 | String ofmt = "%0" + olen + "d";
|
---|
357 | int ilen = 4 + mlen + olen;
|
---|
358 | int isiz = (ilen * index.size()) + 1;
|
---|
359 | byte[] ibuf = new byte[isiz];
|
---|
360 | int i = 0;
|
---|
361 | for (Index item : index) {
|
---|
362 | for (byte ch : item.field) {
|
---|
363 | ibuf[i++] = ch;
|
---|
364 | }
|
---|
365 | byte[] digits = String.format(ffmt, item.length).getBytes();
|
---|
366 | for (byte ch : digits) {
|
---|
367 | ibuf[i++] = ch;
|
---|
368 | }
|
---|
369 | digits = String.format(ofmt, item.offset).getBytes();
|
---|
370 | for (byte ch : digits) {
|
---|
371 | ibuf[i++] = ch;
|
---|
372 | }
|
---|
373 | }
|
---|
374 | ibuf[i] = 0x1e;
|
---|
375 | byte[] fbuf = Arrays.copyOf(leader, (leader.length + ibuf.length + buf.length));
|
---|
376 | System.arraycopy(ibuf, 0, fbuf, leader.length, ibuf.length);
|
---|
377 | System.arraycopy(buf, 0, fbuf, (leader.length + ibuf.length), buf.length);
|
---|
378 | fbuf[20] = (byte)(mlen + 0x30);
|
---|
379 | fbuf[21] = (byte)(olen + 0x30);
|
---|
380 | System.arraycopy(String.format("%05d", fbuf.length).getBytes(), 0, fbuf, 0, 5);
|
---|
381 | System.arraycopy(String.format("%05d", (leader.length + ibuf.length)).getBytes(), 0, fbuf, 12, 5);
|
---|
382 | return fbuf;
|
---|
383 | }
|
---|
384 |
|
---|
385 | }
|
---|