Example usage for java.io UnsupportedEncodingException UnsupportedEncodingException

List of usage examples for java.io UnsupportedEncodingException UnsupportedEncodingException

Introduction

In this page you can find the example usage for java.io UnsupportedEncodingException UnsupportedEncodingException.

Prototype

public UnsupportedEncodingException(String s) 

Source Link

Document

Constructs an UnsupportedEncodingException with a detail message.

Usage

From source file:edu.harvard.liblab.ecru.SolrClient.java

/**
 * URL encodes against UTF-8, then changes all '+' to "%20"
 * /*w ww .  jav a2  s .  c o  m*/
 * @param str
 * @return
 */
private String encode(String str) throws UnsupportedEncodingException {
    String retVal = str;
    try {
        retVal = URLEncoder.encode(str, "UTF-8");
        retVal = retVal.replaceAll("\\+", "%20");
    } catch (Exception e) {
        throw new UnsupportedEncodingException(e.getMessage());
    }
    return retVal;
}

From source file:hu.sztaki.lpds.pgportal.services.dspace.DSpaceUtil.java

/**
 * Handles a workflow download from the repository.
 *
 * This function will connect to DSpace through LNI and download a DSpace
 * Dissemination Information Package (DIP) from a specified handle. The
 * compressed workflow is then extracted from the DIP (it will be in the
 * form of 'bitstream_<#>.zip'). This compressed workflow is then uploaded
 * into the user's workflow sotrage space on guse.
 *
 * @param handle/*from  ww w .  j a v a2s. com*/
 * @param user
 * @param h storage datas, keys: newGrafName,newAbstName,newRealName
 */
public void importFromDspace(String handle, String user, Hashtable h)
        throws UnsupportedEncodingException, IOException, HttpException, Exception {
    if ("".equals(handle.trim())) {
        throw new UnsupportedEncodingException("");
    }
    String[] parts = handle.split("/");
    handle = "";
    for (int i = 0; i < parts.length; i++) {
        // Make sure we don't pass a handle with illegal URL characters
        // This should only happen if a user enters the handle incorrectly
        // If there's a problem, handle will be invalid anyways , throws UnsupportedEncodingException
        handle = handle.concat(URLEncoder.encode(parts[i].trim(), "UTF-8"));

        if (i + 1 != parts.length) {
            handle = handle.concat("/");
        }
    }

    // Create LNI client
    LNIclient lni = new LNIclient(getDSpaceURL() + "lni/dav/", anonDSpaceUser, anonDSpacePass);

    String dspaceDIP_string = getTempdir() + user + DSPACE_FILE_DSCRIMINATOR + ".zip";
    purge(dspaceDIP_string);
    try {
        // Start the GET from DSpace
        InputStream is = lni.startGet(handle, "METS", null);
        // Read the input stream from DSpace and extract bitstream
        // We have the DIP, now we would like the Item ()
        // TODO: Note that we are expecting a zip file here. Perhaps a more flexible method can be implemented.
        File bitstream = new File(dspaceDIP_string);
        unzipBitstream(is, bitstream);
        // Finish GET process
        lni.finishGet();
        uploadToStorage(user, bitstream, h);
    } finally {
        purge(dspaceDIP_string);
    }
}

From source file:org.eclipse.smarthome.binding.homematic.internal.communicator.message.BinRpcMessage.java

private void validateBinXSignature(byte[] sig) throws UnsupportedEncodingException {
    if (sig[0] != 'B' || sig[1] != 'i' || sig[2] != 'n') {
        throw new UnsupportedEncodingException("No BinX signature");
    }//  w w w . ja v a  2 s  .  co m
}

From source file:com.aliyun.datahub.flume.sink.serializer.OdpsRegexEventSerializer.java

@Override
public OdpsWriter createOdpsWriter(Table odpsTable, StreamWriter[] streamWriters, String dateFormat)
        throws UnsupportedEncodingException {
    throw new UnsupportedEncodingException("Not implemented!");
}

