Search in sources :

Example 6 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project hive by apache.

the class OrcRecordUpdater method parseKeyIndex.

static RecordIdentifier[] parseKeyIndex(Reader reader) {
    String[] stripes;
    try {
        ByteBuffer val = reader.getMetadataValue(OrcRecordUpdater.ACID_KEY_INDEX_NAME).duplicate();
        stripes = utf8Decoder.decode(val).toString().split(";");
    } catch (CharacterCodingException e) {
        throw new IllegalArgumentException("Bad string encoding for " + OrcRecordUpdater.ACID_KEY_INDEX_NAME, e);
    }
    RecordIdentifier[] result = new RecordIdentifier[stripes.length];
    for (int i = 0; i < stripes.length; ++i) {
        if (stripes[i].length() != 0) {
            String[] parts = stripes[i].split(",");
            result[i] = new RecordIdentifier();
            result[i].setValues(Long.parseLong(parts[0]), Integer.parseInt(parts[1]), Long.parseLong(parts[2]));
        }
    }
    return result;
}
Also used : RecordIdentifier(org.apache.hadoop.hive.ql.io.RecordIdentifier) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 7 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project buck by facebook.

the class SegmentCommandUtils method createFromBuffer.

@SuppressWarnings("PMD.PrematureDeclaration")
public static SegmentCommand createFromBuffer(ByteBuffer buffer, NulTerminatedCharsetDecoder decoder) {
    LoadCommandCommonFields fields = LoadCommandCommonFieldsUtils.createFromBuffer(buffer);
    Preconditions.checkArgument(SegmentCommand.VALID_CMD_VALUES.contains(fields.getCmd()));
    boolean is64Bit = fields.getCmd().equals(SegmentCommand.LC_SEGMENT_64);
    String segname;
    try {
        segname = decoder.decodeString(buffer);
    } catch (CharacterCodingException e) {
        throw new HumanReadableException(e, "Cannot read segname for SegmentCommand at %d", fields.getOffsetInBinary());
    }
    buffer.position(fields.getOffsetInBinary() + LoadCommandCommonFields.CMD_AND_CMDSIZE_SIZE + SegmentCommand.SEGNAME_SIZE_IN_BYTES);
    return SegmentCommand.of(fields, segname, UnsignedLong.fromLongBits(is64Bit ? buffer.getLong() : buffer.getInt() & 0xFFFFFFFFL), UnsignedLong.fromLongBits(is64Bit ? buffer.getLong() : buffer.getInt() & 0xFFFFFFFFL), UnsignedLong.fromLongBits(is64Bit ? buffer.getLong() : buffer.getInt() & 0xFFFFFFFFL), UnsignedLong.fromLongBits(is64Bit ? buffer.getLong() : buffer.getInt() & 0xFFFFFFFFL), buffer.getInt(), buffer.getInt(), UnsignedInteger.fromIntBits(buffer.getInt()), UnsignedInteger.fromIntBits(buffer.getInt()));
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 8 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project buck by facebook.

the class UnixArchive method getIntFromStringAtRange.

private int getIntFromStringAtRange(int len, CharsetDecoder decoder) {
    String filenameLengthString;
    int offset = buffer.position();
    try {
        filenameLengthString = readStringWithLength(buffer, len, decoder);
    } catch (CharacterCodingException e) {
        throw new HumanReadableException(e, "Unable to read int from buffer (range %d..%d)", offset, offset + len);
    }
    return Integer.parseInt(filenameLengthString.trim());
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 9 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project hadoop by apache.

the class TextOutputReader method splitKeyVal.

// split a UTF-8 line into key and value
private void splitKeyVal(byte[] line, int length, Text key, Text val) throws IOException {
    // Need to find numKeyFields separators
    int pos = UTF8ByteArrayUtils.findBytes(line, 0, length, separator);
    for (int k = 1; k < numKeyFields && pos != -1; k++) {
        pos = UTF8ByteArrayUtils.findBytes(line, pos + separator.length, length, separator);
    }
    try {
        if (pos == -1) {
            key.set(line, 0, length);
            val.set("");
        } else {
            StreamKeyValUtil.splitKeyVal(line, 0, length, key, val, pos, separator.length);
        }
    } catch (CharacterCodingException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException)

Example 10 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project che by eclipse.

the class FileStoreTextFileBuffer method commitFileBufferContent.

/*
	 * @see org.eclipse.core.internal.filebuffers.FileBuffer#commitFileBufferContent(org.eclipse.core.runtime.IProgressMonitor, boolean)
	 */
protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException {
    //		if (!isSynchronized() && !overwrite)
    //			throw new CoreException(new Status(IStatus.WARNING, FileBuffersPlugin.PLUGIN_ID, IResourceStatus.OUT_OF_SYNC_LOCAL, FileBuffersMessages.FileBuffer_error_outOfSync, null));
    String encoding = computeEncoding();
    Charset charset;
    try {
        charset = Charset.forName(encoding);
    } catch (UnsupportedCharsetException ex) {
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_unsupported_encoding_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
        throw new CoreException(s);
    } catch (IllegalCharsetNameException ex) {
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_illegal_encoding_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, ex);
        throw new CoreException(s);
    }
    CharsetEncoder encoder = charset.newEncoder();
    encoder.onMalformedInput(CodingErrorAction.REPLACE);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    byte[] bytes;
    int bytesLength;
    try {
        ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(fDocument.get()));
        bytesLength = byteBuffer.limit();
        if (byteBuffer.hasArray())
            bytes = byteBuffer.array();
        else {
            bytes = new byte[bytesLength];
            byteBuffer.get(bytes);
        }
    } catch (CharacterCodingException ex) {
        Assert.isTrue(ex instanceof UnmappableCharacterException);
        String message = NLSUtility.format(FileBuffersMessages.ResourceTextFileBuffer_error_charset_mapping_failed_message_arg, encoding);
        IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CHARSET_MAPPING_FAILED, message, null);
        throw new CoreException(s);
    }
    IFileInfo fileInfo = fFileStore.fetchInfo();
    if (fileInfo != null && fileInfo.exists()) {
        if (!overwrite)
            checkSynchronizationState();
        InputStream stream = new ByteArrayInputStream(bytes, 0, bytesLength);
        /*
			 * XXX:
			 * This is a workaround for a corresponding bug in Java readers and writer,
			 * see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
			 */
        if (fHasBOM && CHARSET_UTF_8.equals(encoding))
            stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
        // here the file synchronizer should actually be removed and afterwards added again. However,
        // we are already inside an operation, so the delta is sent AFTER we have added the listener
        setFileContents(stream, monitor);
        // set synchronization stamp to know whether the file synchronizer must become active
        fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
    //			if (fAnnotationModel instanceof IPersistableAnnotationModel) {
    //				IPersistableAnnotationModel persistableModel= (IPersistableAnnotationModel) fAnnotationModel;
    //				persistableModel.commit(fDocument);
    //			}
    } else {
        fFileStore.getParent().mkdir(EFS.NONE, null);
        OutputStream out = fFileStore.openOutputStream(EFS.NONE, null);
        try {
            /*
				 * XXX:
				 * This is a workaround for a corresponding bug in Java readers and writer,
				 * see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
				 */
            if (fHasBOM && CHARSET_UTF_8.equals(encoding))
                out.write(IContentDescription.BOM_UTF_8);
            out.write(bytes, 0, bytesLength);
            out.flush();
            out.close();
        } catch (IOException x) {
            IStatus s = new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, x.getLocalizedMessage(), x);
            throw new CoreException(s);
        } finally {
            try {
                out.close();
            } catch (IOException x) {
            }
        }
        // set synchronization stamp to know whether the file synchronizer must become active
        fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)196 ByteBuffer (java.nio.ByteBuffer)114 CharBuffer (java.nio.CharBuffer)48 CharsetDecoder (java.nio.charset.CharsetDecoder)44 IOException (java.io.IOException)34 CharsetEncoder (java.nio.charset.CharsetEncoder)31 CoderResult (java.nio.charset.CoderResult)30 Charset (java.nio.charset.Charset)27 InputStream (java.io.InputStream)9 Date (java.util.Date)9 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)8 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 Path (java.nio.file.Path)6 ParseException (java.text.ParseException)6 Test (org.junit.Test)6 CoreException (org.eclipse.core.runtime.CoreException)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 OutputStream (java.io.OutputStream)4