Java Code Examples for java.io.UnsupportedEncodingException#toString()

The following examples show how to use java.io.UnsupportedEncodingException#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ByteArrayDataSource.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a new ByteArrayDataSource object.
 *
 * @param data
 *            The data
 * @param type
 *            The parameters
 */
public ByteArrayDataSource( String data, String type )
{
    try
    {
        // Assumption that the string contains only ASCII
        // characters! Otherwise just pass a charset into this
        // constructor and use it in getBytes()
        _data = data.getBytes( AppPropertiesService.getProperty( PROPERTY_CHARSET ) );
    }
    catch( UnsupportedEncodingException uex )
    {
        throw new AppException( uex.toString( ) );
    }

    _type = type;
}
 
Example 2
Source File: NodeState.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        return toString("").trim();
    }
    catch (UnsupportedEncodingException e) {
        return e.toString();
    }
}
 
Example 3
Source File: GZIPHeader.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
public String getComment(){
  if(comment==null) return "";
  try {
    return new String(comment, "ISO-8859-1");
  }
  catch (UnsupportedEncodingException e) {
    throw new InternalError(e.toString());
  }
}
 
Example 4
Source File: CertificateHolderReference.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public byte[] getEncoded()
{
    String ref = countryCode + holderMnemonic + sequenceNumber;

    try
    {
        return ref.getBytes(ReferenceEncoding);
    }
    catch (UnsupportedEncodingException e)
    {
        throw new IllegalStateException(e.toString());
    }
}
 
Example 5
Source File: GZIPHeader.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
public String getComment(){
  if(comment==null) return "";
  try {
    return new String(comment, "ISO-8859-1");
  }
  catch (UnsupportedEncodingException e) {
    throw new InternalError(e.toString());
  }
}
 
Example 6
Source File: B64Code.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
/**
 * Base 64 encode as described in RFC 1421.
 * <p>Does not insert whitespace as described in RFC 1521.
 * @param s String to encode.
 * @return String containing the encoded form of the input.
 */
static public String encode(String s)
{
    try
    {
        return encode(s,null);
    }
    catch (UnsupportedEncodingException e)
    {
        throw new IllegalArgumentException(e.toString());
    }
}
 
Example 7
Source File: B64Code.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
/**
 * Base 64 encode as described in RFC 1421.
 * <p>Does not insert whitespace as described in RFC 1521.
 * @param s String to encode.
 * @return String containing the encoded form of the input.
 */
static public String encode(String s)
{
    try
    {
        return encode(s,null);
    }
    catch (UnsupportedEncodingException e)
    {
        throw new IllegalArgumentException(e.toString());
    }
}
 
Example 8
Source File: Uri.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This method returns the length of a URI segment. The length of the URI
 * segment is defined as the number of bytes in the unescaped, UTF-8 encoded
 * representation of the segment.
 * <p>
 * The method verifies that the URI segment is well-formed.
 * 
 * @param segment the URI segment
 * @return URI segment length
 * @throws NullPointerException if the specified segment is {@code null}
 * @throws IllegalArgumentException if the specified URI segment is
 *         malformed
 */
private static int getSegmentLength(String segment) {
	if (segment.length() == 0)
		throw new IllegalArgumentException("URI segment is empty.");

	StringBuffer newsegment = new StringBuffer(segment);
	int i = 0;
	while (i < newsegment.length()) { // length can decrease during the
										// loop!
		if (newsegment.charAt(i) == '\\') {
			if (i == newsegment.length() - 1) // last character cannot be a
												// '\'
				throw new IllegalArgumentException("URI segment ends with the escape character.");

			newsegment.deleteCharAt(i); // remove the extra '\'
		} else
			if (newsegment.charAt(i) == '/')
				throw new IllegalArgumentException("URI segment contains an unescaped '/' character.");

		i++;
	}

	if (newsegment.toString().equals(".."))
		throw new IllegalArgumentException("URI segment must not be \"..\".");

	try {
		return newsegment.toString().getBytes("UTF-8").length;
	} catch (UnsupportedEncodingException e) {
		// This should never happen. All implementations must support
		// UTF-8 encoding;
		throw new RuntimeException(e.toString());
	}
}
 
Example 9
Source File: CharUtil.java    From SpringBootUnity with MIT License 5 votes vote down vote up
/**
 * 转换编码 ISO-8859-1到GB2312
 */
public static String iso2gb(String text) {
    String result;
    try {
        result = new String(text.getBytes(StandardCharsets.ISO_8859_1), "GB2312");
    } catch (UnsupportedEncodingException ex) {
        result = ex.toString();
    }
    return result;
}
 
Example 10
Source File: Text.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * Does a URL decoding of the {@code string} using the
 * {@code escape} character. Please note that in opposite to the
 * {@link java.net.URLDecoder} it does not transform the + into spaces.
 *
 * @param string the string to decode
 * @param escape the escape character
 * @return the decoded string
 * @throws NullPointerException           if {@code string} is {@code null}.
 * @throws IllegalArgumentException       if the 2 characters following the escape
 *                                        character do not represent a hex-number
 *                                        or if not enough characters follow an
 *                                        escape character
 */
