Java Code Examples for com.mysql.cj.exceptions.DataReadException

The following examples show how to use com.mysql.cj.exceptions.DataReadException. These examples are extracted from open source projects. 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 Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <T> T decodeSet(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        StringBuilder vals = new StringBuilder();
        while (inputStream.getBytesUntilLimit() > 0) {
            if (vals.length() > 0) {
                vals.append(",");
            }
            long valLen = inputStream.readUInt64();
            // TODO: charset
            vals.append(new String(inputStream.readRawBytes((int) valLen)));
        }
        // TODO: charset mess here
        byte[] buf = vals.toString().getBytes();
        return vf.createFromBytes(buf, 0, buf.length);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 2
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 6 votes vote down vote up
public <T> T decodeUInt8(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_INT8) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "LONG" }));
    }

    // don't use BigInteger unless sign bit is used
    if ((bytes[offset + 7] & 0x80) == 0) {
        return this.decodeInt8(bytes, offset, length, vf);
    }

    // first byte is 0 to indicate sign
    byte[] bigEndian = new byte[] { 0, bytes[offset + 7], bytes[offset + 6], bytes[offset + 5], bytes[offset + 4], bytes[offset + 3], bytes[offset + 2],
            bytes[offset + 1], bytes[offset] };
    BigInteger bigInt = new BigInteger(bigEndian);
    return vf.createFromBigInteger(bigInt);
}
 
Example 3
Source Project: FoxTelem   Source File: XProtocolDecoder.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public <T> T decodeSet(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        StringBuilder vals = new StringBuilder();
        while (inputStream.getBytesUntilLimit() > 0) {
            if (vals.length() > 0) {
                vals.append(",");
            }
            long valLen = inputStream.readUInt64();
            // TODO: charset
            vals.append(new String(inputStream.readRawBytes((int) valLen)));
        }
        // TODO: charset mess here
        byte[] buf = vals.toString().getBytes();
        return vf.createFromBytes(buf, 0, buf.length);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 4
public <T> T decodeUInt8(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_INT8) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "LONG" }));
    }

    // don't use BigInteger unless sign bit is used
    if ((bytes[offset + 7] & 0x80) == 0) {
        return this.decodeInt8(bytes, offset, length, vf);
    }

    // first byte is 0 to indicate sign
    byte[] bigEndian = new byte[] { 0, bytes[offset + 7], bytes[offset + 6], bytes[offset + 5], bytes[offset + 4], bytes[offset + 3], bytes[offset + 2],
            bytes[offset + 1], bytes[offset] };
    BigInteger bigInt = new BigInteger(bigEndian);
    return vf.createFromBigInteger(bigInt);
}
 
Example 5
Source Project: FoxTelem   Source File: SqlDateValueFactory.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public Date createFromDate(int year, int month, int day) {
    synchronized (this.cal) {
        try {
            if (year == 0 && month == 0 && day == 0) {
                throw new DataReadException(Messages.getString("ResultSet.InvalidZeroDate"));
            }

            this.cal.clear();
            this.cal.set(year, month - 1, day);
            long ms = this.cal.getTimeInMillis();
            return new Date(ms);
        } catch (IllegalArgumentException e) {
            throw ExceptionFactory.createException(WrongArgumentException.class, e.getMessage(), e);
        }
    }
}
 
Example 6
@Override
public Timestamp createFromTimestamp(int year, int month, int day, int hours, int minutes, int seconds, int nanos) {
    if (year == 0 && month == 0 && day == 0) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidZeroDate"));
    }

    synchronized (this.cal) {
        try {
            // this method is HUGEly faster than Java 8's Calendar.Builder()
            this.cal.set(year, month - 1, day, hours, minutes, seconds);
            Timestamp ts = new Timestamp(this.cal.getTimeInMillis());
            ts.setNanos(nanos);
            return ts;
        } catch (IllegalArgumentException e) {
            throw ExceptionFactory.createException(WrongArgumentException.class, e.getMessage(), e);
        }
    }
}
 
Example 7
Source Project: FoxTelem   Source File: SqlTimeValueFactory.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public Time createFromTime(int hours, int minutes, int seconds, int nanos) {
    if (hours < 0 || hours >= 24) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidTimeValue", new Object[] { "" + hours + ":" + minutes + ":" + seconds }));
    }

    synchronized (this.cal) {
        try {
            // c.f. java.sql.Time "The date components should be set to the "zero epoch" value of January 1, 1970 and should not be accessed."
            this.cal.set(1970, 0, 1, hours, minutes, seconds);
            this.cal.set(Calendar.MILLISECOND, 0);
            long ms = (nanos / 1000000) + this.cal.getTimeInMillis();
            return new Time(ms);
        } catch (IllegalArgumentException e) {
            throw ExceptionFactory.createException(WrongArgumentException.class, e.getMessage(), e);
        }
    }
}
 
