private Collection<String> readStringValues(AtfxCache atfxCache, long iidExtComp) throws AoException { long start = System.currentTimeMillis(); long aidExtComp = atfxCache.getAidsByBaseType("aoexternalcomponent").iterator().next(); // get filename int attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "filename_url"); String filenameUrl = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.stringVal(); File fileRoot = new File(atfxCache.getContext().get("FILE_ROOT").value.u.stringVal()); File extCompFile = new File(fileRoot, filenameUrl); // get datatype attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "value_type"); int valueType = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.enumVal(); if (valueType != 12) { throw new AoException( ErrorCode.AO_NOT_IMPLEMENTED, SeverityFlag.ERROR, 0, "Unsupported 'value_type' for data type DT_STRING or DT_DATE: " + valueType); } // read length attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "component_length"); int componentLength = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.longVal(); // read start offset, may be DT_LONG or DT_LONGLONG long startOffset = 0; attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "start_offset"); TS_Value vStartOffset = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp); if (vStartOffset.u.discriminator() == DataType.DT_LONG) { startOffset = vStartOffset.u.longVal(); } else if (vStartOffset.u.discriminator() == DataType.DT_LONGLONG) { startOffset = ODSHelper.asJLong(vStartOffset.u.longlongVal()); } // value_offset is irrelevant according ODS Standard 3.42, page 3-51 // read values RandomAccessFile raf = null; byte[] backingBuffer = new byte[componentLength]; List<String> list = new ArrayList<String>(); try { // open source channel raf = new BufferedRandomAccessFile(extCompFile, "r", BUFFER_SIZE); raf.seek(startOffset); raf.read(backingBuffer, 0, backingBuffer.length); int startPosition = 0; for (int position = 0; position < componentLength; position++) { if (backingBuffer[position] == 0) { list.add( new String(backingBuffer, startPosition, position - startPosition, "ISO-8859-1")); startPosition = position + 1; } } LOG.info( "Read " + list.size() + " string values from component file '" + filenameUrl + "' in " + (System.currentTimeMillis() - start) + "ms [value_type=" + ODSHelper.valueType2String(valueType) + "]"); return list; } catch (IOException e) { LOG.error(e.getMessage(), e); throw new AoException(ErrorCode.AO_NOT_FOUND, SeverityFlag.ERROR, 0, e.getMessage()); } finally { try { if (raf != null) { raf.close(); } raf = null; } catch (Exception e) { LOG.error(e.getMessage(), e); } } }
public TS_Value readFlags(AtfxCache atfxCache, long iidLc) throws AoException { long start = System.currentTimeMillis(); // read external component instances long aidLc = atfxCache.getAidsByBaseType("aolocalcolumn").iterator().next(); ApplicationRelation relExtComps = atfxCache.getApplicationRelationByBaseName(aidLc, "external_component"); Collection<Long> iidExtComps = atfxCache.getRelatedInstanceIds(aidLc, iidLc, relExtComps); if (iidExtComps.size() < 1) { return null; } else if (iidExtComps.size() != 1) { throw new AoException( ErrorCode.AO_NOT_IMPLEMENTED, SeverityFlag.ERROR, 0, "The implementation currently only may read exactly one external component file"); } long iidExtComp = iidExtComps.iterator().next(); long aidExtComp = atfxCache.getAidsByBaseType("aoexternalcomponent").iterator().next(); // get filename Integer attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "flags_filename_url"); if (attrNo == null) { return null; } TS_Value v = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp); if ((v == null) || (v.flag != 15) || (v.u.stringVal() == null) || (v.u.stringVal().length() < 1)) { return null; } String flagsFilenameUrl = v.u.stringVal(); File fileRoot = new File(atfxCache.getContext().get("FILE_ROOT").value.u.stringVal()); File flagsFile = new File(fileRoot, flagsFilenameUrl); // read start offset, may be DT_LONG or DT_LONGLONG int flagsStartOffset = 0; attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "flags_start_offset"); if (attrNo == null) { throw new AoException( ErrorCode.AO_NOT_FOUND, SeverityFlag.ERROR, 0, "Application attribute derived from base attribute 'flags_start_offset' not found"); } TS_Value vStartOffset = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp); if (vStartOffset.u.discriminator() == DataType.DT_LONG) { flagsStartOffset = vStartOffset.u.longVal(); } else if (vStartOffset.u.discriminator() == DataType.DT_LONGLONG) { flagsStartOffset = (int) ODSHelper.asJLong(vStartOffset.u.longlongVal()); } // read length attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "component_length"); int componentLength = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.longVal(); // read values TS_Value tsValue = new TS_Value(); tsValue.flag = (short) 15; tsValue.u = new TS_Union(); tsValue.u.shortSeq(new short[componentLength]); RandomAccessFile raf = null; byte[] backingBuffer = new byte[2]; try { // open source channel raf = new BufferedRandomAccessFile(flagsFile, "r", BUFFER_SIZE); raf.seek(flagsStartOffset); for (int i = 0; i < componentLength; i++) { raf.read(backingBuffer, 0, backingBuffer.length); ByteBuffer sourceMbb = ByteBuffer.wrap(backingBuffer); sourceMbb.order(ByteOrder.LITTLE_ENDIAN); tsValue.u.shortSeq()[i] = sourceMbb.getShort(); } LOG.info( "Read " + componentLength + " flags from component file '" + flagsFilenameUrl + "' in " + (System.currentTimeMillis() - start) + "ms"); return tsValue; } catch (IOException e) { LOG.error(e.getMessage(), e); throw new AoException(ErrorCode.AO_NOT_FOUND, SeverityFlag.ERROR, 0, e.getMessage()); } finally { backingBuffer = null; try { if (raf != null) { raf.close(); } raf = null; } catch (Exception e) { LOG.error(e.getMessage(), e); } } }
private List<Number> readNumberValues(AtfxCache atfxCache, long iidExtComp, DataType rawDataType) throws AoException { long start = System.currentTimeMillis(); List<Number> list = new ArrayList<Number>(); long aidExtComp = atfxCache.getAidsByBaseType("aoexternalcomponent").iterator().next(); // get filename Integer attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "filename_url"); String filenameUrl = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.stringVal(); File fileRoot = new File(atfxCache.getContext().get("FILE_ROOT").value.u.stringVal()); File extCompFile = new File(fileRoot, filenameUrl); // get value type attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "value_type"); int valueType = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.enumVal(); // read length attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "component_length"); int componentLength = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.longVal(); // read start offset, may be DT_LONG or DT_LONGLONG int startOffset = 0; attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "start_offset"); TS_Value vStartOffset = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp); if (vStartOffset.u.discriminator() == DataType.DT_LONG) { startOffset = vStartOffset.u.longVal(); } else if (vStartOffset.u.discriminator() == DataType.DT_LONGLONG) { startOffset = (int) ODSHelper.asJLong(vStartOffset.u.longlongVal()); } // read value offset attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "value_offset"); int valueOffset = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.longVal(); // read bit count short bitCount = 0; attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "ao_bit_count"); if (attrNo != null) { bitCount = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.shortVal(); } // read bit offset short bitOffset = 0; attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "ao_bit_offset"); if (attrNo != null) { bitOffset = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.shortVal(); } // block_size, valuesperblock attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "block_size"); int blockSize = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.longVal(); // valuesperblock attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "valuesperblock"); int valuesperblock = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.longVal(); // read values RandomAccessFile raf = null; try { // open source file raf = new BufferedRandomAccessFile(extCompFile, "r", BUFFER_SIZE); raf.seek(startOffset); // initialize buffer ByteBuffer sourceMbb = ByteBuffer.allocate(blockSize); ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN; // dt_short_beo [7], dt_long_beo [8], dt_longlong_beo [9], ieeefloat4_beo [10], ieeefloat8_beo // [11], // dt_boolean_flags_beo [15], dt_byte_flags_beo [16], dt_string_flags_beo [17], dt_bytestr_beo // [18], // dt_sbyte_flags_beo [20], dt_ushort_beo [22], dt_ulong_beo [24], dt_string_utf8_beo [26] // dt_bit_int_beo [28], dt_bit_uint_beo [30], dt_bit_float_beo [32] if ((valueType == 7) || (valueType == 8) || (valueType == 9) || (valueType == 10) || (valueType == 11) || (valueType == 15) || (valueType == 16) || (valueType == 17) || (valueType == 18) || (valueType == 20) || (valueType == 22) || (valueType == 24) || (valueType == 26) || (valueType == 28) || (valueType == 30) || (valueType == 32)) { byteOrder = ByteOrder.BIG_ENDIAN; } sourceMbb.order(byteOrder); // loop over blocks for (int i = 0; i < componentLength; i += valuesperblock) { // read whole block into memory byte[] record = new byte[blockSize]; raf.read(record, 0, record.length); sourceMbb.clear(); sourceMbb.put(record); sourceMbb.position(valueOffset); // sub blocks are consecutive, puhh! for (int j = 0; j < valuesperblock; j++) { // 1=dt_byte if (valueType == 1) { // int b = sourceMbb.get() & 0xFF; list.add(sourceMbb.get()); } // 19=dt_sbyte else if (valueType == 19) { // short b = (short) (sourceMbb.get() & 0xFF); list.add(sourceMbb.get()); } // 2=dt_short, 7=dt_short_beo else if ((valueType == 2) || (valueType == 7)) { list.add(sourceMbb.getShort()); } // 3=dt_long, 8=dt_long_beo else if ((valueType == 3) || (valueType == 8)) { list.add(sourceMbb.getInt()); } // 4=dt_longlong, 9=dt_longlong_beo else if ((valueType == 4) || (valueType == 9)) { list.add(sourceMbb.getLong()); } // 5=ieeefloat4, 10=ieeefloat4_beo else if ((valueType == 5) || (valueType == 10)) { list.add(sourceMbb.getFloat()); } // 6=ieeefloat8, 11=ieeefloat8_beo else if ((valueType == 6) || (valueType == 11)) { list.add(sourceMbb.getDouble()); } // 21=dt_ushort, 22=dt_ushort_beo else if ((valueType == 21) || (valueType == 22)) { int val = sourceMbb.getShort() & 0xffff; list.add(val); } // 23=dt_ulong, 24=dt_ulong_beo else if ((valueType == 23) || (valueType == 24)) { long val = sourceMbb.getInt() & 0xffffffffL; list.add(val); } // 29=dt_bit_uint, 30=dt_bit_uint_beo else if ((valueType == 29) || (valueType == 30)) { // read that number of bytes from the byte position within the file int bytesToRead = ((bitCount + bitOffset - 1) / 8) + 1; byte[] tmp = new byte[bytesToRead]; sourceMbb.get(tmp); // put the byte into an integer to enable bit shifting if (tmp.length <= 4) { ByteBuffer bb = ByteBuffer.allocate(4); bb.order(byteOrder); bb.put(tmp); bb.rewind(); int intValue = bb.getInt(); intValue = intValue >> bitOffset; // shift right bit offset int mask = 0xFFFFFFFF >>> (32 - bitCount); // mask out unnecessary bits intValue = intValue & mask; list.add(intValue); } // put the byte into a long to enable bit shifting else { ByteBuffer bb = ByteBuffer.allocate(8); bb.order(byteOrder); bb.put(tmp); bb.rewind(); long longValue = bb.getLong(); longValue = longValue >> bitOffset; // shift right bit offset long mask = 0xFFFFFFFFFFFFFFFFl >>> (64 - bitCount); // mask out unnecessary bits longValue = longValue & mask; list.add(longValue); } } // unsupported data type else { throw new AoException( ErrorCode.AO_NOT_IMPLEMENTED, SeverityFlag.ERROR, 0, "Unsupported 'value_type': " + ODSHelper.valueType2String(valueType)); } } } LOG.info( "Read " + list.size() + " numeric values from component file '" + filenameUrl + "' in " + (System.currentTimeMillis() - start) + "ms [value_type=" + ODSHelper.valueType2String(valueType) + "]"); return list; } catch (IOException e) { LOG.error(e.getMessage(), e); throw new AoException(ErrorCode.AO_NOT_FOUND, SeverityFlag.ERROR, 0, e.getMessage()); } finally { if (raf != null) { try { raf.close(); } catch (IOException ioe) { LOG.error(ioe.getMessage(), ioe); } raf = null; } } }