Java Code Examples for com.mysql.jdbc.StringUtils#getBytes()

The following examples show how to use com.mysql.jdbc.StringUtils#getBytes() . 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: MysqlOldPasswordPlugin.java    From r-course with MIT License 6 votes vote down vote up
public boolean nextAuthenticationStep(Buffer fromServer, List<Buffer> toServer) throws SQLException {
    toServer.clear();

    Buffer bresp = null;

    String pwd = this.password;

    if (fromServer == null || pwd == null || pwd.length() == 0) {
        bresp = new Buffer(new byte[0]);
    } else {
        bresp = new Buffer(
                StringUtils.getBytes(Util.newCrypt(pwd, fromServer.readString().substring(0, 8), this.connection.getPasswordCharacterEncoding())));

        bresp.setPosition(bresp.getBufLength());
        int oldBufLength = bresp.getBufLength();

        bresp.writeByte((byte) 0);

        bresp.setBufLength(oldBufLength + 1);
        bresp.setPosition(0);
    }
    toServer.add(bresp);

    return true;
}
 
Example 2
Source File: MysqlClearPasswordPlugin.java    From r-course with MIT License 6 votes vote down vote up
public boolean nextAuthenticationStep(Buffer fromServer, List<Buffer> toServer) throws SQLException {
    toServer.clear();

    Buffer bresp;
    try {
        String encoding = this.connection.versionMeetsMinimum(5, 7, 6) ? this.connection.getPasswordCharacterEncoding() : "UTF-8";
        bresp = new Buffer(StringUtils.getBytes(this.password != null ? this.password : "", encoding));
    } catch (UnsupportedEncodingException e) {
        throw SQLError.createSQLException(Messages.getString("MysqlClearPasswordPlugin.1", new Object[] { this.connection.getPasswordCharacterEncoding() }),
                SQLError.SQL_STATE_GENERAL_ERROR, null);
    }

    bresp.setPosition(bresp.getBufLength());
    int oldBufLength = bresp.getBufLength();

    bresp.writeByte((byte) 0);

    bresp.setBufLength(oldBufLength + 1);
    bresp.setPosition(0);

    toServer.add(bresp);
    return true;
}
 
Example 3
Source File: MysqlOldPasswordPlugin.java    From Komondor with GNU General Public License v3.0 6 votes vote down vote up
public boolean nextAuthenticationStep(Buffer fromServer, List<Buffer> toServer) throws SQLException {
    toServer.clear();

    Buffer bresp = null;

    String pwd = this.password;

    if (fromServer == null || pwd == null || pwd.length() == 0) {
        bresp = new Buffer(new byte[0]);
    } else {
        bresp = new Buffer(
                StringUtils.getBytes(Util.newCrypt(pwd, fromServer.readString().substring(0, 8), this.connection.getPasswordCharacterEncoding())));

        bresp.setPosition(bresp.getBufLength());
        int oldBufLength = bresp.getBufLength();

        bresp.writeByte((byte) 0);

        bresp.setBufLength(oldBufLength + 1);
        bresp.setPosition(0);
    }
    toServer.add(bresp);

    return true;
}
 
Example 4
Source File: MysqlClearPasswordPlugin.java    From Komondor with GNU General Public License v3.0 6 votes vote down vote up
public boolean nextAuthenticationStep(Buffer fromServer, List<Buffer> toServer) throws SQLException {
    toServer.clear();

    Buffer bresp;
    try {
        String encoding = this.connection.versionMeetsMinimum(5, 7, 6) ? this.connection.getPasswordCharacterEncoding() : "UTF-8";
        bresp = new Buffer(StringUtils.getBytes(this.password != null ? this.password : "", encoding));
    } catch (UnsupportedEncodingException e) {
        throw SQLError.createSQLException(Messages.getString("MysqlClearPasswordPlugin.1", new Object[] { this.connection.getPasswordCharacterEncoding() }),
                SQLError.SQL_STATE_GENERAL_ERROR, null);
    }

    bresp.setPosition(bresp.getBufLength());
    int oldBufLength = bresp.getBufLength();

    bresp.writeByte((byte) 0);

    bresp.setBufLength(oldBufLength + 1);
    bresp.setPosition(0);

    toServer.add(bresp);
    return true;
}
 