Example 8
Source Project: quarkus   Source File: MySQLJDBCReflections.java    License: Apache License 2.0 5 votes vote down vote up
@BuildStep
void registerExceptionsForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJCommunicationsException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, CJConnectionFeatureNotAvailableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJTimeoutException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJPacketTooBigException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, AssertionFailedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ClosedOnExpiredPasswordException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ConnectionIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataConversionException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataReadException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataTruncationException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DeadlockTimeoutRollbackMarker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, FeatureNotAvailableException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, InvalidConnectionAttributeException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NumberOutOfRange.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, OperationCancelledException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PasswordExpiredException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PropertyNotModifiableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, RSAException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, SSLParamsException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StatementIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StreamingNotifiable.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, UnableToConnectException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, UnsupportedConnectionStringException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, WrongArgumentException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, "com.mysql.cj.jdbc.MysqlXAException"));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, StandardLoadBalanceExceptionChecker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NdbLoadBalanceExceptionChecker.class.getName()));
}
 
Example 9
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeTime(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        boolean negative = inputStream.readRawByte() > 0;
        int hours = 0;
        int minutes = 0;
        int seconds = 0;

        int nanos = 0;

        if (!inputStream.isAtEnd()) {
            hours = (int) inputStream.readInt64();
            if (!inputStream.isAtEnd()) {
                minutes = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    seconds = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        nanos = 1000 * (int) inputStream.readInt64();
                    }
                }
            }
        }

        return vf.createFromTime(negative ? -1 * hours : hours, minutes, seconds, nanos);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 10
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        int year = (int) inputStream.readUInt64();
        int month = (int) inputStream.readUInt64();
        int day = (int) inputStream.readUInt64();

        // do we have a time too?
        if (inputStream.getBytesUntilLimit() > 0) {
            int hours = 0;
            int minutes = 0;
            int seconds = 0;

            int nanos = 0;

            if (!inputStream.isAtEnd()) {
                hours = (int) inputStream.readInt64();
                if (!inputStream.isAtEnd()) {
                    minutes = (int) inputStream.readInt64();
                    if (!inputStream.isAtEnd()) {
                        seconds = (int) inputStream.readInt64();
                        if (!inputStream.isAtEnd()) {
                            nanos = 1000 * (int) inputStream.readInt64();
                        }
                    }
                }
            }

            return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
        }
        return vf.createFromDate(year, month, day);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 11
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeInt8(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        return vf.createFromLong(CodedInputStream.newInstance(bytes, offset, length).readSInt64());
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 12
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeUInt8(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        // protobuf stores an unsigned 64bit int into a java long with the highest bit as the sign, we re-interpret it using ByteBuffer (with a prepended
        // 0-byte to avoid negative)
        BigInteger v = new BigInteger(
                ByteBuffer.allocate(9).put((byte) 0).putLong(CodedInputStream.newInstance(bytes, offset, length).readUInt64()).array());
        return vf.createFromBigInteger(v);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 13
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeFloat(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        return vf.createFromDouble(CodedInputStream.newInstance(bytes, offset, length).readFloat());
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 14
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeDouble(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        return vf.createFromDouble(CodedInputStream.newInstance(bytes, offset, length).readDouble());
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 15
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeByteArray(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // c.f. Streaming_command_delegate::get_string()
        int size = inputStream.getBytesUntilLimit();
        size--; // for null terminator
        return vf.createFromBytes(inputStream.readRawBytes(size), 0, size);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 16
Source Project: lams   Source File: XProtocolDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T decodeBit(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        // protobuf stores an unsigned 64bit int into a java long with the highest bit as the sign, we re-interpret it using ByteBuffer (with a prepended
        // 0-byte to avoid negative)
        byte[] buf = ByteBuffer.allocate(Long.BYTES + 1).put((byte) 0).putLong(CodedInputStream.newInstance(bytes, offset, length).readUInt64()).array();
        return vf.createFromBit(buf, 0, Long.BYTES + 1);
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example 17
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length == 0) {
        return vf.createFromTimestamp(0, 0, 0, 0, 0, 0, 0);
    } else if (length != NativeConstants.BIN_LEN_DATE && length != NativeConstants.BIN_LEN_TIMESTAMP && length != NativeConstants.BIN_LEN_TIMESTAMP_NO_US) {
        // the value can be any of these lengths (check protocol docs)
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIMESTAMP" }));
    }

    int year = 0;
    int month = 0;
    int day = 0;

    int hours = 0;
    int minutes = 0;
    int seconds = 0;

    int nanos = 0;

    year = (bytes[offset + 0] & 0xff) | ((bytes[offset + 1] & 0xff) << 8);
    month = bytes[offset + 2];
    day = bytes[offset + 3];

    if (length > NativeConstants.BIN_LEN_DATE) {
        hours = bytes[offset + 4];
        minutes = bytes[offset + 5];
        seconds = bytes[offset + 6];
    }

    if (length > NativeConstants.BIN_LEN_TIMESTAMP_NO_US) {
        // MySQL PS protocol uses microseconds
        nanos = 1000 * ((bytes[offset + 7] & 0xff) | ((bytes[offset + 8] & 0xff) << 8) | ((bytes[offset + 9] & 0xff) << 16)
                | ((bytes[offset + 10] & 0xff) << 24));
    }

    return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
}
 
Example 18
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeTime(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length == 0) {
        return vf.createFromTime(0, 0, 0, 0);
    } else if (length != NativeConstants.BIN_LEN_TIME && length != NativeConstants.BIN_LEN_TIME_NO_US) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIME" }));
    }

    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int nanos = 0;

    boolean negative = bytes[offset] == 1;

    days = (bytes[offset + 1] & 0xff) | ((bytes[offset + 2] & 0xff) << 8) | ((bytes[offset + 3] & 0xff) << 16) | ((bytes[offset + 4] & 0xff) << 24);
    hours = bytes[offset + 5];
    minutes = bytes[offset + 6];
    seconds = bytes[offset + 7];

    if (negative) {
        days *= -1;
    }

    if (length > NativeConstants.BIN_LEN_TIMESTAMP_NO_US) {
        // MySQL PS protocol uses microseconds
        nanos = 1000 * (bytes[offset + 1] & 0xff) | ((bytes[offset + 2] & 0xff) << 8) | ((bytes[offset + 3] & 0xff) << 16)
                | ((bytes[offset + 4] & 0xff) << 24);
    }

    return vf.createFromTime(days * 24 + hours, minutes, seconds, nanos);
}
 
Example 19
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeDate(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length == 0) {
        return vf.createFromDate(0, 0, 0);
    } else if (length != NativeConstants.BIN_LEN_DATE) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "DATE" }));
    }
    int year = (bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8);
    int month = bytes[offset + 2];
    int day = bytes[offset + 3];
    return vf.createFromDate(year, month, day);
}
 
