Search in sources :

Example 86 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.

the class ChannelsTest method testnewReaderCharsetError.

public void testnewReaderCharsetError() throws Exception {
    this.fins = new FileInputStream(tmpFile);
    ReadableByteChannel rbChannel = Channels.newChannel(this.fins);
    try {
        Channels.newReader(rbChannel, Charset.forName(BAD_CODE_SET).newDecoder(), -1);
        fail();
    } catch (UnsupportedCharsetException e) {
    // correct
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileInputStream(java.io.FileInputStream)

Example 87 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project wso2-axis2-transports by wso2.

the class JMSSender method createJMSMessage.

/**
 * Create a JMS Message from the given MessageContext and using the given
 * session
 *
 * @param msgContext the MessageContext
 * @param session    the JMS session
 * @param contentTypeProperty the message property to be used to store the
 *                            content type
 * @return a JMS message from the context and session
 * @throws JMSException on exception
 * @throws AxisFault on exception
 */
private Message createJMSMessage(MessageContext msgContext, Session session, String contentTypeProperty) throws JMSException, AxisFault {
    Message message = null;
    String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);
    // check the first element of the SOAP body, do we have content wrapped using the
    // default wrapper elements for binary (BaseConstants.DEFAULT_BINARY_WRAPPER) or
    // text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP messages
    // for JMS but just get the payload in its native format
    String jmsPayloadType = guessMessageType(msgContext);
    if (jmsPayloadType == null) {
        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
        } catch (AxisFault axisFault) {
            throw new JMSException("Unable to get the message formatter to use");
        }
        String contentType = messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction());
        boolean useBytesMessage = msgType != null && JMSConstants.JMS_BYTE_MESSAGE.equals(msgType) || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;
        OutputStream out;
        StringWriter sw;
        if (useBytesMessage) {
            BytesMessage bytesMsg = session.createBytesMessage();
            sw = null;
            out = new BytesMessageOutputStream(bytesMsg);
            message = bytesMsg;
        } else {
            sw = new StringWriter();
            try {
                out = new WriterOutputStream(sw, format.getCharSetEncoding());
            } catch (UnsupportedCharsetException ex) {
                handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
                return null;
            }
        }
        try {
            messageFormatter.writeTo(msgContext, format, out, true);
            out.close();
        } catch (IOException e) {
            rollbackIfXATransaction(msgContext);
            handleException("IO Error while creating BytesMessage", e);
        }
        if (!useBytesMessage) {
            TextMessage txtMsg = session.createTextMessage();
            txtMsg.setText(sw.toString());
            message = txtMsg;
        }
        if (contentTypeProperty != null) {
            message.setStringProperty(contentTypeProperty, contentType);
        }
    } else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
        message = session.createBytesMessage();
        BytesMessage bytesMsg = (BytesMessage) message;
        OMElement wrapper = msgContext.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
        OMNode omNode = wrapper.getFirstOMChild();
        if (omNode != null && omNode instanceof OMText) {
            Object dh = ((OMText) omNode).getDataHandler();
            if (dh != null && dh instanceof DataHandler) {
                try {
                    ((DataHandler) dh).writeTo(new BytesMessageOutputStream(bytesMsg));
                } catch (IOException e) {
                    rollbackIfXATransaction(msgContext);
                    handleException("Error serializing binary content of element : " + BaseConstants.DEFAULT_BINARY_WRAPPER, e);
                }
            }
        }
    } else if (JMSConstants.JMS_TEXT_MESSAGE.equals(jmsPayloadType)) {
        message = session.createTextMessage();
        TextMessage txtMsg = (TextMessage) message;
        txtMsg.setText(msgContext.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText());
    } else if (JMSConstants.JMS_MAP_MESSAGE.equalsIgnoreCase(jmsPayloadType)) {
        message = session.createMapMessage();
        JMSUtils.convertXMLtoJMSMap(msgContext.getEnvelope().getBody().getFirstChildWithName(JMSConstants.JMS_MAP_QNAME), (MapMessage) message);
    }
    // set the JMS correlation ID if specified
    String correlationId = getProperty(msgContext, JMSConstants.JMS_COORELATION_ID);
    if (correlationId == null && msgContext.getRelatesTo() != null) {
        correlationId = msgContext.getRelatesTo().getValue();
    }
    if (correlationId != null) {
        message.setJMSCorrelationID(correlationId);
    }
    if (msgContext.isServerSide()) {
        // set SOAP Action as a property on the JMS message
        setProperty(message, msgContext, BaseConstants.SOAPACTION);
    } else {
        String action = msgContext.getOptions().getAction();
        if (action != null) {
            message.setStringProperty(BaseConstants.SOAPACTION, action);
        }
    }
    JMSUtils.setTransportHeaders(msgContext, message);
    // set INTERNAL_TRANSACTION_COUNTED property in the JMS message if it is present in the messageContext
    setTransactionProperty(msgContext, message);
    return message;
}
Also used : AxisFault(org.apache.axis2.AxisFault) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) BytesMessageOutputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage) BytesMessageOutputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream) OMElement(org.apache.axiom.om.OMElement) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) DataHandler(javax.activation.DataHandler) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) OMNode(org.apache.axiom.om.OMNode) StringWriter(java.io.StringWriter) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OMText(org.apache.axiom.om.OMText) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) TextMessage(javax.jms.TextMessage)

