Search in sources :

Example 1 with InputStreamDataWriter

use of org.dcm4che3.net.InputStreamDataWriter in project karnak by OsiriX-Foundation.

the class ForwardService method transfer.

public List<File> transfer(ForwardDicomNode sourceNode, DicomForwardDestination destination, Attributes copy, Params p) throws IOException {
    StoreFromStreamSCU streamSCU = destination.getStreamSCU();
    DicomInputStream in = null;
    List<File> files;
    Attributes attributesOriginal = new Attributes();
    Attributes attributesToSend = new Attributes();
    try {
        if (!streamSCU.isReadyForDataTransfer()) {
            throw new IllegalStateException("Association not ready for transfer.");
        }
        DataWriter dataWriter;
        String cuid = p.getCuid();
        String iuid = p.getIuid();
        String tsuid = p.getTsuid();
        var syntax = new AdaptTransferSyntax(tsuid, streamSCU.selectTransferSyntax(cuid, destination.getOutputTransferSyntax(tsuid)));
        List<AttributeEditor> editors = destination.getDicomEditors();
        if (copy == null && editors.isEmpty() && syntax.getRequested().equals(tsuid)) {
            dataWriter = new InputStreamDataWriter(p.getData());
            attributesToSend = new DicomInputStream(p.getData()).readDataset();
            attributesOriginal.addAll(attributesToSend);
        } else {
            AttributeEditorContext context = new AttributeEditorContext(tsuid, sourceNode, streamSCU.getRemoteDicomNode());
            in = new DicomInputStream(p.getData(), tsuid);
            in.setIncludeBulkData(IncludeBulkData.URI);
            Attributes attributes = in.readDataset();
            attributesOriginal.addAll(attributes);
            attributesToSend = attributes;
            if (copy != null) {
                copy.addAll(attributes);
            }
            if (!editors.isEmpty()) {
                editors.forEach(e -> e.apply(attributes, context));
                iuid = attributes.getString(Tag.SOPInstanceUID);
                cuid = attributes.getString(Tag.SOPClassUID);
            }
            if (context.getAbort() == Abort.FILE_EXCEPTION) {
                if (p.getData() instanceof PDVInputStream) {
                    ((PDVInputStream) p.getData()).skipAll();
                }
                throw new AbortException(context.getAbort(), context.getAbortMessage());
            } else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
                if (p.getAs() != null) {
                    p.getAs().abort();
                }
                throw new AbortException(context.getAbort(), "DICOM association abort: " + context.getAbortMessage());
            }
            BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
            Editable<PlanarImage> editable = transformImage(attributes, context);
            dataWriter = ImageAdapter.buildDataWriter(attributes, syntax, editable, desc);
        }
        streamSCU.cstore(cuid, iuid, p.getPriority(), dataWriter, syntax.getSuitable());
        progressNotify(destination, p.getIuid(), p.getCuid(), false, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
    } catch (AbortException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
            throw e;
        }
    } catch (IOException e) {
        progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        throw e;
    } catch (Exception e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
        monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
        LOGGER.error(ERROR_WHEN_FORWARDING, e);
    } finally {
        streamSCU.triggerCloseExecutor();
        files = cleanOrGetBulkDataFiles(in, copy == null);
    }
    return files;
}
Also used : StoreFromStreamSCU(org.weasis.dicom.util.StoreFromStreamSCU) Attributes(org.dcm4che3.data.Attributes) IOException(java.io.IOException) DicomInputStream(org.dcm4che3.io.DicomInputStream) HttpException(org.weasis.dicom.web.HttpException) AbortException(org.karnak.backend.exception.AbortException) IOException(java.io.IOException) InputStreamDataWriter(org.dcm4che3.net.InputStreamDataWriter) AttributeEditorContext(org.weasis.dicom.param.AttributeEditorContext) AttributeEditor(org.weasis.dicom.param.AttributeEditor) AdaptTransferSyntax(org.dcm4che3.img.stream.ImageAdapter.AdaptTransferSyntax) BytesWithImageDescriptor(org.dcm4che3.img.stream.BytesWithImageDescriptor) File(java.io.File) PlanarImage(org.weasis.opencv.data.PlanarImage) DataWriter(org.dcm4che3.net.DataWriter) InputStreamDataWriter(org.dcm4che3.net.InputStreamDataWriter) PDVInputStream(org.dcm4che3.net.PDVInputStream) AbortException(org.karnak.backend.exception.AbortException)

