package gov.nasa.gsfc.voyager.cdf; import java.nio.*; import java.io.*; import java.lang.reflect.*; public class DataTypes { public static final int ENCODING_COUNT = 17; public static final ByteOrder[] endian_ness = new ByteOrder[ENCODING_COUNT]; static { for (int i = 0; i < ENCODING_COUNT; i++) { endian_ness[i] = null; } endian_ness[1] = ByteOrder.BIG_ENDIAN; endian_ness[2] = ByteOrder.BIG_ENDIAN; endian_ness[4] = ByteOrder.LITTLE_ENDIAN; endian_ness[5] = ByteOrder.BIG_ENDIAN; endian_ness[6] = ByteOrder.LITTLE_ENDIAN; endian_ness[7] = ByteOrder.BIG_ENDIAN; endian_ness[9] = ByteOrder.BIG_ENDIAN; endian_ness[12] = ByteOrder.BIG_ENDIAN; endian_ness[13] = ByteOrder.LITTLE_ENDIAN; endian_ness[16] = ByteOrder.LITTLE_ENDIAN; } public static final int EPOCH16 = 32; public static final int FLOAT = 0; public static final int DOUBLE = 1; public static final int SIGNED_INTEGER = 2; public static final int UNSIGNED_INTEGER = 3; public static final int STRING = 4; public static int LAST_TYPE = 53; public static Method[] method = new Method[LAST_TYPE]; public static int[] typeCategory = new int[LAST_TYPE]; public static int[] size = new int[LAST_TYPE]; static { for (int i = 0; i < LAST_TYPE; i++) { method[i] = null; size[i] = 1; typeCategory[i] = -1; } // byte Class bb = ByteBuffer.class; try { Method meth = bb.getMethod("get", new Class[] {}); method[1] = meth; typeCategory[1] = SIGNED_INTEGER; method[11] = meth; typeCategory[11] = UNSIGNED_INTEGER; method[41] = meth; typeCategory[41] = SIGNED_INTEGER; meth = bb.getMethod("getShort", new Class[] {}); method[2] = meth; typeCategory[2] = SIGNED_INTEGER; size[2] = 2; method[12] = meth; typeCategory[12] = UNSIGNED_INTEGER; size[12] = 2; meth = bb.getMethod("getInt", new Class[] {}); method[4] = meth; typeCategory[4] = SIGNED_INTEGER; size[4] = 4; method[14] = meth; typeCategory[14] = UNSIGNED_INTEGER; size[14] = 4; meth = bb.getMethod("getFloat", new Class[] {}); method[21] = meth; typeCategory[21] = FLOAT; size[21] = 4; method[44] = meth; typeCategory[44] = FLOAT; size[44] = 4; meth = bb.getMethod("getDouble", new Class[] {}); method[22] = meth; typeCategory[22] = DOUBLE; size[22] = 8; method[45] = meth; typeCategory[45] = DOUBLE; size[45] = 8; method[31] = meth; typeCategory[31] = DOUBLE; size[31] = 8; method[32] = meth; typeCategory[32] = DOUBLE; size[32] = 8; typeCategory[41] = SIGNED_INTEGER; typeCategory[51] = STRING; typeCategory[52] = STRING; } catch (Exception ex) { } } public DataTypes() { Class tc = getClass(); try { Method meth = tc.getMethod("getString", new Class[] {ByteBuffer.class, Integer.class}); method[51] = meth; method[52] = meth; } catch (Exception ex) { } } public static String getString(ByteBuffer buf, Integer nc) { ByteBuffer slice = buf.slice(); byte [] ba = new byte[nc.intValue()]; int i = 0; for (; i < ba.length; i++) { ba[i] = slice.get(); if (ba[i] == 0) break; } return new String(ba, 0, i); } public static ByteOrder getByteOrder(int encoding) { if (endian_ness[encoding] != null) return endian_ness[encoding]; throw new IllegalArgumentException("Unsupported encoding " + encoding); } public static boolean isStringType(int type) { return (typeCategory[type] == STRING); } }