Example 88 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project Payara by payara.

the class Utility method convertCharArrayToByteArray.

/**
 * Convert the char array to byte array with respect to given charset.
 *
 * @param charArray
 * @param strCharset  null or "" means default charset
 * @exception CharacterCodingException
 */
public static byte[] convertCharArrayToByteArray(char[] charArray, String strCharset) throws CharacterCodingException {
    if (charArray == null) {
        return null;
    }
    char[] cArray = (char[]) charArray.clone();
    CharBuffer charBuffer = CharBuffer.wrap(cArray);
    Charset charSet;
    if (strCharset == null || "".equals(strCharset)) {
        charSet = Charset.defaultCharset();
    } else if (Charset.isSupported(strCharset)) {
        charSet = Charset.forName(strCharset);
    } else {
        CharacterCodingException e = new CharacterCodingException();
        e.initCause(new UnsupportedCharsetException(strCharset));
        throw e;
    }
    CharsetEncoder encoder = charSet.newEncoder();
    ByteBuffer byteBuffer = null;
    try {
        byteBuffer = encoder.encode(charBuffer);
    } catch (CharacterCodingException cce) {
        throw cce;
    } catch (Throwable t) {
        CharacterCodingException e = new CharacterCodingException();
        e.initCause(t);
        throw e;
    }
    byte[] result = new byte[byteBuffer.remaining()];
    byteBuffer.get(result);
    clear(byteBuffer);
    clear(charBuffer);
    return result.clone();
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 89 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project Payara by payara.

the class Utility method convertByteArrayToCharArray.

/**
 * Convert the byte array to char array with respect to given charset.
 *
 * @param byteArray
 * @param charset  null or "" means default charset
 * @exception CharacterCodingException
 */
public static char[] convertByteArrayToCharArray(byte[] byteArray, String charset) throws CharacterCodingException {
    if (byteArray == null) {
        return null;
    }
    byte[] bArray = (byte[]) byteArray.clone();
    ByteBuffer byteBuffer = ByteBuffer.wrap(bArray);
    Charset charSet;
    if (charset == null || "".equals(charset)) {
        charSet = Charset.defaultCharset();
    } else if (Charset.isSupported(charset)) {
        charSet = Charset.forName(charset);
    } else {
        CharacterCodingException e = new CharacterCodingException();
        e.initCause(new UnsupportedCharsetException(charset));
        throw e;
    }
    CharsetDecoder decoder = charSet.newDecoder();
    CharBuffer charBuffer = null;
    try {
        charBuffer = decoder.decode(byteBuffer);
    } catch (CharacterCodingException cce) {
        throw cce;
    } catch (Throwable t) {
        CharacterCodingException e = new CharacterCodingException();
        e.initCause(t);
        throw e;
    }
    char[] result = toCharArray(charBuffer);
    clear(byteBuffer);
    clear(charBuffer);
    return result;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 90 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project hutool by looly.

the class HttpConnection method getCharset.

/**
 * 获取字符集编码<br>
 * 从Http连接的头信息中获得字符集<br>
 * 从ContentType中获取
 *
 * @return {@link Charset}编码
 * @since 3.0.9
 */
public Charset getCharset() {
    Charset charset = null;
    final String charsetName = getCharsetName();
    if (StrUtil.isNotBlank(charsetName)) {
        try {
            charset = Charset.forName(charsetName);
        } catch (UnsupportedCharsetException e) {
        // ignore
        }
    }
    return charset;
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset)

Aggregations

UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)116 Charset (java.nio.charset.Charset)55 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)43 IOException (java.io.IOException)35 File (java.io.File)11 ByteBuffer (java.nio.ByteBuffer)11 InputStream (java.io.InputStream)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 CoreException (org.eclipse.core.runtime.CoreException)10 CharacterCodingException (java.nio.charset.CharacterCodingException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HashMap (java.util.HashMap)7 MediaType (okhttp3.MediaType)7 Request (okhttp3.Request)7 Response (okhttp3.Response)7 ResponseBody (okhttp3.ResponseBody)7 Buffer (okio.Buffer)7 BufferedSource (okio.BufferedSource)7 InputStreamReader (java.io.InputStreamReader)6 OutputStream (java.io.OutputStream)6