Example 20
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeUInt2(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_INT2) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "SHORT" }));
    }
    int asInt = ((bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8));
    return vf.createFromLong(asInt);
}
 
Example 21
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeInt2(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_INT2) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "SHORT" }));
    }
    short asShort = (short) ((bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8));
    return vf.createFromLong(asShort);
}
 
Example 22
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeUInt4(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_INT4) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "INT" }));
    }
    long asLong = (bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8) | ((bytes[offset + 2] & 0xff) << 16)
            | ((long) (bytes[offset + 3] & 0xff) << 24);
    return vf.createFromLong(asLong);
}
 
Example 23
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeInt4(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_INT4) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "SHORT" }));
    }
    int asInt = (bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8) | ((bytes[offset + 2] & 0xff) << 16) | ((bytes[offset + 3] & 0xff) << 24);
    return vf.createFromLong(asInt);
}
 
Example 24
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeInt8(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_INT8) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "LONG" }));
    }
    long asLong = (bytes[offset] & 0xff) | ((long) (bytes[offset + 1] & 0xff) << 8) | ((long) (bytes[offset + 2] & 0xff) << 16)
            | ((long) (bytes[offset + 3] & 0xff) << 24) | ((long) (bytes[offset + 4] & 0xff) << 32) | ((long) (bytes[offset + 5] & 0xff) << 40)
            | ((long) (bytes[offset + 6] & 0xff) << 48) | ((long) (bytes[offset + 7] & 0xff) << 56);
    return vf.createFromLong(asLong);
}
 
Example 25
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeFloat(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_FLOAT) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "FLOAT" }));
    }
    int asInt = (bytes[offset] & 0xff) | ((bytes[offset + 1] & 0xff) << 8) | ((bytes[offset + 2] & 0xff) << 16) | ((bytes[offset + 3] & 0xff) << 24);
    return vf.createFromDouble(Float.intBitsToFloat(asInt));
}
 