From source file:org.apache.wiki.util.TextUtil.java

/**
 *  URL encoder does not handle all characters correctly.
 *  See <A HREF="http://developer.java.sun.com/developer/bugParade/bugs/4257115.html">
 *  Bug parade, bug #4257115</A> for more information.
 *  <P>/*from   w  w w .  j a  v  a2s . c o  m*/
 *  Thanks to CJB for this fix.
 *
 *  @param bytes The byte array containing the bytes of the string
 *  @param encoding The encoding in which the string should be interpreted
 *  @return A decoded String
 *
 *  @throws UnsupportedEncodingException If the encoding is unknown.
 *  @throws IllegalArgumentException If the byte array is not a valid string.
 */
protected static String urlDecode(byte[] bytes, String encoding)
        throws UnsupportedEncodingException, IllegalArgumentException {
    if (bytes == null) {
        return null;
    }

    byte[] decodeBytes = new byte[bytes.length];
    int decodedByteCount = 0;

    try {
        for (int count = 0; count < bytes.length; count++) {
            switch (bytes[count]) {
            case '+':
                decodeBytes[decodedByteCount++] = (byte) ' ';
                break;

            case '%':
                decodeBytes[decodedByteCount++] = (byte) ((HEX_DIGITS.indexOf(bytes[++count]) << 4)
                        + (HEX_DIGITS.indexOf(bytes[++count])));
                break;

            default:
                decodeBytes[decodedByteCount++] = bytes[count];
            }
        }

    } catch (IndexOutOfBoundsException ae) {
        throw new IllegalArgumentException("Malformed UTF-8 string?");
    }

    String processedPageName = null;

    try {
        processedPageName = new String(decodeBytes, 0, decodedByteCount, encoding);
    } catch (UnsupportedEncodingException e) {
        throw new UnsupportedEncodingException("UTF-8 encoding not supported on this platform");
    }

    return processedPageName;
}

From source file:com.puppycrawl.tools.checkstyle.api.FileText.java

/**
 * Creates a new file text representation.
 *
 * <p>The file will be read using the specified encoding, replacing
 * malformed input and unmappable characters with the default
 * replacement character./*from   w  ww .ja v a 2 s.c om*/
 *
 * @param file the name of the file
 * @param charsetName the encoding to use when reading the file
 * @throws NullPointerException if the text is null
 * @throws IOException if the file could not be read
 */
public FileText(File file, String charsetName) throws IOException {
    this.file = file;

    // We use our own decoder, to be sure we have complete control
    // about replacements.
    final CharsetDecoder decoder;
    try {
        charset = Charset.forName(charsetName);
        decoder = charset.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    } catch (final UnsupportedCharsetException ex) {
        final String message = "Unsupported charset: " + charsetName;
        final UnsupportedEncodingException ex2 = new UnsupportedEncodingException(message);
        ex2.initCause(ex);
        throw ex2;
    }

    fullText = readFile(file, decoder);

    // Use the BufferedReader to break down the lines as this
    // is about 30% faster than using the
    // LINE_TERMINATOR.split(fullText, -1) method
    final ArrayList<String> textLines = new ArrayList<>();
    final BufferedReader reader = new BufferedReader(new StringReader(fullText));
    while (true) {
        final String line = reader.readLine();
        if (line == null) {
            break;
        }
        textLines.add(line);
    }
    lines = textLines.toArray(new String[textLines.size()]);
}

From source file:com.sc.l45.weblogviewer.reader.ReversedLinesFileReaderLineSeparator.java

/**
 * Creates a ReversedLinesFileReader with the given block size and encoding.
 *
 * @param file//from  ww w  .  jav  a2  s  . co  m
 *            the file to be read
 * @param blockSize
 *            size of the internal buffer (for ideal performance this should
 *            match with the block size of the underlying file system).
 * @param encoding
 *            the encoding of the file
 * @throws IOException  if an I/O error occurs
 * @since 2.3
 */
