Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-220] read Stream with maximum length correction
  • Loading branch information
rusher committed Nov 20, 2015
1 parent f76d42f commit 71e319c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 27 deletions.
Expand Up @@ -131,13 +131,16 @@ public void sendStream(InputStream is) throws IOException {
* @throws IOException if any error occur during data send to server
*/
public void sendStream(InputStream is, long readLength) throws IOException {
byte[] buffer = new byte[(int) readLength];
int len;
while ((len = is.read(buffer, 0, (int) readLength)) > 0) {
write(buffer, 0, len);
if (len >= readLength) {
byte[] buffer = new byte[8192];
long remainingReadLength = readLength;
int read;
while (remainingReadLength > 0) {
read = is.read(buffer, 0, Math.min((int)remainingReadLength, 8192));
if (read == -1) {
return;
}
write(buffer, 0, read);
remainingReadLength -= read;
}
}

Expand Down Expand Up @@ -165,14 +168,18 @@ public void sendStream(java.io.Reader reader, MariaDbCharset charset) throws IOE
*/
public void sendStream(java.io.Reader reader, long readLength, MariaDbCharset charset) throws IOException {
char[] buffer = new char[8192];
int len;
while ((len = reader.read(buffer, 0, (int) readLength)) > 0) {
byte[] bytes = new String(buffer, 0, len).getBytes(charset.javaIoCharsetName);
write(bytes, 0, bytes.length);
if (len >= readLength) {
long remainingReadLength = readLength;
int read;
while (remainingReadLength > 0) {
read = reader.read(buffer, 0, Math.min((int)remainingReadLength, 8192));
if (read == -1) {
return;
}
byte[] bytes = new String(buffer, 0, read).getBytes(charset.javaIoCharsetName);
write(bytes, 0, bytes.length);
remainingReadLength -= read;
}

}

/**
Expand Down
81 changes: 64 additions & 17 deletions src/test/java/org/mariadb/jdbc/BlobTest.java
Expand Up @@ -6,6 +6,8 @@

import java.io.*;
import java.sql.*;
import java.util.Arrays;
import java.util.Random;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -25,6 +27,7 @@ public static void initClass() throws SQLException {
createTable("BlobTestclobtest2", "strm text");
createTable("BlobTestclobtest3", "id int not null primary key, strm text");
createTable("BlobTestclobtest4", "id int not null primary key, strm text");
createTable("BlobTestclobtest5", "id int not null primary key, strm text");
createTable("BlobTestblobtest", "id int not null primary key, strm blob");
createTable("BlobTestblobtest2", "id int not null primary key, strm blob");
createTable("conj77_test", "Name VARCHAR(100) NOT NULL,Archive LONGBLOB, PRIMARY KEY (Name)", "Engine=InnoDB DEFAULT CHARSET utf8");
Expand Down Expand Up @@ -119,6 +122,67 @@ public void testCharacterStreamWithMultibyteCharacter() throws Throwable {
assertEquals(toInsert, sb.toString());
}


@Test
public void testReaderWithLength() throws SQLException, IOException {
PreparedStatement stmt = sharedConnection.prepareStatement("insert into BlobTestclobtest5 (id, strm) values (?,?)");
byte[] arr = new byte[32000];
Arrays.fill(arr, (byte) 'b');

stmt.setInt(1, 1);
String clob = new String(arr);
stmt.setCharacterStream(2, new StringReader(clob), 20000);
stmt.execute();
ResultSet rs = sharedConnection.createStatement().executeQuery("select * from BlobTestclobtest5");
rs.next();
Reader readStuff = rs.getCharacterStream("strm");

char[] chars = new char[50000];
readStuff.read(chars);

byte[] arrResult = new byte[20000];
Arrays.fill(arrResult, (byte) 'b');

for (int i = 0; i < chars.length; i++) {
if (i < 20000) {
assertEquals(arrResult[i], chars[i]);
} else {
assertEquals(chars[i], '\u0000');
}
}
}


@Test
public void testBlobWithLength() throws SQLException, IOException {
PreparedStatement stmt = sharedConnection.prepareStatement("insert into BlobTestblobtest2 (id, strm) values (?,?)");
byte[] arr = new byte[32000];
Random rand = new Random();
rand.nextBytes(arr);
InputStream stream = new ByteArrayInputStream(arr);
stmt.setInt(1, 1);
stmt.setBlob(2, stream, 20000);
stmt.execute();

//check what stream not read after length:
int remainRead = 0;
while (stream.read() >= 0) {
remainRead++;
}
assertEquals(12000, remainRead);

ResultSet rs = sharedConnection.createStatement().executeQuery("select * from BlobTestblobtest2");
rs.next();
InputStream readStuff = rs.getBlob("strm").getBinaryStream();
int pos = 0;
int ch;
while ((ch = readStuff.read()) != -1) {
assertEquals(arr[pos++] & 0xff, ch);
}
assertEquals(20000, pos);

}

@Test
public void testClobWithLengthAndMultibyteCharacter() throws SQLException, IOException {
PreparedStatement stmt = sharedConnection.prepareStatement("insert into BlobTestclobtest (id, strm) values (?,?)");
Expand Down Expand Up @@ -178,23 +242,6 @@ public void testBlob() throws SQLException, IOException {
}
}

@Test
public void testBlobWithLength() throws SQLException, IOException {
PreparedStatement stmt = sharedConnection.prepareStatement("insert into BlobTestblobtest2 (id, strm) values (?,?)");
byte[] theBlob = {1, 2, 3, 4, 5, 6};
InputStream stream = new ByteArrayInputStream(theBlob);
stmt.setInt(1, 1);
stmt.setBlob(2, stream, 4);
stmt.execute();
ResultSet rs = sharedConnection.createStatement().executeQuery("select * from BlobTestblobtest2");
rs.next();
InputStream readStuff = rs.getBlob("strm").getBinaryStream();
int ch;
int pos = 0;
while ((ch = readStuff.read()) != -1) {
assertEquals(theBlob[pos++], ch);
}
}

@Test
public void testClobWithLength() throws SQLException, IOException {
Expand Down

0 comments on commit 71e319c

Please sign in to comment.