Example 5
Source File: StringRegressionTest.java    From r-course with MIT License 4 votes vote down vote up
/**
 * Tests that the 0x5c escaping works (we didn't use to have this).
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testSjis5c() throws Exception {
    byte[] origByteStream = new byte[] { (byte) 0x95, (byte) 0x5c, (byte) 0x8e, (byte) 0x96 };

    //
    // Print the hex values of the string
    //
    StringBuilder bytesOut = new StringBuilder();

    for (int i = 0; i < origByteStream.length; i++) {
        bytesOut.append(Integer.toHexString(origByteStream[i] & 255));
        bytesOut.append(" ");
    }

    System.out.println(bytesOut.toString());

    String origString = new String(origByteStream, "SJIS");
    byte[] newByteStream = StringUtils.getBytes(origString, "SJIS", "ISO8859_1              ", false, null, null);

    //
    // Print the hex values of the string (should have an extra 0x5c)
    //
    bytesOut = new StringBuilder();

    for (int i = 0; i < newByteStream.length; i++) {
        bytesOut.append(Integer.toHexString(newByteStream[i] & 255));
        bytesOut.append(" ");
    }

    System.out.println(bytesOut.toString());

    //
    // Now, insert and retrieve the value from the database
    //
    Connection sjisConn = null;
    Statement sjisStmt = null;

    try {
        Properties props = new Properties();
        props.put("useUnicode", "true");
        props.put("characterEncoding", "SJIS");
        sjisConn = getConnectionWithProps(props);

        sjisStmt = sjisConn.createStatement();

        this.rs = sjisStmt.executeQuery("SHOW VARIABLES LIKE 'character_set%'");

        while (this.rs.next()) {
            System.out.println(this.rs.getString(1) + " = " + this.rs.getString(2));
        }

        sjisStmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");

        if (versionMeetsMinimum(4, 1)) {
            sjisStmt.executeUpdate("CREATE TABLE sjisTest (field1 char(50)) DEFAULT CHARACTER SET SJIS");
        } else {
            sjisStmt.executeUpdate("CREATE TABLE sjisTest (field1 char(50))");
        }

        this.pstmt = sjisConn.prepareStatement("INSERT INTO sjisTest VALUES (?)");
        this.pstmt.setString(1, origString);
        this.pstmt.executeUpdate();

        this.rs = sjisStmt.executeQuery("SELECT * FROM sjisTest");

        while (this.rs.next()) {
            byte[] testValueAsBytes = this.rs.getBytes(1);

            bytesOut = new StringBuilder();

            for (int i = 0; i < testValueAsBytes.length; i++) {
                bytesOut.append(Integer.toHexString(testValueAsBytes[i] & 255));
                bytesOut.append(" ");
            }

            System.out.println("Value retrieved from database: " + bytesOut.toString());

            String testValue = this.rs.getString(1);

            assertTrue(testValue.equals(origString));
        }
    } finally {
        this.stmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");
    }
}
 
Example 6
Source File: ProfilerEvent.java    From r-course with MIT License 4 votes vote down vote up
/**
 * Creates a binary representation of this event.
 * 
 * @return a binary representation of this event
 * @throws Exception
 *             if an error occurs while packing this event.
 */
public byte[] pack() throws Exception {

    int len = 1 + 4 + 4 + 4 + 8 + 4 + 4;

    byte[] eventCreationAsBytes = null;

    getEventCreationPointAsString();

    if (this.eventCreationPointDesc != null) {
        eventCreationAsBytes = StringUtils.getBytes(this.eventCreationPointDesc, "ISO8859_1");
        len += (4 + eventCreationAsBytes.length);
    } else {
        len += 4;
    }

    byte[] messageAsBytes = null;

    if (this.message != null) {
        messageAsBytes = StringUtils.getBytes(this.message, "ISO8859_1");
        len += (4 + messageAsBytes.length);
    } else {
        len += 4;
    }

    byte[] durationUnitsAsBytes = null;

    if (this.durationUnits != null) {
        durationUnitsAsBytes = StringUtils.getBytes(this.durationUnits, "ISO8859_1");
        len += (4 + durationUnitsAsBytes.length);
    } else {
        len += 4;
        durationUnitsAsBytes = StringUtils.getBytes("", "ISO8859_1");
    }

    byte[] buf = new byte[len];

    int pos = 0;

    buf[pos++] = this.eventType;
    pos = writeLong(this.connectionId, buf, pos);
    pos = writeInt(this.statementId, buf, pos);
    pos = writeInt(this.resultSetId, buf, pos);
    pos = writeLong(this.eventCreationTime, buf, pos);
    pos = writeLong(this.eventDuration, buf, pos);
    pos = writeBytes(durationUnitsAsBytes, buf, pos);
    pos = writeInt(this.eventCreationPointIndex, buf, pos);

    if (eventCreationAsBytes != null) {
        pos = writeBytes(eventCreationAsBytes, buf, pos);
    } else {
        pos = writeInt(0, buf, pos);
    }

    if (messageAsBytes != null) {
        pos = writeBytes(messageAsBytes, buf, pos);
    } else {
        pos = writeInt(0, buf, pos);
    }

    return buf;
}
 