Example 2 with InputStreamDataWriter

use of org.dcm4che3.net.InputStreamDataWriter in project dcm4che by dcm4che.

the class BasicRetrieveTask method createDataWriter.

protected DataWriter createDataWriter(T inst, String tsuid) throws Exception {
    DicomInputStream in = new DicomInputStream(inst.getFile());
    in.readFileMetaInformation();
    return new InputStreamDataWriter(in);
}
Also used : InputStreamDataWriter(org.dcm4che3.net.InputStreamDataWriter) DicomInputStream(org.dcm4che3.io.DicomInputStream)

Example 3 with InputStreamDataWriter

use of org.dcm4che3.net.InputStreamDataWriter in project dcm4che by dcm4che.

the class StoreSCU method send.

public void send(final File f, long fmiEndPos, String cuid, String iuid, String filets) throws IOException, InterruptedException, ParserConfigurationException, SAXException {
    String ts = selectTransferSyntax(cuid, filets);
    if (f.getName().endsWith(".xml")) {
        Attributes parsedDicomFile = SAXReader.parse(new FileInputStream(f));
        if (CLIUtils.updateAttributes(parsedDicomFile, attrs, uidSuffix))
            iuid = parsedDicomFile.getString(Tag.SOPInstanceUID);
        if (!ts.equals(filets)) {
            Decompressor.decompress(parsedDicomFile, filets);
        }
        as.cstore(cuid, iuid, priority, new DataWriterAdapter(parsedDicomFile), ts, rspHandlerFactory.createDimseRSPHandler(f));
    } else {
        if (uidSuffix == null && attrs.isEmpty() && ts.equals(filets)) {
            FileInputStream in = new FileInputStream(f);
            try {
                in.skip(fmiEndPos);
                InputStreamDataWriter data = new InputStreamDataWriter(in);
                as.cstore(cuid, iuid, priority, data, ts, rspHandlerFactory.createDimseRSPHandler(f));
            } finally {
                SafeClose.close(in);
            }
        } else {
            DicomInputStream in = new DicomInputStream(f);
            try {
                in.setIncludeBulkData(IncludeBulkData.URI);
                Attributes data = in.readDataset();
                if (CLIUtils.updateAttributes(data, attrs, uidSuffix))
                    iuid = data.getString(Tag.SOPInstanceUID);
                if (!ts.equals(filets)) {
                    Decompressor.decompress(data, filets);
                }
                as.cstore(cuid, iuid, priority, new DataWriterAdapter(data), ts, rspHandlerFactory.createDimseRSPHandler(f));
            } finally {
                SafeClose.close(in);
            }
        }
    }
}
Also used : Attributes(org.dcm4che3.data.Attributes) DicomInputStream(org.dcm4che3.io.DicomInputStream)

Aggregations

DicomInputStream (org.dcm4che3.io.DicomInputStream)3 Attributes (org.dcm4che3.data.Attributes)2 InputStreamDataWriter (org.dcm4che3.net.InputStreamDataWriter)2 File (java.io.File)1 IOException (java.io.IOException)1 BytesWithImageDescriptor (org.dcm4che3.img.stream.BytesWithImageDescriptor)1 AdaptTransferSyntax (org.dcm4che3.img.stream.ImageAdapter.AdaptTransferSyntax)1 DataWriter (org.dcm4che3.net.DataWriter)1 PDVInputStream (org.dcm4che3.net.PDVInputStream)1 AbortException (org.karnak.backend.exception.AbortException)1 AttributeEditor (org.weasis.dicom.param.AttributeEditor)1 AttributeEditorContext (org.weasis.dicom.param.AttributeEditorContext)1 StoreFromStreamSCU (org.weasis.dicom.util.StoreFromStreamSCU)1 HttpException (org.weasis.dicom.web.HttpException)1 PlanarImage (org.weasis.opencv.data.PlanarImage)1