Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 3ae038a

Browse files
committed
Added support for returning blob data
Can now call getBlob to get back a byte[] array for blob data within a MySQL Column.
1 parent 6a11144 commit 3ae038a

File tree

16 files changed

+165
-90
lines changed

16 files changed

+165
-90
lines changed
0 Bytes
Binary file not shown.

.idea/caches/gradle_models.ser

-18 Bytes
Binary file not shown.

.idea/gradle.xml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown-navigator.xml

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AndroidMySQLConnector/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ else
2020
}
2121

2222
archivesBaseName="AndroidMySQLConnector"
23-
version '0.25'
23+
version '0.26'
2424
group 'com.BoardiesITSolutions'
2525

2626

@@ -30,8 +30,8 @@ android {
3030
//applicationId "com.BoardiesITSolutions.AndroidMySQLConnector"
3131
minSdkVersion 19
3232
targetSdkVersion 28
33-
versionCode 14
34-
versionName "0.25"
33+
versionCode 15
34+
versionName "0.26"
3535
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3636
}
3737
buildTypes {

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/ColumnDefinition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class ColumnDefinition
55
public enum ColumnType {
66
DECIMAL, TINY, SHORT, LONG, FLOAT, DOUBLE, NULL, TIMESTAMP, LONGLONG, INT24, DATE, TIME, DATETIME, YEAR,
77
NEWDATE, VARCHAR, BIT, TIMESTAMP2, DATETIME2, TIME2, NEWDECIMAL, ENUM, SET, TINY_BLOB, MEDIUM_BLOB,
8-
LONG_BLOG, BLOG, VAR_STRING, STRING, GEOMETRY
8+
LONG_BLOG, BLOB, VAR_STRING, STRING, GEOMETRY
99
}
1010

1111
/**
@@ -154,7 +154,7 @@ private void setColumnType(int columnType)
154154
this.columnType = ColumnType.LONG_BLOG;
155155
break;
156156
case 0xfc:
157-
this.columnType = ColumnType.BLOG;
157+
this.columnType = ColumnType.BLOB;
158158
break;
159159
case 0xfd:
160160
this.columnType = ColumnType.VAR_STRING;

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/Connection.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.BoardiesITSolutions.AndroidMySQLConnector;
22

33
import android.annotation.TargetApi;
4-
import android.content.Context;
54
import android.os.Build;
65
import android.support.annotation.RequiresApi;
7-
import android.support.v4.app.Fragment;
86
import android.support.v7.app.AppCompatActivity;
97
import android.util.Log;
108

@@ -312,34 +310,34 @@ private void processWelcomePacket() throws MySQLConnException, IOException {
312310

313311
mysqlIO.shiftCurrentBytePosition(4);
314312

315-
protocolVersion = (byte) (mysqlIO.extractData(1));
313+
protocolVersion = (byte) (mysqlIO.extractDataAsString(1));
316314

317-
serverVersion = mysqlIO.extractData(false);
315+
serverVersion = mysqlIO.extractDataAsString(false);
318316

319317
//Pass the server version in to the major, minor and subminor versions - these could
320318
//be used if things need to be done differently based on the server we're connecting to
321319
parseVersionNumber();
322320

323-
byte[] temp = (byte[]) mysqlIO.extractData(4);
321+
byte[] temp = (byte[]) mysqlIO.extractDataAsString(4);
324322
temp = mysqlIO.swapByteArray(temp);
325323
connectionID = mysqlIO.fromByteArray(temp);
326-
byte[] salt1 = (byte[]) mysqlIO.extractData(8);
324+
byte[] salt1 = (byte[]) mysqlIO.extractDataAsString(8);
327325
authSalt = Connection.toString(salt1, 0, salt1.length);
328326
//There is a null terminator at the end of the salt, shift by one as we don't need it
329327
mysqlIO.shiftCurrentBytePosition(1);
330328

331329

332-
byte[] serverCapabilities = (byte[]) mysqlIO.extractData(2);
330+
byte[] serverCapabilities = (byte[]) mysqlIO.extractDataAsString(2);
333331
baseServerCapabilities = (serverCapabilities[0] & 0xff) | ((serverCapabilities[1] & 0xff) << 8);
334332
Connection.this.serverCapabilities = baseServerCapabilities;
335333

336-
//serverLanguage = String.format("%02X", (byte) mysqlIO.extractData(1));
337-
serverLanguage = ((byte)mysqlIO.extractData(1)) & 0xff;
334+
//serverLanguage = String.format("%02X", (byte) mysqlIO.extractDataAsString(1));
335+
serverLanguage = ((byte)mysqlIO.extractDataAsString(1)) & 0xff;
338336

339337
setCharset();
340-
serverStatus = mysqlIO.fromByteArray((byte[]) mysqlIO.extractData(2));
338+
serverStatus = mysqlIO.fromByteArray((byte[]) mysqlIO.extractDataAsString(2));
341339

342-
byte[] extendedServerCapabilitiesArray = (byte[]) mysqlIO.extractData(2);
340+
byte[] extendedServerCapabilitiesArray = (byte[]) mysqlIO.extractDataAsString(2);
343341

344342
int extendedServerCapabilities = (extendedServerCapabilitiesArray[0] & 0xff) | ((extendedServerCapabilitiesArray[1] & 0xff) << 8);
345343
Connection.this.extendedServerCapabilities = extendedServerCapabilities;
@@ -352,7 +350,7 @@ private void processWelcomePacket() throws MySQLConnException, IOException {
352350

353351
if ((Connection.this.serverCapabilities & CLIENT_PLUGIN_AUTH) == CLIENT_PLUGIN_AUTH)
354352
{
355-
authPluginDataLength = (byte) mysqlIO.extractData(1);
353+
authPluginDataLength = (byte) mysqlIO.extractDataAsString(1);
356354
}
357355
else
358356
{
@@ -409,7 +407,7 @@ private void processWelcomePacket() throws MySQLConnException, IOException {
409407
length = 13;
410408
}
411409

412-
byte[] salt = (byte[]) mysqlIO.extractData(length);
410+
byte[] salt = (byte[]) mysqlIO.extractDataAsString(length);
413411
authSalt2 = Connection.toString(salt, 0, salt.length);
414412

415413
StringBuilder stringBuilder = new StringBuilder(length);
@@ -419,7 +417,7 @@ private void processWelcomePacket() throws MySQLConnException, IOException {
419417
}
420418

421419
if ((Connection.this.serverCapabilities & CLIENT_PLUGIN_AUTH) == CLIENT_PLUGIN_AUTH) {
422-
authPluginName = mysqlIO.extractData(false);
420+
authPluginName = mysqlIO.extractDataAsString(false);
423421
}
424422

425423
//Check if we're using TLS connection, if so we need to send an SSL Request Packet
@@ -773,7 +771,10 @@ public Statement createStatement()
773771

774772
private void parseVersionNumber()
775773
{
776-
this.serverVersion = this.serverVersion.substring(0, this.serverVersion.indexOf("-")-1);
774+
if (this.serverVersion.indexOf("-") >= 0)
775+
{
776+
this.serverVersion = this.serverVersion.substring(0, this.serverVersion.indexOf("-") - 1);
777+
}
777778
this.serverVersion = this.serverVersion.replaceAll("[^\\d.]", "");
778779
if ((this.serverVersion != null) && this.serverVersion.length() > 0)
779780
{

AndroidMySQLConnector/src/main/java/com/BoardiesITSolutions/AndroidMySQLConnector/MySQLIO.java

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class MySQLIO
2626
private byte[] fullData;
2727
private int currentBytesRead = 0;
2828

29+
public static boolean breakSocketGetData = true;
30+
2931
public MySQLIO(Connection connection, Socket mysqlSock) throws IOException
3032
{
3133
this.connection = connection;
@@ -69,29 +71,49 @@ public byte[] getSocketByteArray()
6971

7072
private void getSocketData() throws IOException
7173
{
74+
7275
Log.d("MySQLIO", "Reading Socket Data");
76+
byte[] data = new byte[1024];
7377
ByteArrayOutputStream baos = new ByteArrayOutputStream();
78+
int payloadLength = -1;
7479
Log.d("MySQLIO", "Created baos");
75-
byte[] data = new byte[1024];
76-
int bytesRead = -1;
77-
while ((bytesRead = sockStream.read(data)) != -1) {
78-
Log.d("MySQLIO", "Read from socket. Bytes Read: " + bytesRead);
79-
baos.write(data, 0, bytesRead);
80-
Log.d("MySQLIO", "BAOS written: Bytes Written: " + bytesRead);
81-
if (bytesRead < 1024) {
82-
Log.d("MySQLIO", "Breaking from loop");
83-
break;
80+
int totalBytesRead = 0;
81+
try {
82+
int bytesRead = -1;
83+
while ((bytesRead = sockStream.read(data)) != -1) {
84+
totalBytesRead += bytesRead;
85+
Log.d("MySQLIO", "Read from socket. Bytes Read: " + bytesRead);
86+
baos.write(data, 0, bytesRead);
87+
Log.d("MySQLIO", "BAOS written: Bytes Written: " + bytesRead);
88+
if (bytesRead < 1024) {
89+
Log.d("MySQLIO", "Receiving less data than buffer filled. Checking for EOF");
90+
byte[] temp = baos.toByteArray();
91+
if ((int)temp[temp.length-5] == 0xfe || (int)temp[temp.length-1] == 0)
92+
{
93+
Log.d("MySQLIO", "EOF detected, breaking loop and processing response");
94+
break;
95+
}
96+
}
97+
}
98+
Log.d("MySQLIO", "Loop completed");
99+
if (baos.size() == 0) {
100+
Log.d("MySQLIO", "No data in response");
84101
}
102+
fullData = baos.toByteArray();
103+
Log.d("MySQLIO", "Full data written to: now size: " + fullData.length);
104+
currentBytesRead = 0;
85105
}
86-
Log.d("MySQLIO", "Loop compleeted");
87-
if (baos.size() == 0)
106+
catch (Exception ex)
88107
{
89-
Log.d("MySQLIO", "No data in response");
108+
fullData = baos.toByteArray();
109+
currentBytesRead = 0;
90110
}
91-
fullData = baos.toByteArray();
92-
Log.d("MySQLIO", "Full data written to: now size: " + fullData.length);
93-
currentBytesRead = 0;
111+
}
94112

113+
private int extractPayloadLengthFromBuffer(byte[] buffer)
114+
{
115+
byte[] value = Arrays.copyOfRange(buffer, 0, 3);
116+
return fromByteArray(value)+4;
95117
}
96118

97119
/**
@@ -104,6 +126,7 @@ public void reset(boolean dontExpectResponse) throws IOException
104126
this.currentBytesRead = 0;
105127
if (!dontExpectResponse)
106128
{
129+
Log.d("MySQLIO", "socket data has reset. Getting socket data");
107130
this.getSocketData();
108131
}
109132
}
@@ -118,7 +141,14 @@ public byte readCurrentByteWithoutShift()
118141
return this.fullData[currentBytesRead];
119142
}
120143

121-
public String extractData(boolean returnAsHex, int length)
144+
public byte[] extractData(int length)
145+
{
146+
byte[] temp = Arrays.copyOfRange(fullData, currentBytesRead, currentBytesRead+length);
147+
this.shiftCurrentBytePosition(length);
148+
return temp;
149+
}
150+
151+
public String extractDataAsString(boolean returnAsHex, int length)
122152
{
123153
byte[] temp = Arrays.copyOfRange(fullData, currentBytesRead, currentBytesRead+length);
124154
if (returnAsHex)
@@ -146,7 +176,7 @@ public String extractData(boolean returnAsHex, int length)
146176
* @return
147177
*/
148178
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
149-
public String extractData(boolean returnAsHex)
179+
public String extractDataAsString(boolean returnAsHex)
150180
{
151181
if (returnAsHex)
152182
{
@@ -184,7 +214,7 @@ public String extractData(boolean returnAsHex)
184214
}
185215
}
186216

187-
public Object extractData(int length) throws IndexOutOfBoundsException
217+
public Object extractDataAsString(int length) throws IndexOutOfBoundsException
188218
{
189219
if (length == 1)
190220
{
@@ -195,6 +225,13 @@ public Object extractData(int length) throws IndexOutOfBoundsException
195225
else
196226
{
197227
byte[] value = Arrays.copyOfRange(fullData, currentBytesRead, currentBytesRead+length);
228+
/*for (int i = 0; i < value.length; i++)
229+
{
230+
if (value[i] == -1)
231+
{
232+
value[i] = (byte)0xff;
233+
}
234+
}*/
198235
this.shiftCurrentBytePosition(length);
199236
return value;
200237
}
@@ -236,6 +273,7 @@ else if (bytes[0] < 0xfd)
236273
byte[] temp = Arrays.copyOfRange(bytes, 0, 3);
237274
return fromByteArray(temp);
238275
}
276+
239277
return -1;
240278
}
241279

@@ -321,9 +359,12 @@ public void sendDataOnSocket(byte[] data, boolean dontExpectResponse, IIntConnec
321359
sockOut.write(data);
322360
sockOut.flush();
323361

362+
Log.d("MySQLIO", "Socketdata written and about to reset");
324363
//Check the response
325364
this.reset(dontExpectResponse);
365+
Log.d("MySQLIO", "Socket data reset method has been called. Going to call socket data sent interface");
326366
iIntConnectionInterface.socketDataSent();
367+
Log.d("MySQLIO", "Calling socket data sent interface");
327368
}
328369

329370
public void sendDataOnSocket(byte[] data, IIntConnectionInterface iIntConnectionInterface) throws IOException

0 commit comments

Comments
 (0)