Example 7
Source File: StringRegressionTest.java    From Komondor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests that the 0x5c escaping works (we didn't use to have this).
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testSjis5c() throws Exception {
    byte[] origByteStream = new byte[] { (byte) 0x95, (byte) 0x5c, (byte) 0x8e, (byte) 0x96 };

    //
    // Print the hex values of the string
    //
    StringBuilder bytesOut = new StringBuilder();

    for (int i = 0; i < origByteStream.length; i++) {
        bytesOut.append(Integer.toHexString(origByteStream[i] & 255));
        bytesOut.append(" ");
    }

    System.out.println(bytesOut.toString());

    String origString = new String(origByteStream, "SJIS");
    byte[] newByteStream = StringUtils.getBytes(origString, "SJIS", "ISO8859_1              ", false, null, null);

    //
    // Print the hex values of the string (should have an extra 0x5c)
    //
    bytesOut = new StringBuilder();

    for (int i = 0; i < newByteStream.length; i++) {
        bytesOut.append(Integer.toHexString(newByteStream[i] & 255));
        bytesOut.append(" ");
    }

    System.out.println(bytesOut.toString());

    //
    // Now, insert and retrieve the value from the database
    //
    Connection sjisConn = null;
    Statement sjisStmt = null;

    try {
        Properties props = new Properties();
        props.put("useUnicode", "true");
        props.put("characterEncoding", "SJIS");
        sjisConn = getConnectionWithProps(props);

        sjisStmt = sjisConn.createStatement();

        this.rs = sjisStmt.executeQuery("SHOW VARIABLES LIKE 'character_set%'");

        while (this.rs.next()) {
            System.out.println(this.rs.getString(1) + " = " + this.rs.getString(2));
        }

        sjisStmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");

        if (versionMeetsMinimum(4, 1)) {
            sjisStmt.executeUpdate("CREATE TABLE sjisTest (field1 char(50)) DEFAULT CHARACTER SET SJIS");
        } else {
            sjisStmt.executeUpdate("CREATE TABLE sjisTest (field1 char(50))");
        }

        this.pstmt = sjisConn.prepareStatement("INSERT INTO sjisTest VALUES (?)");
        this.pstmt.setString(1, origString);
        this.pstmt.executeUpdate();

        this.rs = sjisStmt.executeQuery("SELECT * FROM sjisTest");

        while (this.rs.next()) {
            byte[] testValueAsBytes = this.rs.getBytes(1);

            bytesOut = new StringBuilder();

            for (int i = 0; i < testValueAsBytes.length; i++) {
                bytesOut.append(Integer.toHexString(testValueAsBytes[i] & 255));
                bytesOut.append(" ");
            }

            System.out.println("Value retrieved from database: " + bytesOut.toString());

            String testValue = this.rs.getString(1);

            assertTrue(testValue.equals(origString));
        }
    } finally {
        this.stmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");
    }
}
 
Example 8
Source File: ProfilerEvent.java    From Komondor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a binary representation of this event.
 * 
 * @return a binary representation of this event
 * @throws Exception
 *             if an error occurs while packing this event.
 */
public byte[] pack() throws Exception {

    int len = 1 + 4 + 4 + 4 + 8 + 4 + 4;

    byte[] eventCreationAsBytes = null;

    getEventCreationPointAsString();

    if (this.eventCreationPointDesc != null) {
        eventCreationAsBytes = StringUtils.getBytes(this.eventCreationPointDesc, "ISO8859_1");
        len += (4 + eventCreationAsBytes.length);
    } else {
        len += 4;
    }

    byte[] messageAsBytes = null;

    if (this.message != null) {
        messageAsBytes = StringUtils.getBytes(this.message, "ISO8859_1");
        len += (4 + messageAsBytes.length);
    } else {
        len += 4;
    }

    byte[] durationUnitsAsBytes = null;

    if (this.durationUnits != null) {
        durationUnitsAsBytes = StringUtils.getBytes(this.durationUnits, "ISO8859_1");
        len += (4 + durationUnitsAsBytes.length);
    } else {
        len += 4;
        durationUnitsAsBytes = StringUtils.getBytes("", "ISO8859_1");
    }

    byte[] buf = new byte[len];

    int pos = 0;

    buf[pos++] = this.eventType;
    pos = writeLong(this.connectionId, buf, pos);
    pos = writeInt(this.statementId, buf, pos);
    pos = writeInt(this.resultSetId, buf, pos);
    pos = writeLong(this.eventCreationTime, buf, pos);
    pos = writeLong(this.eventDuration, buf, pos);
    pos = writeBytes(durationUnitsAsBytes, buf, pos);
    pos = writeInt(this.eventCreationPointIndex, buf, pos);

    if (eventCreationAsBytes != null) {
        pos = writeBytes(eventCreationAsBytes, buf, pos);
    } else {
        pos = writeInt(0, buf, pos);
    }

    if (messageAsBytes != null) {
        pos = writeBytes(messageAsBytes, buf, pos);
    } else {
        pos = writeInt(0, buf, pos);
    }

    return buf;
}