Skip to content

Commit

Permalink
[misc] checkstyle improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Jan 14, 2019
1 parent 29eb179 commit 71858d6
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 40 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/mariadb/jdbc/MariaDbClob.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,32 +243,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 UncheckedIOException("invalid UTF8", new CharacterCodingException());
}
} else if (firstByte >> 3 != -2) {
throw new UncheckedIOException("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++;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ protected void setTimerTask(boolean isBatch) {
timerTaskFuture = timeoutScheduler.schedule(() -> {
try {
isTimedout = true;
if (!isBatch) protocol.cancelCurrentQuery();
if (!isBatch) {
protocol.cancelCurrentQuery();
}
protocol.interrupt();
} catch (Throwable e) {
//eat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,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,
Expand Down Expand Up @@ -404,7 +406,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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1325,37 +1325,33 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
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;
Expand All @@ -1370,7 +1366,6 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
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)) {
Expand All @@ -1383,16 +1378,16 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
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)) {
Expand All @@ -1412,7 +1407,9 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
} else if (type.equals(ZonedDateTime.class)) {
ZonedDateTime zonedDateTime = row
.getInternalZonedDateTime(col, ZonedDateTime.class, timeZone);
if (zonedDateTime == null) return null;
if (zonedDateTime == null) {
return null;
}
return type.cast(row.getInternalZonedDateTime(col, ZonedDateTime.class, timeZone));

} else if (type.equals(OffsetDateTime.class)) {
Expand All @@ -1422,22 +1419,30 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {

} else if (type.equals(OffsetDateTime.class)) {
LocalDate localDate = row.getInternalLocalDate(col, timeZone);
if (localDate == null) return null;
if (localDate == null) {
return null;
}
return type.cast(localDate);

} else if (type.equals(LocalDate.class)) {
LocalDate localDate = row.getInternalLocalDate(col, timeZone);
if (localDate == null) return null;
if (localDate == null) {
return null;
}
return type.cast(localDate);

} else if (type.equals(LocalTime.class)) {
LocalTime localTime = row.getInternalLocalTime(col, timeZone);
if (localTime == null) return null;
if (localTime == null) {
return null;
}
return type.cast(localTime);

} else if (type.equals(OffsetTime.class)) {
OffsetTime offsetTime = row.getInternalOffsetTime(col, timeZone);
if (offsetTime == null) return null;
if (offsetTime == null) {
return null;
}
return type.cast(offsetTime);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,43 @@ public class ReadAheadBufferedStream extends FilterInputStream {
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) {
Expand All @@ -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;
}
}
}

Expand All @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public class StandardPacketInputStream implements PacketInputStream {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public class AbstractQueryProtocol extends AbstractConnectProtocol implements Pr
final ReentrantLock lock) {
super(urlParser, globalInfo, lock);
logQuery = new LogQueryTool(options);
galeraAllowedStates = urlParser.getOptions().galeraAllowedState == null ?
Collections.emptyList() :
galeraAllowedStates = urlParser.getOptions().galeraAllowedState == null
? Collections.emptyList() :
Arrays.asList(urlParser.getOptions().galeraAllowedState.split(","));
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/mariadb/jdbc/ExecuteBatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public void useBatchMultiSendWithError() throws Exception {
Thread thread2;

while (it2.hasNext()) {
thread2 = it2.next();
thread2 = it2.next();
if (thread2.getName().contains("MariaDb-bulk-")) {
assertEquals(State.WAITING, thread2.getState());
}
Expand Down
14 changes: 9 additions & 5 deletions src/test/java/org/mariadb/jdbc/MariaDbClobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/style.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
<property name="severity" value="warning"/>

<property name="fileExtensions" value="java, properties, xml"/>
<module name="SuppressWarningsFilter" />
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>

<module name="TreeWalker">
<module name="SuppressWarningsHolder" />
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
Expand Down

0 comments on commit 71858d6

Please sign in to comment.