📄 ddfsubfielddefinition.java
字号:
return nLength; } } /** * Extract a zero terminated string containing the data for this * subfield. Given a pointer to the data for this subfield (from * within a DDFRecord) this method will return the data for this * subfield. The number of bytes consumed as part of this field * can also be fetched. This number may be one longer than the * string length if there is a terminator character used. * <p> * * This function will return the raw binary data of a subfield for * types other than DDFString, including data past zero chars. * This is the standard way of extracting DDFBinaryString * subfields for instance. * <p> * * @param pachSourceData The pointer to the raw data for this * field. This may have come from DDFRecord::GetData(), * taking into account skip factors over previous subfields * data. * @param nMaxBytes The maximum number of bytes that are * accessable after pachSourceData. * @param pnConsumedBytes Pointer to an integer into which the * number of bytes consumed by this field should be * written. May be null to ignore. This is used as a skip * factor to increment pachSourceData to point to the next * subfields data. * * @return A pointer to a buffer containing the data for this * field. The returned pointer is to an internal buffer * which is invalidated on the next ExtractStringData() * call on this DDFSubfieldDefn(). It should not be freed * by the application. */ String extractStringData(byte[] pachSourceData, int nMaxBytes, MutableInt pnConsumedBytes) { int oldConsumed = 0; if (pnConsumedBytes != null) { oldConsumed = pnConsumedBytes.value; } int nLength = getDataLength(pachSourceData, nMaxBytes, pnConsumedBytes); String ns = new String(pachSourceData, 0, nLength); if (Debug.debugging("iso8211detail")) { Debug.output(" extracting string data from " + nLength + " bytes of " + pachSourceData.length + ": " + ns + ": consumed " + pnConsumedBytes.value + " vs. " + oldConsumed + ", max = " + nMaxBytes); } return ns; } /** * Extract a subfield value as a float. Given a pointer to the * data for this subfield (from within a DDFRecord) this method * will return the floating point data for this subfield. The * number of bytes consumed as part of this field can also be * fetched. This method may be called for any type of subfield, * and will return zero if the subfield is not numeric. * * @param pachSourceData The pointer to the raw data for this * field. This may have come from DDFRecord::GetData(), * taking into account skip factors over previous subfields * data. * @param nMaxBytes The maximum number of bytes that are * accessable after pachSourceData. * @param pnConsumedBytes Pointer to an integer into which the * number of bytes consumed by this field should be * written. May be null to ignore. This is used as a skip * factor to increment pachSourceData to point to the next * subfields data. * * @return The subfield's numeric value (or zero if it isn't * numeric). */ public double extractFloatData(byte[] pachSourceData, int nMaxBytes, MutableInt pnConsumedBytes) { switch (pszFormatString.charAt(0)) { case 'A': case 'I': case 'R': case 'S': case 'C': String dataString = extractStringData(pachSourceData, nMaxBytes, pnConsumedBytes); if (dataString.equals("")) { return 0; } try { return Double.parseDouble(dataString); } catch (NumberFormatException nfe) { if (Debug.debugging("iso8211")) { Debug.output("DDFSubfieldDefinition.extractFloatData: number format problem: " + dataString); } return 0; } case 'B': case 'b': byte[] abyData = new byte[8]; if (pnConsumedBytes != null) { pnConsumedBytes.value = nFormatWidth; } if (nFormatWidth > nMaxBytes) { Debug.error("DDFSubfieldDefinition: format width is greater than max bytes for float"); return 0.0; } // Byte swap the data if it isn't in machine native // format. In any event we copy it into our buffer to // ensure it is word aligned. // // DFD - don't think this applies to Java, since it's // always big endian // if (pszFormatString.charAt(0) == 'B') || // (pszFormatString.charAt(0) == 'b') { // for (int i = 0; i < nFormatWidth; i++) { // abyData[nFormatWidth-i-1] = pachSourceData[i]; // } // } else { System.arraycopy(pachSourceData, 0, abyData, 0, nFormatWidth); // } // Interpret the bytes of data. switch (eBinaryFormat) { case DDFBinaryFormat.UInt: case DDFBinaryFormat.SInt: case DDFBinaryFormat.FloatReal: return (double) MoreMath.BuildIntegerBE(abyData); // if (nFormatWidth == 1) // return(abyData[0]); // else if (nFormatWidth == 2) // return(*((GUInt16 *) abyData)); // else if (nFormatWidth == 4) // return(*((GUInt32 *) abyData)); // else { // return 0.0; // } // case DDFBinaryFormat.SInt: // if (nFormatWidth == 1) // return(*((signed char *) abyData)); // else if (nFormatWidth == 2) // return(*((GInt16 *) abyData)); // else if (nFormatWidth == 4) // return(*((GInt32 *) abyData)); // else { // return 0.0; // } // case DDFBinaryFormat.FloatReal: // if (nFormatWidth == 4) // return(*((float *) abyData)); // else if (nFormatWidth == 8) // return(*((double *) abyData)); // else { // return 0.0; // } case DDFBinaryFormat.NotBinary: case DDFBinaryFormat.FPReal: case DDFBinaryFormat.FloatComplex: return 0.0; } break; // end of 'b'/'B' case. default: } return 0.0; } /** * Extract a subfield value as an integer. Given a pointer to the * data for this subfield (from within a DDFRecord) this method * will return the int data for this subfield. The number of bytes * consumed as part of this field can also be fetched. This method * may be called for any type of subfield, and will return zero if * the subfield is not numeric. * * @param pachSourceData The pointer to the raw data for this * field. This may have come from DDFRecord::GetData(), * taking into account skip factors over previous subfields * data. * @param nMaxBytes The maximum number of bytes that are * accessable after pachSourceData. * @param pnConsumedBytes Pointer to an integer into which the * number of bytes consumed by this field should be * written. May be null to ignore. This is used as a skip * factor to increment pachSourceData to point to the next * subfields data. * * @return The subfield's numeric value (or zero if it isn't * numeric). */ public int extractIntData(byte[] pachSourceData, int nMaxBytes, MutableInt pnConsumedBytes) { switch (pszFormatString.charAt(0)) { case 'A': case 'I': case 'R': case 'S': case 'C': String dataString = extractStringData(pachSourceData, nMaxBytes, pnConsumedBytes); if (dataString.equals("")) { return 0; } try { return Double.valueOf(dataString).intValue(); } catch (NumberFormatException nfe) { if (Debug.debugging("iso8211")) { Debug.output("DDFSubfieldDefinition.extractIntData: number format problem: " + dataString); } return 0; } case 'B': case 'b': byte[] abyData = new byte[8]; if (nFormatWidth > nMaxBytes) { Debug.error("DDFSubfieldDefinition: format width is greater than max bytes for int"); return 0; } if (pnConsumedBytes != null) { pnConsumedBytes.value = nFormatWidth; } System.arraycopy(pachSourceData, 0, abyData, 0, nFormatWidth); // Interpret the bytes of data. switch (eBinaryFormat) { case DDFBinaryFormat.UInt: case DDFBinaryFormat.SInt: case DDFBinaryFormat.FloatReal: return (int) MoreMath.BuildIntegerBE(abyData); // case DDFBinaryFormat.UInt: // if (nFormatWidth == 4) // return((int) *((GUInt32 *) abyData)); // else if (nFormatWidth == 1) // return(abyData[0]); // else if (nFormatWidth == 2) // return(*((GUInt16 *) abyData)); // else { // CPLAssert(false); // return 0; // } // case DDFBinaryFormat.SInt: // if (nFormatWidth == 4) // return(*((GInt32 *) abyData)); // else if (nFormatWidth == 1) // return(*((signed char *) abyData)); // else if (nFormatWidth == 2) // return(*((GInt16 *) abyData)); // else { // CPLAssert(false); // return 0; // } // case DDFBinaryFormat.FloatReal: // if (nFormatWidth == 4) // return((int) *((float *) abyData)); // else if (nFormatWidth == 8) // return((int) *((double *) abyData)); // else { // CPLAssert(false); // return 0; // } case DDFBinaryFormat.NotBinary: case DDFBinaryFormat.FPReal: case DDFBinaryFormat.FloatComplex: return 0; } break; // end of 'b'/'B' case. default: return 0; } return 0; } /** * Dump subfield value to debugging file. * * @param pachData Pointer to data for this subfield. * @param nMaxBytes Maximum number of bytes available in pachData. */ public String dumpData(byte[] pachData, int nMaxBytes) { StringBuffer sb = new StringBuffer(); if (eType == DDFDataType.DDFFloat) { sb.append(" Subfield " + pszName + "=" + extractFloatData(pachData, nMaxBytes, null) + "\n"); } else if (eType == DDFDataType.DDFInt) { sb.append(" Subfield " + pszName + "=" + extractIntData(pachData, nMaxBytes, null) + "\n"); } else if (eType == DDFDataType.DDFBinaryString) { sb.append(" Subfield " + pszName + "=" + extractStringData(pachData, nMaxBytes, null) + "\n"); } else { sb.append(" Subfield " + pszName + "=" + extractStringData(pachData, nMaxBytes, null) + "\n"); } return sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -