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

Commit

Permalink
Added support for returning blob data
Browse files Browse the repository at this point in the history
Can now call getBlob to get back a byte[] array for blob data within a MySQL Column.
  • Loading branch information
boardy committed Feb 23, 2019
1 parent 6a11144 commit 3ae038a
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 90 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Binary file modified .idea/caches/gradle_models.ser
Binary file not shown.
6 changes: 0 additions & 6 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions .idea/markdown-navigator.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions AndroidMySQLConnector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ else
}

archivesBaseName="AndroidMySQLConnector"
version '0.25'
version '0.26'
group 'com.BoardiesITSolutions'


Expand All @@ -30,8 +30,8 @@ android {
//applicationId "com.BoardiesITSolutions.AndroidMySQLConnector"
minSdkVersion 19
targetSdkVersion 28
versionCode 14
versionName "0.25"
versionCode 15
versionName "0.26"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class ColumnDefinition
public enum ColumnType {
DECIMAL, TINY, SHORT, LONG, FLOAT, DOUBLE, NULL, TIMESTAMP, LONGLONG, INT24, DATE, TIME, DATETIME, YEAR,
NEWDATE, VARCHAR, BIT, TIMESTAMP2, DATETIME2, TIME2, NEWDECIMAL, ENUM, SET, TINY_BLOB, MEDIUM_BLOB,
LONG_BLOG, BLOG, VAR_STRING, STRING, GEOMETRY
LONG_BLOG, BLOB, VAR_STRING, STRING, GEOMETRY
}

/**
Expand Down Expand Up @@ -154,7 +154,7 @@ private void setColumnType(int columnType)
this.columnType = ColumnType.LONG_BLOG;
break;
case 0xfc:
this.columnType = ColumnType.BLOG;
this.columnType = ColumnType.BLOB;
break;
case 0xfd:
this.columnType = ColumnType.VAR_STRING;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.BoardiesITSolutions.AndroidMySQLConnector;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

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

mysqlIO.shiftCurrentBytePosition(4);

protocolVersion = (byte) (mysqlIO.extractData(1));
protocolVersion = (byte) (mysqlIO.extractDataAsString(1));

serverVersion = mysqlIO.extractData(false);
serverVersion = mysqlIO.extractDataAsString(false);

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

byte[] temp = (byte[]) mysqlIO.extractData(4);
byte[] temp = (byte[]) mysqlIO.extractDataAsString(4);
temp = mysqlIO.swapByteArray(temp);
connectionID = mysqlIO.fromByteArray(temp);
byte[] salt1 = (byte[]) mysqlIO.extractData(8);
byte[] salt1 = (byte[]) mysqlIO.extractDataAsString(8);
authSalt = Connection.toString(salt1, 0, salt1.length);
//There is a null terminator at the end of the salt, shift by one as we don't need it
mysqlIO.shiftCurrentBytePosition(1);


byte[] serverCapabilities = (byte[]) mysqlIO.extractData(2);
byte[] serverCapabilities = (byte[]) mysqlIO.extractDataAsString(2);
baseServerCapabilities = (serverCapabilities[0] & 0xff) | ((serverCapabilities[1] & 0xff) << 8);
Connection.this.serverCapabilities = baseServerCapabilities;

//serverLanguage = String.format("%02X", (byte) mysqlIO.extractData(1));
serverLanguage = ((byte)mysqlIO.extractData(1)) & 0xff;
//serverLanguage = String.format("%02X", (byte) mysqlIO.extractDataAsString(1));
serverLanguage = ((byte)mysqlIO.extractDataAsString(1)) & 0xff;

setCharset();
serverStatus = mysqlIO.fromByteArray((byte[]) mysqlIO.extractData(2));
serverStatus = mysqlIO.fromByteArray((byte[]) mysqlIO.extractDataAsString(2));

byte[] extendedServerCapabilitiesArray = (byte[]) mysqlIO.extractData(2);
byte[] extendedServerCapabilitiesArray = (byte[]) mysqlIO.extractDataAsString(2);

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

if ((Connection.this.serverCapabilities & CLIENT_PLUGIN_AUTH) == CLIENT_PLUGIN_AUTH)
{
authPluginDataLength = (byte) mysqlIO.extractData(1);
authPluginDataLength = (byte) mysqlIO.extractDataAsString(1);
}
else
{
Expand Down Expand Up @@ -409,7 +407,7 @@ private void processWelcomePacket() throws MySQLConnException, IOException {
length = 13;
}

byte[] salt = (byte[]) mysqlIO.extractData(length);
byte[] salt = (byte[]) mysqlIO.extractDataAsString(length);
authSalt2 = Connection.toString(salt, 0, salt.length);

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

if ((Connection.this.serverCapabilities & CLIENT_PLUGIN_AUTH) == CLIENT_PLUGIN_AUTH) {
authPluginName = mysqlIO.extractData(false);
authPluginName = mysqlIO.extractDataAsString(false);
}

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

private void parseVersionNumber()
{
this.serverVersion = this.serverVersion.substring(0, this.serverVersion.indexOf("-")-1);
if (this.serverVersion.indexOf("-") >= 0)
{
this.serverVersion = this.serverVersion.substring(0, this.serverVersion.indexOf("-") - 1);
}
this.serverVersion = this.serverVersion.replaceAll("[^\\d.]", "");
if ((this.serverVersion != null) && this.serverVersion.length() > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class MySQLIO
private byte[] fullData;
private int currentBytesRead = 0;

public static boolean breakSocketGetData = true;

public MySQLIO(Connection connection, Socket mysqlSock) throws IOException
{
this.connection = connection;
Expand Down Expand Up @@ -69,29 +71,49 @@ public byte[] getSocketByteArray()

private void getSocketData() throws IOException
{

Log.d("MySQLIO", "Reading Socket Data");
byte[] data = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int payloadLength = -1;
Log.d("MySQLIO", "Created baos");
byte[] data = new byte[1024];
int bytesRead = -1;
while ((bytesRead = sockStream.read(data)) != -1) {
Log.d("MySQLIO", "Read from socket. Bytes Read: " + bytesRead);
baos.write(data, 0, bytesRead);
Log.d("MySQLIO", "BAOS written: Bytes Written: " + bytesRead);
if (bytesRead < 1024) {
Log.d("MySQLIO", "Breaking from loop");
break;
int totalBytesRead = 0;
try {
int bytesRead = -1;
while ((bytesRead = sockStream.read(data)) != -1) {
totalBytesRead += bytesRead;
Log.d("MySQLIO", "Read from socket. Bytes Read: " + bytesRead);
baos.write(data, 0, bytesRead);
Log.d("MySQLIO", "BAOS written: Bytes Written: " + bytesRead);
if (bytesRead < 1024) {
Log.d("MySQLIO", "Receiving less data than buffer filled. Checking for EOF");
byte[] temp = baos.toByteArray();
if ((int)temp[temp.length-5] == 0xfe || (int)temp[temp.length-1] == 0)
{
Log.d("MySQLIO", "EOF detected, breaking loop and processing response");
break;
}
}
}
Log.d("MySQLIO", "Loop completed");
if (baos.size() == 0) {
Log.d("MySQLIO", "No data in response");
}
fullData = baos.toByteArray();
Log.d("MySQLIO", "Full data written to: now size: " + fullData.length);
currentBytesRead = 0;
}
Log.d("MySQLIO", "Loop compleeted");
if (baos.size() == 0)
catch (Exception ex)
{
Log.d("MySQLIO", "No data in response");
fullData = baos.toByteArray();
currentBytesRead = 0;
}
fullData = baos.toByteArray();
Log.d("MySQLIO", "Full data written to: now size: " + fullData.length);
currentBytesRead = 0;
}

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

/**
Expand All @@ -104,6 +126,7 @@ public void reset(boolean dontExpectResponse) throws IOException
this.currentBytesRead = 0;
if (!dontExpectResponse)
{
Log.d("MySQLIO", "socket data has reset. Getting socket data");
this.getSocketData();
}
}
Expand All @@ -118,7 +141,14 @@ public byte readCurrentByteWithoutShift()
return this.fullData[currentBytesRead];
}

public String extractData(boolean returnAsHex, int length)
public byte[] extractData(int length)
{
byte[] temp = Arrays.copyOfRange(fullData, currentBytesRead, currentBytesRead+length);
this.shiftCurrentBytePosition(length);
return temp;
}

public String extractDataAsString(boolean returnAsHex, int length)
{
byte[] temp = Arrays.copyOfRange(fullData, currentBytesRead, currentBytesRead+length);
if (returnAsHex)
Expand Down Expand Up @@ -146,7 +176,7 @@ public String extractData(boolean returnAsHex, int length)
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public String extractData(boolean returnAsHex)
public String extractDataAsString(boolean returnAsHex)
{
if (returnAsHex)
{
Expand Down Expand Up @@ -184,7 +214,7 @@ public String extractData(boolean returnAsHex)
}
}

public Object extractData(int length) throws IndexOutOfBoundsException
public Object extractDataAsString(int length) throws IndexOutOfBoundsException
{
if (length == 1)
{
Expand All @@ -195,6 +225,13 @@ public Object extractData(int length) throws IndexOutOfBoundsException
else
{
byte[] value = Arrays.copyOfRange(fullData, currentBytesRead, currentBytesRead+length);
/*for (int i = 0; i < value.length; i++)
{
if (value[i] == -1)
{
value[i] = (byte)0xff;
}
}*/
this.shiftCurrentBytePosition(length);
return value;
}
Expand Down Expand Up @@ -236,6 +273,7 @@ else if (bytes[0] < 0xfd)
byte[] temp = Arrays.copyOfRange(bytes, 0, 3);
return fromByteArray(temp);
}