public static String unescape(String string, char escape)  {
    try {
        byte[] utf8 = string.getBytes("utf-8");

        // Check whether escape occurs at invalid position
        if ((utf8.length >= 1 && utf8[utf8.length - 1] == escape) ||
            (utf8.length >= 2 && utf8[utf8.length - 2] == escape)) {
            throw new IllegalArgumentException("Premature end of escape sequence at end of input");
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream(utf8.length);
        for (int k = 0; k < utf8.length; k++) {
            byte b = utf8[k];
            if (b == escape) {
                out.write((decodeDigit(utf8[++k]) << 4) + decodeDigit(utf8[++k]));
            }
            else {
                out.write(b);
            }
        }

        return new String(out.toByteArray(), "utf-8");
    }
    catch (UnsupportedEncodingException e) {
        throw new InternalError(e.toString());
    }
}
 
Example 11
Source File: GZIPHeader.java    From jtransc with Apache License 2.0 5 votes vote down vote up
public String getName(){
  if(name==null) return "";
  try {
    return new String(name, "ISO-8859-1");
  }
  catch (UnsupportedEncodingException e) {
    throw new InternalError(e.toString());
  }
}
 
Example 12
Source File: TestParameters.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public String toString() {
    try {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        if (values.length == 0) {
            return URLEncoder.encode(name, "UTF-8");
        }
        for (String value : values) {
            if (first) {
                first = false;
            } else {
                result.append('&');
            }
            if (name != null) {
                result.append(URLEncoder.encode(name, "UTF-8"));
            }
            if (value != null) {
                result.append('=');
                result.append(URLEncoder.encode(value, "UTF-8"));
            }
        }
        return result.toString();
    } catch (UnsupportedEncodingException ex) {
        return ex.toString();
    }
}
 
Example 13
Source File: TestParameters.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        if (values.length == 0) {
            return URLEncoder.encode(name, "UTF-8");
        }
        for (String value : values) {
            if (first) {
                first = false;
            } else {
                result.append('&');
            }
            if (name != null) {
                result.append(URLEncoder.encode(name, "UTF-8"));
            }
            if (value != null) {
                result.append('=');
                result.append(URLEncoder.encode(value, "UTF-8"));
            }
        }
        return result.toString();
    } catch (UnsupportedEncodingException ex) {
        return ex.toString();
    }
}
 
Example 14
Source File: MockRequestExecutor.java    From esigate with Apache License 2.0 5 votes vote down vote up
private CloseableHttpResponse getResource(String url) throws HttpErrorPage {
    String result = resources.get(url);

    if (result == null) {
        throw new HttpErrorPage(HttpStatus.SC_NOT_FOUND, "Not found", "The page: " + url + " does not exist");
    }
    try {
        return new HttpResponseBuilder().status(HttpStatus.SC_OK).reason("OK").entity(result).build();
    } catch (UnsupportedEncodingException e) {
        throw new HttpErrorPage(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString(), e.toString());
    }
}
 
Example 15
Source File: Exec.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new OutputBufferThread to consume the given InputStream.
 * 
 * @param is InputStream to consume
 */
public OutputBufferThread(InputStream is) {
  this.setDaemon(true);
  output = new ArrayList<String>();
  try {
    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  } catch (UnsupportedEncodingException e) {
    throw new RuntimeException("Unsupported encoding " + e.toString());
  }
}
 
Example 16
Source File: BEValue.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns this BEValue as a String, interpreted with the specified
 * encoding.
 *
 * @param encoding The encoding to interpret the bytes as when converting
 * them into a {@link String}.
 * @throws InvalidBEncodingException If the value is not a byte[].
 */
public String getString(String encoding) throws InvalidBEncodingException {
	try {
		return new String(this.getBytes(), encoding);
	} catch (ClassCastException cce) {
		throw new InvalidBEncodingException(cce.toString());
	} catch (UnsupportedEncodingException uee) {
		throw new InternalError(uee.toString());
	}
}
 
Example 17
Source File: Exec.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new OutputBufferThread to consume the given InputStream.
 * 
 * @param is InputStream to consume
 */
public OutputBufferThread(InputStream is) {
  this.setDaemon(true);
  output = new ArrayList<String>();
  try {
    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  } catch (UnsupportedEncodingException e) {
    throw new RuntimeException("Unsupported encoding " + e.toString());
  }
}
 
Example 18
Source File: CertificateHolderReference.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public byte[] getEncoded()
{
    String ref = countryCode + holderMnemonic + sequenceNumber;

    try
    {
        return ref.getBytes(ReferenceEncoding);
    }
    catch (UnsupportedEncodingException e)
    {
        throw new IllegalStateException(e.toString());
    }
}
 
Example 19
Source File: TestParameters.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        if (values.length == 0) {
            return URLEncoder.encode(name, "UTF-8");
        }
        for (String value : values) {
            if (first) {
                first = false;
            } else {
                result.append('&');
            }
            if (name != null) {
                result.append(URLEncoder.encode(name, "UTF-8"));
            }
            if (value != null) {
                result.append('=');
                result.append(URLEncoder.encode(value, "UTF-8"));
            }
        }
        return result.toString();
    } catch (UnsupportedEncodingException ex) {
        return ex.toString();
    }
}
 
Example 20
Source File: GZIPHeader.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public String getComment(){
  if(comment==null) return "";
  try {
    return new String(comment, "ISO-8859-1");
  }
  catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e.toString());
  }
}