/** * This method will return the buffer that contains whole CIMS object. * * @return A byte array containing whole CIMS object */ public ByteArray toByteArray() { ByteArray buffer = new ByteArray( CimsObjectHeader.RECORD_LENGTH + CimsCodedDataHeader.RECORD_LENGTH + getLengthOfImageSegments()); updateCimsObjectHeader(); int lastSegmentCopiedOffset = 0; // copy image segments to output buffer and // update segment offsets and lengths for image segments in CIMS object header for (int i = 0; i < segmentArray.length; i++) { if (segmentArray[i] != null && segmentArray[i].length > 0) { // segment offset int segmentOffset = CimsObjectHeader.RECORD_LENGTH + CimsCodedDataHeader.RECORD_LENGTH + lastSegmentCopiedOffset; // copy segment array to output buffer buffer.write(segmentArray[i], 0, segmentArray[i].length, segmentOffset); cimsObjectHeader.setSegmentOffset(i + 1, segmentOffset); cimsObjectHeader.setSegmentLength(i + 1, segmentArray[i].length); lastSegmentCopiedOffset += segmentArray[i].length; } } // copy last so pick up any updates to header during loop. buffer.write(cimsObjectHeader.toByteArray(), 0, CimsObjectHeader.RECORD_LENGTH, 0); buffer.write( cimsCodedDataHeader.toByteArray(), 0, CimsCodedDataHeader.RECORD_LENGTH, CimsObjectHeader.RECORD_LENGTH); return buffer; }
/** * Primary constructor for <code>ImageObject</code>. Will store the buffer reference and calculate * the segment offsets and lengths for use in retrieving data for this object. It also stores all * image segments so it can recreate original buffer. * * @param buffer The byte array containing the ImageObject values. */ public ImageObject(ByteArray buffer) { byte[] cimsObjectHeaderArray = buffer.read(0, CimsObjectHeader.RECORD_LENGTH); cimsObjectHeader = new CimsObjectHeader(cimsObjectHeaderArray); // initialize coded data header from input buffer int offset0 = cimsObjectHeader.getSegmentOffset(0); int length0 = cimsObjectHeader.getSegmentLength(0); byte[] cimsCodedDataHeaderArray = new byte[CimsCodedDataHeader.RECORD_LENGTH]; buffer.read(offset0, cimsObjectHeaderArray, 0, length0); cimsCodedDataHeader = new CimsCodedDataHeader(cimsCodedDataHeaderArray); // initialize segment array, i.e. load all images for (int i = 0; i < segmentArray.length; i++) { int segmentOffset = cimsObjectHeader.getSegmentOffset(i + 1); int segmentLength = cimsObjectHeader.getSegmentLength(i + 1); if (segmentLength > 0) { segmentArray[i] = buffer.read(segmentOffset, segmentLength); } } }