Logo Search packages:      
Sourcecode: mysql-connector-java version File versions

String com::mysql::jdbc::Field::getStringFromBytes ( int  stringStart,
int  stringLength 
) [inline, private]

Create a string with the correct charset encoding from the byte-buffer that contains the data for this field

Definition at line 608 of file Field.java.

References com::mysql::jdbc::Connection::getCharacterSetMetadata(), com::mysql::jdbc::Connection::getCharsetConverter(), com::mysql::jdbc::ConnectionProperties::getEncoding(), com::mysql::jdbc::ConnectionProperties::getUseUnicode(), and com::mysql::jdbc::SingleByteCharsetConverter::toString().

Referenced by getDatabaseName(), getName(), getOriginalName(), getOriginalTableName(), and getTableName().

                                                                         {
        if ((stringStart == -1) || (stringLength == -1)) {
            return null;
        }

        String stringVal = null;

        if (this.connection != null) {
            if (this.connection.getUseUnicode()) {
                  String encoding = this.connection.getCharacterSetMetadata();
                
                if (encoding == null) {
                  encoding = connection.getEncoding();
                }

                if (encoding != null) {
                    SingleByteCharsetConverter converter = null;

                    if (this.connection != null) {
                        converter = this.connection.getCharsetConverter(encoding);
                    }

                    if (converter != null) { // we have a converter
                        stringVal = converter.toString(this.buffer, stringStart,
                                stringLength);
                    } else {
                        // we have no converter, use JVM converter 
                        byte[] stringBytes = new byte[stringLength];

                        int endIndex = stringStart + stringLength;
                        int pos = 0;

                        for (int i = stringStart; i < endIndex; i++) {
                            stringBytes[pos++] = this.buffer[i];
                        }

                        try {
                            stringVal = new String(stringBytes, encoding);
                        } catch (UnsupportedEncodingException ue) {
                            throw new RuntimeException(
                                Messages.getString("Field.12") + encoding //$NON-NLS-1$
                                + Messages.getString("Field.13")); //$NON-NLS-1$
                        }
                    }
                } else {
                    // we have no encoding, use JVM standard charset
                    stringVal = StringUtils.toAsciiString(this.buffer, stringStart,
                            stringLength);
                }
            } else {
                // we are not using unicode, so use JVM standard charset 
                stringVal = StringUtils.toAsciiString(this.buffer, stringStart,
                        stringLength);
            }
        } else {
            // we don't have a connection, so punt 
            stringVal = StringUtils.toAsciiString(this.buffer, stringStart,
                    stringLength);
        }

        return stringVal;
    }


Generated by  Doxygen 1.6.0   Back to index