Example 26
Source Project: lams   Source File: MysqlBinaryValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeDouble(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != NativeConstants.BIN_LEN_DOUBLE) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "DOUBLE" }));
    }
    long valueAsLong = (bytes[offset + 0] & 0xff) | ((long) (bytes[offset + 1] & 0xff) << 8) | ((long) (bytes[offset + 2] & 0xff) << 16)
            | ((long) (bytes[offset + 3] & 0xff) << 24) | ((long) (bytes[offset + 4] & 0xff) << 32) | ((long) (bytes[offset + 5] & 0xff) << 40)
            | ((long) (bytes[offset + 6] & 0xff) << 48) | ((long) (bytes[offset + 7] & 0xff) << 56);
    return vf.createFromDouble(Double.longBitsToDouble(valueAsLong));
}
 
Example 27
Source Project: lams   Source File: MysqlTextValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeDate(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != DATE_BUF_LEN) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "DATE" }));
    }
    int year = StringUtils.getInt(bytes, offset, offset + 4);
    int month = StringUtils.getInt(bytes, offset + 5, offset + 7);
    int day = StringUtils.getInt(bytes, offset + 8, offset + 10);
    return vf.createFromDate(year, month, day);
}
 
Example 28
Source Project: lams   Source File: MysqlTextValueDecoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length < TIMESTAMP_NOFRAC_STR_LEN || (length > TIMESTAMP_STR_LEN_MAX && length != TIMESTAMP_STR_LEN_WITH_NANOS)) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIMESTAMP" }));
    } else if (length != TIMESTAMP_NOFRAC_STR_LEN) {
        // need at least two extra bytes for fractional, '.' and a digit
        if (bytes[offset + TIMESTAMP_NOFRAC_STR_LEN] != (byte) '.' || length < TIMESTAMP_NOFRAC_STR_LEN + 2) {
            throw new DataReadException(
                    Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
        }
    }

    // delimiter verification
    if (bytes[offset + 4] != (byte) '-' || bytes[offset + 7] != (byte) '-' || bytes[offset + 10] != (byte) ' ' || bytes[offset + 13] != (byte) ':'
            || bytes[offset + 16] != (byte) ':') {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
    }

    int year = StringUtils.getInt(bytes, offset, offset + 4);
    int month = StringUtils.getInt(bytes, offset + 5, offset + 7);
    int day = StringUtils.getInt(bytes, offset + 8, offset + 10);
    int hours = StringUtils.getInt(bytes, offset + 11, offset + 13);
    int minutes = StringUtils.getInt(bytes, offset + 14, offset + 16);
    int seconds = StringUtils.getInt(bytes, offset + 17, offset + 19);
    // nanos from MySQL fractional
    int nanos;
    if (length == TIMESTAMP_STR_LEN_WITH_NANOS) {
        nanos = StringUtils.getInt(bytes, offset + 20, offset + length);
    } else {
        nanos = (length == TIMESTAMP_NOFRAC_STR_LEN) ? 0 : StringUtils.getInt(bytes, offset + 20, offset + length);
        // scale out nanos appropriately. mysql supports up to 6 digits of fractional seconds, each additional digit increasing the range by a factor of
        // 10. one digit is tenths, two is hundreths, etc
        nanos = nanos * (int) Math.pow(10, 9 - (length - TIMESTAMP_NOFRAC_STR_LEN - 1));
    }

    return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
}
 
Example 29
Source Project: lams   Source File: SqlDateValueFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Date createFromDate(int year, int month, int day) {
    synchronized (this.cal) {
        if (year == 0 && month == 0 && day == 0) {
            throw new DataReadException(Messages.getString("ResultSet.InvalidZeroDate"));
        }

        this.cal.clear();
        this.cal.set(year, month - 1, day);
        long ms = this.cal.getTimeInMillis();
        return new Date(ms);
    }
}
 
Example 30
/**
 * Create a Timestamp from a TIME value.
 *
 * @return a timestamp at the given time on 1970 Jan 1.
 */
@Override
public Timestamp createFromTime(int hours, int minutes, int seconds, int nanos) {
    if (hours < 0 || hours >= 24) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidTimeValue", new Object[] { "" + hours + ":" + minutes + ":" + seconds }));
    }

    return createFromTimestamp(1970, 1, 1, hours, minutes, seconds, nanos);
}