public ReversedLinesFileReaderLineSeparator(final File file, final int blockSize, final Charset encoding)
        throws IOException {
    this.blockSize = blockSize;
    this.encoding = encoding;

    randomAccessFile = new RandomAccessFile(file, "r");
    totalByteLength = randomAccessFile.length();
    int lastBlockLength = (int) (totalByteLength % blockSize);
    if (lastBlockLength > 0) {
        totalBlockCount = totalByteLength / blockSize + 1;
    } else {
        totalBlockCount = totalByteLength / blockSize;
        if (totalByteLength > 0) {
            lastBlockLength = blockSize;
        }
    }
    currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);

    // --- check & prepare encoding ---
    Charset charset = Charsets.toCharset(encoding);
    CharsetEncoder charsetEncoder = charset.newEncoder();
    float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
    if (maxBytesPerChar == 1f) {
        // all one byte encodings are no problem
        byteDecrement = 1;
    } else if (charset == Charset.forName("UTF-8")) {
        // UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte
        // http://en.wikipedia.org/wiki/UTF-8
        byteDecrement = 1;
    } else if (charset == Charset.forName("Shift_JIS")) {
        // Same as for UTF-8
        // http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
        byteDecrement = 1;
    } else if (charset == Charset.forName("UTF-16BE") || charset == Charset.forName("UTF-16LE")) {
        // UTF-16 new line sequences are not allowed as second tuple of four byte sequences,
        // however byte order has to be specified
        byteDecrement = 2;
    } else if (charset == Charset.forName("UTF-16")) {
        throw new UnsupportedEncodingException(
                "For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)");
    } else {
        throw new UnsupportedEncodingException(
                "Encoding " + encoding + " is not supported yet (feel free to submit a patch)");
    }
    // NOTE: The new line sequences are matched in the order given, so it is important that \r\n is BEFORE \n
    newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding),
            "\r".getBytes(encoding) };

    avoidNewlineSplitBufferSize = newLineSequences[0].length;
}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.ReversedLinesFileReader.java

/**
 * Creates a ReversedLinesFileReader with the given block size and encoding.
 *
 * @param file//w w w. j a  v a 2s  .  c  om
 *            the file to be read
 * @param blockSize
 *            size of the internal buffer (for ideal performance this should
 *            match with the block size of the underlying file system).
 * @param encoding
 *            the encoding of the file
 * @throws IOException  if an I/O error occurs
 * @since 2.3
 */
public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding)
        throws IOException {
    this.blockSize = blockSize;
    this.encoding = encoding;

    randomAccessFile = new RandomAccessFile(file, "r");
    totalByteLength = randomAccessFile.length();
    int lastBlockLength = (int) (totalByteLength % blockSize);
    if (lastBlockLength > 0) {
        totalBlockCount = totalByteLength / blockSize + 1;
    } else {
        totalBlockCount = totalByteLength / blockSize;
        if (totalByteLength > 0) {
            lastBlockLength = blockSize;
        }
    }
    currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);

    // --- check & prepare encoding ---
    Charset charset = Charsets.toCharset(encoding);
    CharsetEncoder charsetEncoder = charset.newEncoder();
    float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
    if (maxBytesPerChar == 1f) {
        // all one byte encodings are no problem
        byteDecrement = 1;
    } else if (charset == Charset.forName("UTF-8")) {
        // UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte
        // http://en.wikipedia.org/wiki/UTF-8
        byteDecrement = 1;
    } else if (charset == Charset.forName("Shift_JIS") || // Same as for UTF-8 http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
            charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
            charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
            charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
            charset == Charset.forName("x-windows-950")) { // Windows code page 950 (Traditional Chinese)
        byteDecrement = 1;
    } else if (charset == Charset.forName("UTF-16BE") || charset == Charset.forName("UTF-16LE")) {
        // UTF-16 new line sequences are not allowed as second tuple of four byte sequences,
        // however byte order has to be specified
        byteDecrement = 2;
    } else if (charset == Charset.forName("UTF-16")) {
        throw new UnsupportedEncodingException(
                "For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)");
    } else {
        throw new UnsupportedEncodingException(
                "Encoding " + encoding + " is not supported yet (feel free to submit a patch)");
    }
    // NOTE: The new line sequences are matched in the order given, so it is important that \r\n is BEFORE \n
    newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding),
            "\r".getBytes(encoding) };

    avoidNewlineSplitBufferSize = newLineSequences[0].length;
}

