Skip to content
Permalink
Browse files

[misc] checkstyle improvement

(cherry picked from commit 71858d6)
  • Loading branch information...
rusher committed Jan 14, 2019
1 parent 9f2517a commit 195a59acbce1dd23d684deb2573276acf2132f75
@@ -242,32 +242,32 @@ public long length() {
int pos = offset;

//set ASCII (<= 127 chars)
for(; len < length && data[pos] >= 0; ) {
for (; len < length && data[pos] >= 0; ) {
len++;
pos++;
}

//multi-bytes UTF-8
while(pos < offset + length) {
while (pos < offset + length) {
byte firstByte = data[pos++];
if (firstByte < 0) {
if (firstByte >> 5 != -2 || (firstByte & 30) == 0) {
if (firstByte >> 4 == -2) {
if (pos + 1 < offset + length) {
pos+=2;
pos += 2;
len++;
} else {
throw new RuntimeException("invalid UTF8", new CharacterCodingException());
}
} else if (firstByte >> 3 != -2) {
throw new RuntimeException("invalid UTF8", new CharacterCodingException());
} else if (pos + 2 < offset + length) {
pos+=3;
len+=2;
pos += 3;
len += 2;
} else {
//bad truncated UTF8
pos += offset + length;
len+=1;
len += 1;
}
} else {
pos++;
@@ -168,7 +168,9 @@ protected void setTimerTask(final boolean isBatch) {
public void run() {
try {
isTimedout = true;
if (!isBatch) protocol.cancelCurrentQuery();
if (!isBatch) {
protocol.cancelCurrentQuery();
}
protocol.interrupt();
} catch (Throwable e) {
//eat
@@ -270,7 +270,9 @@ private void executeBatchInternal(int queryParameterSize) throws SQLException {
executing = true;
try {
executeQueryPrologue(serverPrepareResult);
if (queryTimeout != 0) setTimerTask(true);
if (queryTimeout != 0) {
setTimerTask(true);
}

results = new Results(this,
0,
@@ -403,7 +405,9 @@ protected boolean executeInternal(int fetchSize) throws SQLException {
lock.lock();
try {
executeQueryPrologue(serverPrepareResult);
if (queryTimeout != 0) setTimerTask(false);
if (queryTimeout != 0) {
setTimerTask(false);
}

ParameterHolder[] parameterHolders = currentParameterHolder.values()
.toArray(new ParameterHolder[0]);
@@ -68,6 +68,7 @@
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
@@ -265,7 +266,7 @@ public static ResultSet createGeneratedData(long[] data, Protocol protocol,
ColumnInformation[] columns = new ColumnInformation[1];
columns[0] = ColumnInformation.create("insert_id", ColumnType.BIGINT);

List<byte[]> rows = new ArrayList<byte[]>();
List<byte[]> rows = new ArrayList<>();
for (long rowData : data) {
if (rowData != 0) {
rows.add(StandardPacketInputStream.create(String.valueOf(rowData).getBytes()));
@@ -990,7 +991,7 @@ public InputStream getAsciiStream(int columnIndex) throws SQLException {
return null;
}
return new ByteArrayInputStream(
new String(row.buf, row.pos, row.getLengthMaxFieldSize(), Buffer.UTF_8)
new String(row.buf, row.pos, row.getLengthMaxFieldSize(), StandardCharsets.UTF_8)
.getBytes());
}

@@ -1258,7 +1259,7 @@ public InputStream getUnicodeStream(int columnIndex) throws SQLException {
return null;
}
return new ByteArrayInputStream(
new String(row.buf, row.pos, row.getLengthMaxFieldSize(), Buffer.UTF_8)
new String(row.buf, row.pos, row.getLengthMaxFieldSize(), StandardCharsets.UTF_8)
.getBytes());
}

@@ -1317,37 +1318,33 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
throw new SQLException("Class type cannot be null");
}
checkObjectRange(columnIndex);
if (row.lastValueWasNull()) {
return null;
}
ColumnInformation col = columnsInformation[columnIndex - 1];

if (type.equals(String.class)) {
return (T) row.getInternalString(col, null, timeZone);

} else if (type.equals(Integer.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Integer) row.getInternalInt(col);

} else if (type.equals(Long.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Long) row.getInternalLong(col);

} else if (type.equals(Short.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Short) row.getInternalShort(col);

} else if (type.equals(Double.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Double) row.getInternalDouble(col);

} else if (type.equals(Float.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Float) row.getInternalFloat(col);

} else if (type.equals(Byte.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Byte) row.getInternalByte(col);

} else if (type.equals(byte[].class)) {
if (row.lastValueWasNull()) return null;
byte[] data = new byte[row.getLengthMaxFieldSize()];
System.arraycopy(row.buf, row.pos, data, 0, row.getLengthMaxFieldSize());
return (T) data;
@@ -1362,7 +1359,6 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
return (T) row.getInternalTimestamp(col, null, timeZone);

} else if (type.equals(Boolean.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Boolean) row.getInternalBoolean(col);

} else if (type.equals(Calendar.class)) {
@@ -1375,16 +1371,16 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
return type.cast(calendar);

} else if (type.equals(Clob.class) || type.equals(NClob.class)) {
if (row.lastValueWasNull()) return null;
return (T) new MariaDbClob(row.buf, row.pos, row.getLengthMaxFieldSize());

} else if (type.equals(InputStream.class)) {
if (row.lastValueWasNull()) return null;
return (T) new ByteArrayInputStream(row.buf, row.pos, row.getLengthMaxFieldSize());

} else if (type.equals(Reader.class)) {
String value = row.getInternalString(col, null, timeZone);
if (value == null) return null;
if (value == null) {
return null;
}
return (T) new StringReader(value);

} else if (type.equals(BigDecimal.class)) {
@@ -679,7 +679,8 @@ public Timestamp getInternalTimestamp(ColumnInformation columnInfo, Calendar use

default:
String value = new String(buf, pos, length, StandardCharsets.UTF_8);
throw new SQLException("Value type \"" + columnInfo.getColumnType().getTypeName() + "\" with value \"" + value + "\" cannot be parse as Timestamp");
throw new SQLException("Value type \"" + columnInfo.getColumnType().getTypeName()
+ "\" with value \"" + value + "\" cannot be parse as Timestamp");
}
}

@@ -1006,6 +1006,11 @@ public boolean isMasterConnected() {
return masterProtocol != null && masterProtocol.isConnected();
}

/**
* Indicate if connection has an active transaction.
*
* @return boolean
*/
public boolean inTransaction() {
if (masterProtocol != null) {
return masterProtocol.inTransaction();
@@ -45,25 +45,43 @@
private volatile byte[] buf;
private int end;
private int pos;
private int BUF_SIZE = 16384;
private static final int BUF_SIZE = 16384;

public ReadAheadBufferedStream(InputStream in) {
super(in);
buf = new byte[BUF_SIZE];
}

/**
* Reading one byte from cache of socket if needed.
*
* @return byte value
* @throws IOException if socket reading error.
*/
public synchronized int read() throws IOException {
if (pos >= end) {
fillBuffer(1);
if (pos >= end)
if (pos >= end) {
return -1;
}
}
return buf[pos++] & 0xff;
}

/**
* Returing byte array, from cache of reading socket if needed.
*
* @param externalBuf buffer to fill
* @param off offset
* @param len length to read
* @return number of added bytes
* @throws IOException if exception during socket reading
*/
public synchronized int read(byte[] externalBuf, int off, int len) throws IOException {

if (len == 0) return 0;
if (len == 0) {
return 0;
}

int totalReads = 0;
while (true) {
@@ -74,14 +92,18 @@ public synchronized int read(byte[] externalBuf, int off, int len) throws IOExce
//buffer length is less than asked byte and buffer is empty
// => filling directly into external buffer
int reads = super.read(externalBuf, off + totalReads, len - totalReads);
if (reads <= 0) return (totalReads == 0) ? -1 : totalReads;
if (reads <= 0) {
return (totalReads == 0) ? -1 : totalReads;
}
return totalReads + reads;

} else {

//filling internal buffer
fillBuffer(len - totalReads);
if (end <= 0) return (totalReads == 0) ? -1 : totalReads;
if (end <= 0) {
return (totalReads == 0) ? -1 : totalReads;
}
}
}

@@ -91,7 +113,9 @@ public synchronized int read(byte[] externalBuf, int off, int len) throws IOExce
pos += copyLength;
totalReads += copyLength;

if (totalReads >= len || super.available() <= 0) return totalReads;
if (totalReads >= len || super.available() <= 0) {
return totalReads;
}
}
}

@@ -84,6 +84,12 @@

private LruTraceCache traceCache = null;

/**
* Constructor of standard socket MySQL packet stream reader.
*
* @param in stream
* @param options connection options
*/
public StandardPacketInputStream(InputStream in, Options options) {
inputStream = options.useReadAheadInput ? new ReadAheadBufferedStream(in)
: new BufferedInputStream(in, 16384);
@@ -152,8 +152,8 @@
final ReentrantLock lock) {
super(urlParser, globalInfo, lock);
logQuery = new LogQueryTool(options);
galeraAllowedStates = urlParser.getOptions().galeraAllowedState == null ?
Collections.<String>emptyList() :
galeraAllowedStates = urlParser.getOptions().galeraAllowedState == null
? Collections.<String>emptyList() :
Arrays.asList(urlParser.getOptions().galeraAllowedState.split(","));
}

@@ -446,7 +446,7 @@ public void run() {
Thread thread2;

while (it2.hasNext()) {
thread2 = it2.next();
thread2 = it2.next();
if (thread2.getName().contains("MariaDb-bulk-")) {
assertEquals(State.WAITING, thread2.getState());
}
@@ -266,22 +266,26 @@ public void free() {
}

@Test
@SuppressWarnings("checkstyle:AvoidEscapedUnicodeCharacters")
public void clobLength() throws Exception {
Statement stmt = sharedConnection.createStatement();
try (ResultSet rs = stmt
.executeQuery("SELECT 'ab$c', 'ab¢c', 'abहc', 'ab\uD801\uDC37c', 'ab𐍈c' from dual")) {
while (rs.next()) {

Clob clob1 = rs.getClob(1);
Clob clob2 = rs.getClob(2);
Clob clob3 = rs.getClob(3);
Clob clob4 = rs.getClob(4);
Clob clob5 = rs.getClob(5);

assertEquals(4, clob1.length());

Clob clob2 = rs.getClob(2);
assertEquals(4, clob2.length());

Clob clob3 = rs.getClob(3);
assertEquals(4, clob3.length());

Clob clob4 = rs.getClob(4);
assertEquals(5, clob4.length());

Clob clob5 = rs.getClob(5);
assertEquals(5, clob5.length());

clob1.truncate(3);

0 comments on commit 195a59a

Please sign in to comment.
You can’t perform that action at this time.