KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Sending Blobs Larger Than 32K
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: October 21, 2014

4D's Blobs can be large containing up to 2GB of data. When working with the blob and attempting to send the data there can be bottlenecks, such as the 32K limit seen on a couple of systems like ODBC connections.

The most common way to work around this problem is to split the blob up into 32K chunks. One easy method is to use the byte array. 4D's blobs are comprised from a byte array and can be broken up and pieced back together.



The image above shows a 195K blob along with the first 17 elements of it's byte array.
They array will consist of 195K elements.

Applying the concept of byte arrays the following method will split a Blob into 32K byte array chunks:

C_LONGINT($chunk;$size;$remainder;$div;$i;$pos)
C_BLOB($blob;$1)
C_POINTER($2)

$chunk:=32*1024
$blob:=$1
$size:=BLOB size($blob)
$remainder:=Mod($size;$chunk)
$div:=($size-$remainder)/$chunk
CLEAR VARIABLE($2->)
INSERT IN ARRAY($2->;0;$div+1)

For ($i;1;$div)
   $pos:=(($i-1)*$chunk)
   COPY BLOB($blob;$2->{$i};$pos;0;$chunk)
End for

$pos:=(($i-1)*$chunk)
COPY BLOB($blob;$2->{$i};$pos;0;$remainder)


Then the method can be called as Blob_to_Chunk with some blob, $blob:
ARRAY BLOB($Arr_Blob;0)
Blob_to_Chunk ($blob;->$Arr_Blob)


With the result in $Arr_Blob each element is a 32K or smaller blob that can be sent and pieced back at the destination, such as the following method if done in 4D:
C_LONGINT($chunk;$i;$div;$pos;$size)
C_POINTER($1)
ARRAY BLOB($Arr_blob;0)
C_BLOB($blob;$2)

COPY ARRAY($1->;$Arr_blob)
$div:=Size of array($Arr_blob)

For ($i;1;$div)
   $pos:=BLOB size($blob)
   $chunk:=BLOB size($Arr_blob{$i})
   INSERT IN BLOB($blob;$size;$chunk)
End for

$size:=BLOB size($blob)
COPY BLOB($blob;$2;0;0;$size)


Calling it as Chunk_to_Blob with the array of blob pieces, $Arr_Blob, it can be pieced back together:
C_BLOB($blob2)
Chunk_to_Blob (->$Arr_Blob;$blob2)