From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java

public static String decode(String s, String enc) throws UnsupportedEncodingException {

    boolean needToChange = false;
    int numChars = s.length();
    StringBuffer sb = new StringBuffer(numChars > 500 ? numChars / 2 : numChars);
    int i = 0;/*ww  w.j a va  2 s .com*/

    if (enc.isEmpty()) {
        throw new UnsupportedEncodingException("URLDecoder: empty string enc parameter");
    }

    char c;
    byte[] bytes = null;
    while (i < numChars) {
        c = s.charAt(i);
        SWITCH: switch (c) {
        case '+':
            sb.append(' ');
            i++;
            needToChange = true;
            break;
        case '%':
            if (bytes == null) {
                bytes = new byte[(numChars - i) / 3];
            }
            int pos = 0;
            while (((i + 2) < numChars) && (c == '%')) {
                try {
                    bytes[pos++] = (byte) Integer.parseInt(s.substring(i + 1, i + 3), 16);
                } catch (NumberFormatException e) {
                    // this wasn't a byte encoding after all
                    sb.append(s.substring(i, i += 3));
                    break SWITCH;
                }
                i += 3;
                if (i < numChars) {
                    c = s.charAt(i);
                }
            }
            sb.append(new String(bytes, 0, pos, enc));

            /*
             * A trailing, incomplete byte encoding like '%c' ... we're
             * gonna assume that this is meant to be the actual
             * characters, and won't throw an exception
             */
            if ((i < numChars) && (c == '%')) {
                sb.append(s.substring(i + 1));
            }
            needToChange = true;
            break;
        default:
            sb.append(c);
            i++;
            break;
        }
    }

    return (needToChange ? sb.toString() : s);
}

From source file:org.apache.hadoop.mapreduce.lib.input.CSVRecordReader.java

protected Reader getReader(InputStream is, Configuration conf) throws UnsupportedEncodingException {
    String encoding = conf.get(CSVFileInputFormat.FORMAT_ENCODING, CSVFileInputFormat.DEFAULT_ENCODING);
    boolean isEncodingSupported = false;
    for (String supportedEncoding : CSVTextInputFormat.SUPPORTED_ENCODINGS) {
        if (supportedEncoding.equals(encoding)) {
            isEncodingSupported = true;/* w  ww .  j a  v a  2 s  .  co  m*/
            break;
        }
    }
    if (!isEncodingSupported) {
        StringBuilder errSb = new StringBuilder();
        for (String supportedEncoding : CSVTextInputFormat.SUPPORTED_ENCODINGS) {
            if (errSb.length() != 0) {
                errSb.append(", ");
            }
            errSb.append("'");
            errSb.append(supportedEncoding);
            errSb.append("'");
        }
        throw new UnsupportedEncodingException(
                "CSVInputFormat only supports the following encodings: " + errSb.toString());
    }
    if (encoding.equals("ISO-8859-1")) {
        isAscii = true;
    }
    CharsetDecoder decoder = Charset.forName(encoding).newDecoder();
    // NOTE: we are very strict about encoded characters since if we replace or ignore, we may unwittingly mess up
    //       our split points...the reader doesn't tell us how many bytes were malformed/unmappable
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    return new BufferedReader(new InputStreamReader(is, decoder));
}