return -1;
}

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

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

public void sendDataOnSocket(byte[] data, IIntConnectionInterface iIntConnectionInterface) throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

public class MySQLRow
{
private HashMap<ColumnDefinition, String> columnAndRowValue;
private HashMap<ColumnDefinition, Object> columnAndRowValue;

public MySQLRow()
{
this.columnAndRowValue = new HashMap<>();
}

public void addRowValue(ColumnDefinition columnDefinition, String rowValue)
public void addRowValue(ColumnDefinition columnDefinition, Object rowValue)
{
this.columnAndRowValue.put(columnDefinition, rowValue);
}
Expand Down Expand Up @@ -54,5 +54,19 @@ public double getDouble(String column) throws SQLColumnNotFoundException
return Double.parseDouble(value);
}


public byte[] getBlob(String column) throws SQLColumnNotFoundException
{
Set set = this.columnAndRowValue.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext())
{
Map.Entry entry = (Map.Entry)iterator.next();
ColumnDefinition col = (ColumnDefinition)entry.getKey();
if (col.getColumnName().equals(column))
{
return (byte[])entry.getValue();
}
}
throw new SQLColumnNotFoundException("'"+column+"' was not found in result set");
}
}
Loading

0 comments on commit 3ae038a

Please sign in to comment.