Arrays.sort(versions);
for (int i = versions.length - 1; i >= 0; i--) {
// get a managed content copy of this datastream version
DatastreamXMLMetadata existing = (DatastreamXMLMetadata)copyDS.get(versions[i]);
DatastreamManagedContent newDS = new DatastreamManagedContent();
existing.copy(newDS);
// X control group will have been copied over by above, reset it
newDS.DSControlGrp = controlGroup;
// probably not necessary, but just in case...
newDS.DSLocation = null;
newDS.DSLocationType = null;
// add character encoding to mime type (will always be UTF-8 as it has come from X datastream in FOXML)
if (setMIMETypeCharset) {
if (newDS.DSMIME != null && !newDS.DSMIME.equals("") & !newDS.DSMIME.contains("charset=")) {
newDS.DSMIME = newDS.DSMIME + "; charset=UTF-8";
} else {
newDS.DSMIME = "text/xml; charset=UTF-8";
}
}
byte[] byteContent;
// Note: use getContentStream() rather than getting bytes directly, as this is how
// X datastreams are disseminated (we want the M content to be identical on
// dissemination)
if (reformat) {
byteContent = this.getXML(existing.getContentStream(), addXMLHeader);
} else {
// add just the XML header declaring encoding, if requested
if (addXMLHeader) {
byte[] header;
try {
header = xmlHeader.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// should never happen
throw new RuntimeException(e);
}
byte[] existingContent;
try {
existingContent = IOUtils.toByteArray(existing.getContentStream());
} catch (IOException e) {
throw new GeneralException("Error reading existing content from X datastream", e);
}
byteContent = Arrays.copyOf(header, header.length + existingContent.length);
System.arraycopy(existing.xmlContent, 0, byteContent, header.length, existingContent.length);
} else {
try {
byteContent = IOUtils.toByteArray(existing.getContentStream());
} catch (IOException e) {
throw new GeneralException("Error reading existing content from X datastream", e);
}
}
}
// add the content stream
MIMETypedStream content = new MIMETypedStream(null, new ByteArrayInputStream(byteContent), null, byteContent.length);
newDS.putContentStream(content);
// checksum only needs recalc if we added a header
// note getChecksum() caters for checksum type set to disabled
if (addXMLHeader) {
logger.debug("Recalculating checksum. Type=" + newDS.DSChecksumType + " Existing checksum: " + newDS.DSChecksum != null ? newDS.DSChecksum : "none");
// forces computation rather than return existing
newDS.DSChecksum = Datastream.CHECKSUM_NONE;
newDS.DSChecksum = newDS.getChecksum();
logger.debug("New checksum: " + newDS.DSChecksum);
logger.debug("Testing new checksum, response is {}", newDS.compareChecksum());
}
w.addDatastream(newDS, true);
}