KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Large BLOB to array of text using the new command APPEND TO ARRAY
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: November 18, 2004
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

Description: Conversion of a BLOB to an array of text in 2004 using APPEND TO ARRAY

4th Dimension 2004 can now append data to an array in a single command APPEND TO ARRAY(array;value).

The APPEND TO ARRAY command adds a new element at the end of array and assigns value of the array type to the element. In interpreted mode, if array does not exist, the command creates it with regard to the type of value. This command works with all types of arrays: string, number, boolean, date, pointer and picture.

APPEND TO ARRAY makes converting the entire contents of a BLOB (larger than 32kB) to text easy.
The following code is a complete method to pass the contents of a blob of any size to a dynamically resized array of text:


  `````````````````````````````````````
  `Method name: BLOB_to_Text_Array
  `Description: Fills an array of text blocks with data from a BLOB
  `Parameter: $0 is an output of the number of elements in the text array for reference
  `Parameter: $1 is a pointer to a(n) (inter)process variable of type BLOB (to be copied)
  `Parameter: $2 is a pointer to a(n) (inter)process variable of type Text Array (to be filled)
  `````````````````````````````````````
C_POINTER($1)
C_POINTER($2)
C_LONGINT($blobSize;$limitSize)
C_LONGINT($offset;$0)

$limitSize:=20000  ` size of each text block in the array up to 32k
$offset:=0
$blobSize:=BLOB size($1->)

Repeat
  APPEND TO ARRAY($2->;BLOB to text($1->;Text without length ;$offset;$limitSize))
Until ($blobSize<=$offset)

  ` return the number of elements added
$0:=Size of array($2->)

To see an example of the method in action, create the above method, and then create the following method and run it. Then check the directory of the 4D database on which you are working.

  `````````````````````````````````````
  `Method name: test_BLOB_to_Text_Array
  `Description: creates a blob and fills it with data so that it will be larger than 32kB,
` calls BLOB_to_Text_Array to copy the BLOB into text blocks
  `````````````````````````````````````

C_BLOB(blob)
C_LONGINT($numTextArrayElements)
ARRAY TEXT(text_array;0)

  ` your BLOB can contain any kind of data, this command fills the blob with
  ` 1000KB of zeroes to help illustrate what is created in the text array
SET BLOB SIZE(blob;1000*1024;0x0030)

$numTextArrayElements:=Blob_to_Text_Array (->blob;->text_array)

ALERT("The array now contains "+String($numTextArrayElements)+" elements.")

  ` prevent memory leak of blob
SET BLOB SIZE(blob;0)

` Don't forget to CLEAR VARIABLE your text array after you've used it to prevent memory leaks!