List of usage examples for org.apache.commons.codec.binary StringUtils getBytesUsAscii
public static byte[] getBytesUsAscii(final String string)
From source file:com.mirth.connect.plugins.dicomviewer.DICOMViewer.java
public void viewAttachments(String channelId, Long messageId, String attachmentId) { // do viewing code try {/* w w w . j a v a 2 s . com*/ ConnectorMessage message = parent.messageBrowser.getSelectedConnectorMessage(); byte[] rawImage = StringUtils.getBytesUsAscii(parent.mirthClient.getDICOMMessage(message)); ByteArrayInputStream bis = new ByteArrayInputStream(rawImage); DICOM dcm = new DICOM(new Base64InputStream(bis)); // run() is required to create the dicom object. The argument serves multiple purposes. If it is null or empty, it opens a dialog to select a dicom file. // Otherwise, if dicom.show() is called, it is the title of the dialog. Since we are showing the dialog here, we pass the string we want to use as the title. dcm.run("DICOM Image Viewer"); dcm.show(); ImageWindow window = dcm.getWindow(); if (window != null) { Dimension dlgSize = window.getSize(); Dimension frmSize = parent.getSize(); Point loc = parent.getLocation(); if ((frmSize.width == 0 && frmSize.height == 0) || (loc.x == 0 && loc.y == 0)) { dcm.getWindow().setLocationRelativeTo(null); } else { dcm.getWindow().setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); } } else { parent.alertError(parent, "Unable to load DICOM attachment."); } } catch (Exception e) { parent.alertThrowable(parent, e); } }
From source file:net.oauth.jsontoken.crypto.AsciiStringSigner.java
/** * Signs the given ASCII string.//from w ww. ja v a 2s .c om * @throws SignatureException when the signature cannot be generated. */ public byte[] sign(String source) throws SignatureException { return signer.sign(StringUtils.getBytesUsAscii(source)); }
From source file:net.oauth.jsontoken.crypto.AsciiStringVerifier.java
/** * Verifies a signature on an ASCII string. * @param source the source that was signed. * @param signature the signature on the source. * @throws SignatureException if the signature doesn't verify. *//* w w w . j av a 2 s. co m*/ public void verifySignature(String source, byte[] signature) throws SignatureException { verifier.verifySignature(StringUtils.getBytesUsAscii(source), signature); }
From source file:com.mirth.connect.server.attachments.DICOMAttachmentHandler.java
@Override public void initialize(String message, Channel channel) throws AttachmentException { // Taking a string is much more inefficient than taking in a byte array. // If the user manually sends a message, it will arrive as a base64 encoded string, so we must support Strings for DICOM still. // However, DICOM messages that use this initializer should be relatively small in size. index = 0;/*from ww w .j ava2 s.c o m*/ try { dicomObject = DICOMConverter.byteArrayToDicomObject(StringUtils.getBytesUsAscii(message), true); dicomElement = dicomObject.remove(Tag.PixelData); } catch (Throwable t) { throw new AttachmentException(t); } }
From source file:com.mirth.connect.server.attachments.dicom.DICOMAttachmentHandler.java
@Override public void initialize(RawMessage message, Channel channel) throws AttachmentException { index = 0;/* w w w.j a v a2 s. c om*/ try { byte[] messageBytes = null; boolean decode = false; if (message.isBinary()) { messageBytes = message.getRawBytes(); } else { // Taking a string is much more inefficient than taking in a byte array. // If the user manually sends a message, it will arrive as a base64 encoded string, so we must support Strings for DICOM still. // However, DICOM messages that use this initializer should be relatively small in size. messageBytes = StringUtils.getBytesUsAscii(message.getRawData()); decode = true; } dicomObject = DICOMConverter.byteArrayToDicomObject(messageBytes, decode); dicomElement = dicomObject.remove(Tag.PixelData); attachmentId = ServerUUIDGenerator.getUUID(); } catch (Throwable t) { throw new AttachmentException(t); } }
From source file:com.mirth.connect.server.util.DICOMMessageUtil.java
public static byte[] getDICOMRawBytes(ImmutableConnectorMessage message) { byte[] mergedMessage = null; List<Attachment> attachments = MessageController.getInstance().getMessageAttachment(message.getChannelId(), message.getMessageId());// w w w . j a v a 2s. c o m if (attachments != null && attachments.size() > 0) { try { if (attachments.get(0).getType().equals("DICOM")) { mergedMessage = mergeHeaderAttachments(message, attachments); } else { mergedMessage = Base64.decodeBase64(StringUtils.getBytesUsAscii(message.getRaw().getContent())); } } catch (Exception e) { logger.error("Error merging DICOM data", e); mergedMessage = Base64.decodeBase64(StringUtils.getBytesUsAscii(message.getRaw().getContent())); } } else { mergedMessage = Base64.decodeBase64(StringUtils.getBytesUsAscii(message.getRaw().getContent())); } return mergedMessage; }
From source file:com.mirth.connect.server.util.DICOMMessageUtil.java
public static byte[] mergeHeaderAttachments(ImmutableConnectorMessage message, List<Attachment> attachments) throws MessageSerializerException { try {/* w ww .j ava2s .co m*/ byte[] headerBytes; ImmutableMessageContent encoded = message.getEncoded(); ImmutableMessageContent raw = message.getRaw(); if (encoded != null && encoded.getContent() != null && encoded.getDataType().equals("DICOM")) { headerBytes = Base64.decodeBase64(StringUtils.getBytesUsAscii(encoded.getContent())); } else if (raw != null && raw.getContent() != null && raw.getDataType().equals("DICOM")) { headerBytes = Base64.decodeBase64(StringUtils.getBytesUsAscii(raw.getContent())); } else { return new byte[0]; } return mergeHeaderPixelData(headerBytes, attachments); } catch (IOException e) { throw new MessageSerializerException(e); } }
From source file:kr.debop4j.core.tools.StringTool.java
/** * ?? ? (,?, 2? ??? ) ? ?.// w w w . jav a 2s .co m * * @param str ? * @return ? ? true, false */ public static boolean isMultiByteString(final String str) { if (isWhiteSpace(str)) return false; try { byte[] bytes = StringUtils.getBytesUsAscii(str.substring(0, Math.min(2, str.length()))); return isMultiByteString(bytes); } catch (Exception e) { log.error("? ?? ?? . str=" + ellipsisChar(str, 24), e); return false; } }
From source file:com.mirth.connect.server.util.AttachmentUtil.java
public static byte[] reAttachMessage(String raw, ImmutableConnectorMessage connectorMessage, String charsetEncoding, boolean binary) { try {//from ww w . java2 s .c o m Map<Integer, Map<Integer, Object>> replacementObjects = new TreeMap<Integer, Map<Integer, Object>>(); // Determine the buffersize during the first pass for better memory performance int bufferSize = raw.length(); int index = 0; int endIndex; // Initialize the objects here so only one retrieval of the attachment content is ever needed. byte[] dicomObject = null; Map<String, Attachment> attachmentMap = null; // Handle the special case if only a dicom message is requested. // In this case we can skip any byte appending and thus do not need to base64 encode the dicom object // if the type is binary. if (raw.trim().equals(PREFIX + DICOM_KEY + SUFFIX)) { dicomObject = DICOMUtil.getDICOMRawBytes(connectorMessage); if (!binary) { dicomObject = Base64Util.encodeBase64(dicomObject); } return dicomObject; } // Check the raw string in one pass for any attachments. // Stores the start and end indices to replace, along with the attachment content. while ((index = raw.indexOf(PREFIX, index)) != -1) { if (raw.startsWith(DICOM_KEY + SUFFIX, index + PREFIX.length())) { if (dicomObject == null) { // Unfortunately, if the dicom data needs to appended to other base64 data, it must be done so in base64. dicomObject = Base64Util.encodeBase64(DICOMUtil.getDICOMRawBytes(connectorMessage)); } endIndex = index + PREFIX.length() + DICOM_KEY.length() + SUFFIX.length(); Map<Integer, Object> replacementMap = new HashMap<Integer, Object>(); replacementMap.put(KEY_END_INDEX, endIndex); replacementMap.put(KEY_DATA, dicomObject); replacementObjects.put(index, replacementMap); bufferSize += dicomObject.length; index += endIndex - index; } else if (raw.startsWith(ATTACHMENT_KEY, index + PREFIX.length())) { if (attachmentMap == null) { List<Attachment> list = getMessageAttachments(connectorMessage); // Store the attachments in a map with the attachment's Id as the key attachmentMap = new HashMap<String, Attachment>(); for (Attachment attachment : list) { attachmentMap.put(attachment.getId(), attachment); } } int attachmentIdStartIndex = index + PREFIX.length() + ATTACHMENT_KEY.length(); int attachmentIdEndIndex = attachmentIdStartIndex + ATTACHMENT_ID_LENGTH; endIndex = attachmentIdEndIndex + SUFFIX.length(); String attachmentId = raw.substring(attachmentIdStartIndex, attachmentIdStartIndex + ATTACHMENT_ID_LENGTH); if (raw.substring(attachmentIdEndIndex, endIndex).equals(SUFFIX)) { Map<Integer, Object> replacementMap = new HashMap<Integer, Object>(); replacementMap.put(KEY_END_INDEX, endIndex); if (attachmentMap.containsKey(attachmentId)) { Attachment attachment = attachmentMap.get(attachmentId); replacementMap.put(KEY_DATA, attachment.getContent()); bufferSize += attachment.getContent().length; } else { replacementMap.put(KEY_DATA, new byte[0]); } replacementObjects.put(index, replacementMap); } } else { endIndex = index + PREFIX.length(); } index += endIndex - index; } // Release the object pointers of the attachment content so they aren't held in memory for the entire method dicomObject = null; attachmentMap = null; // Initialize the stream's buffer size. The buffer size will always be slightly large than needed, // because the template keys are never removed from the buffer size. // It is not worth doing any extra calculations for the amount of memory saved. ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize); int segmentStartIndex = 0; for (Map.Entry<Integer, Map<Integer, Object>> entry : replacementObjects.entrySet()) { int startReplacementIndex = entry.getKey(); int endReplacementIndex = (Integer) entry.getValue().get(KEY_END_INDEX); byte[] data = (byte[]) entry.getValue().get(KEY_DATA); // Allows the memory used by the attachments to be released at the end of the loop entry.getValue().clear(); byte[] templateSegment; // If the data is binary, the content should be in base64, so using US-ASCII as the charset encoding should be sufficient. if (binary) { templateSegment = StringUtils .getBytesUsAscii(raw.substring(segmentStartIndex, startReplacementIndex)); } else { templateSegment = StringUtil.getBytesUncheckedChunked( raw.substring(segmentStartIndex, startReplacementIndex), Constants.ATTACHMENT_CHARSET); } baos.write(templateSegment); baos.write(data); segmentStartIndex = endReplacementIndex; } byte[] templateSegment; if (binary) { templateSegment = StringUtils.getBytesUsAscii(raw.substring(segmentStartIndex)); } else { templateSegment = StringUtil.getBytesUncheckedChunked(raw.substring(segmentStartIndex), Constants.ATTACHMENT_CHARSET); } byte[] combined; // If there are no attachments, don't bother writing to the output stream. if (segmentStartIndex == 0) { combined = templateSegment; } else { // Write the segment after the last replacement. baos.write(templateSegment); combined = baos.toByteArray(); // Release the memory used by the byte array stream. ByteArrayOutputStreams do not need to be closed. baos = null; } templateSegment = null; // If binary, the content should be in base64 so it is necessary to decode the data. if (binary) { combined = Base64Util.decodeBase64(combined); } else if (charsetEncoding != null && !charsetEncoding.toUpperCase().equals(Constants.ATTACHMENT_CHARSET.toUpperCase())) { // Convert the byte array to a string using the internal encoding. String combinedString = StringUtils.newString(combined, Constants.ATTACHMENT_CHARSET); // First release the reference to the old byte data so it can be reallocated if necessary. combined = null; // Convert the string to a byte array using the requested encoding combined = StringUtil.getBytesUncheckedChunked(combinedString, charsetEncoding); } return combined; } catch (Exception e) { logger.error("Error reattaching attachments", e); return null; } }
From source file:com.mirth.connect.server.util.MessageAttachmentUtil.java
public static byte[] reAttachMessage(String raw, ImmutableConnectorMessage connectorMessage, String charsetEncoding, boolean binary) { try {/*from w ww. j a va 2 s . c o m*/ Map<Integer, Map<Integer, Object>> replacementObjects = new TreeMap<Integer, Map<Integer, Object>>(); // Determine the buffersize during the first pass for better memory performance int bufferSize = raw.length(); int index = 0; int endIndex; // Initialize the objects here so only one retrieval of the attachment content is ever needed. byte[] dicomObject = null; Map<String, Attachment> attachmentMap = null; // Handle the special case if only a dicom message is requested. // In this case we can skip any byte appending and thus do not need to base64 encode the dicom object // if the type is binary. if (raw.trim().equals(PREFIX + DICOM_KEY + SUFFIX)) { dicomObject = DICOMMessageUtil.getDICOMRawBytes(connectorMessage); if (!binary) { dicomObject = Base64Util.encodeBase64(dicomObject); } return dicomObject; } // Check the raw string in one pass for any attachments. // Stores the start and end indices to replace, along with the attachment content. while ((index = raw.indexOf(PREFIX, index)) != -1) { if (raw.startsWith(DICOM_KEY + SUFFIX, index + PREFIX.length())) { if (dicomObject == null) { // Unfortunately, if the dicom data needs to appended to other base64 data, it must be done so in base64. dicomObject = Base64Util.encodeBase64(DICOMMessageUtil.getDICOMRawBytes(connectorMessage)); } endIndex = index + PREFIX.length() + DICOM_KEY.length() + SUFFIX.length(); Map<Integer, Object> replacementMap = new HashMap<Integer, Object>(); replacementMap.put(KEY_END_INDEX, endIndex); replacementMap.put(KEY_DATA, dicomObject); replacementObjects.put(index, replacementMap); bufferSize += dicomObject.length; index += endIndex - index; } else if (raw.startsWith(ATTACHMENT_KEY, index + PREFIX.length())) { if (attachmentMap == null) { List<Attachment> list = getMessageAttachments(connectorMessage); // Store the attachments in a map with the attachment's Id as the key attachmentMap = new HashMap<String, Attachment>(); for (Attachment attachment : list) { attachmentMap.put(attachment.getId(), attachment); } } int attachmentIdStartIndex = index + PREFIX.length() + ATTACHMENT_KEY.length(); int attachmentIdEndIndex = attachmentIdStartIndex + ATTACHMENT_ID_LENGTH; endIndex = attachmentIdEndIndex + SUFFIX.length(); String attachmentId = raw.substring(attachmentIdStartIndex, attachmentIdStartIndex + ATTACHMENT_ID_LENGTH); if (raw.substring(attachmentIdEndIndex, endIndex).equals(SUFFIX)) { Map<Integer, Object> replacementMap = new HashMap<Integer, Object>(); replacementMap.put(KEY_END_INDEX, endIndex); if (attachmentMap.containsKey(attachmentId)) { Attachment attachment = attachmentMap.get(attachmentId); replacementMap.put(KEY_DATA, attachment.getContent()); bufferSize += attachment.getContent().length; } else { replacementMap.put(KEY_DATA, new byte[0]); } replacementObjects.put(index, replacementMap); } } else { endIndex = index + PREFIX.length(); } index += endIndex - index; } // Release the object pointers of the attachment content so they aren't held in memory for the entire method dicomObject = null; attachmentMap = null; // Initialize the stream's buffer size. The buffer size will always be slightly large than needed, // because the template keys are never removed from the buffer size. // It is not worth doing any extra calculations for the amount of memory saved. ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize); int segmentStartIndex = 0; for (Map.Entry<Integer, Map<Integer, Object>> entry : replacementObjects.entrySet()) { int startReplacementIndex = entry.getKey(); int endReplacementIndex = (Integer) entry.getValue().get(KEY_END_INDEX); byte[] data = (byte[]) entry.getValue().get(KEY_DATA); // Allows the memory used by the attachments to be released at the end of the loop entry.getValue().clear(); byte[] templateSegment; // If the data is binary, the content should be in base64, so using US-ASCII as the charset encoding should be sufficient. if (binary) { templateSegment = StringUtils .getBytesUsAscii(raw.substring(segmentStartIndex, startReplacementIndex)); } else { templateSegment = StringUtil.getBytesUncheckedChunked( raw.substring(segmentStartIndex, startReplacementIndex), Constants.ATTACHMENT_CHARSET); } baos.write(templateSegment); baos.write(data); segmentStartIndex = endReplacementIndex; } byte[] templateSegment; if (binary) { templateSegment = StringUtils.getBytesUsAscii(raw.substring(segmentStartIndex)); } else { templateSegment = StringUtil.getBytesUncheckedChunked(raw.substring(segmentStartIndex), Constants.ATTACHMENT_CHARSET); } byte[] combined; // If there are no attachments, don't bother writing to the output stream. if (segmentStartIndex == 0) { combined = templateSegment; } else { // Write the segment after the last replacement. baos.write(templateSegment); combined = baos.toByteArray(); // Release the memory used by the byte array stream. ByteArrayOutputStreams do not need to be closed. baos = null; } templateSegment = null; // If binary, the content should be in base64 so it is necessary to decode the data. if (binary) { combined = Base64Util.decodeBase64(combined); } else if (charsetEncoding != null && !charsetEncoding.toUpperCase().equals(Constants.ATTACHMENT_CHARSET.toUpperCase())) { // Convert the byte array to a string using the internal encoding. String combinedString = StringUtils.newString(combined, Constants.ATTACHMENT_CHARSET); // First release the reference to the old byte data so it can be reallocated if necessary. combined = null; // Convert the string to a byte array using the requested encoding combined = StringUtil.getBytesUncheckedChunked(combinedString, charsetEncoding); } return combined; } catch (Exception e) { logger.error("Error reattaching attachments", e); return null; } }