base.Channel.readOctets

Read a given number of bytes and return it as a character string.

Syntax

readOctets(
   bytes INTEGER)
  RETURNING result STRING
  1. bytes is the number of bytes to read, not the number of characters.

Usage

After opening the channel object, call the readOctets() method to read a given number of bytes from the channel. The bytes are returned as a character string. The bytes read must match the current encoding.

The readOctets() function returns NULL if end of file is reached. To distinguish empty lines from NULL, you must use the STRING data type. If you use a CHAR or VARCHAR, you will get NULL for empty lines. To properly detect end of file, use the isEof() method.

Before reading the actual bytes in a readOctets() call, you typically get the number of bytes to read from the sender, as shown in the example.

A valid use case of the method is the HTTP protocol. Reading HTML content with readLine() is not possible: The body consists of multiple lines, and the last line might not be terminated by a line-terminator, and the stream gets not EOF:

HTTP/1.0 200 OK
Date:  Wed, 16 Apr 2014 18:50:51 GMT
Content-Type: text/html
Content-Length: 1354

<html>
<body>
<h1>My title</h1>
  :
</body>
</html>

Example

WHILE TRUE
  ...
  -- Get the number of bytes to read.
  LET len = ch.readLine()
  -- Read the bytes as character string.
  LET s = ch.readOctets(len)
  IF ch.isEof() THEN EXIT WHILE END IF
  ...
END WHILE