({{, (RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src] 
gives the contents of src as a (RefLink[ByteArray, paclet:ref/ByteArray]) object.}, {, (RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src,n]
reads the first n bytes from src.}, {, (RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src,term]
reads until the termination condition term is satisfied.}})

OpenersNotesSection(Spacer1)Details and Options

In (RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src,…], src can be a file given as "filename" or (RefLink[File, paclet:ref/File])["filename"], an input-oriented stream such as (RefLink[InputStream, paclet:ref/InputStream]) or an external process with head (RefLink[ProcessObject, paclet:ref/ProcessObject]).

(RefLink[ReadByteArray, paclet:ref/ReadByteArray])["file",…] and (RefLink[ReadByteArray, paclet:ref/ReadByteArray])[(RefLink[File, paclet:ref/File])["file"],…] will open "file" for reading if it is not already open. In this case, the file will be closed after reading if the default terminator, (RefLink[EndOfFile, paclet:ref/EndOfFile]), is used.

The termination condition term can be any of the following:

If the termination condition consists of a byte sequence, that sequence is simply dropped and is not returned as part of the byte array.

(RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src] or (RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src,(RefLink[EndOfFile, paclet:ref/EndOfFile])] reads from the current position until a stream has ended or the external process providing input has terminated.

If (RefLink[ReadByteArray, paclet:ref/ReadByteArray]) is called on a source of content that has already been exhausted, it returns (RefLink[EndOfFile, paclet:ref/EndOfFile]).

(RefLink[ReadByteArray, paclet:ref/ReadByteArray]) is a blocking function that by default will not return until it has read whatever it is specified to read.

With the option setting (RefLink[TimeConstraint, paclet:ref/TimeConstraint])->t, (RefLink[ReadByteArray, paclet:ref/ReadByteArray]) waits at most t seconds, then returns whatever it has read by that time.

(RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src,(RefLink[EndOfBuffer, paclet:ref/EndOfBuffer])] does not block, and returns whatever is already available in the input buffer.

OpenersPrimaryExamplesSection(Spacer1)Examples  (13)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Read in an image file as a byte array:

ba=ReadByteArray["ExampleData/rose.gif"]

The byte array contains the contents of the file for further manipulation:

ImportByteArray[ba]==Import["ExampleData/rose.gif"]

Export text to a file:

file=CreateFile[];
Export[file,"aαℵ","Text"];

Read in the file as a byte array:

ba=ReadByteArray[file]

View the individual bytes:

Normal[ba]

Convert the bytes back to a string:

ByteArrayToString[ba]

OpenersExampleSection1(Spacer1)Scope  (5)

Read in a WXF file:

ba=ReadByteArray["ExampleData/atoms.wxf"]

Read in the file, explicitly specifying the (RefLink[EndOfFile, paclet:ref/EndOfFile]) terminator:

%==ReadByteArray["ExampleData/atoms.wxf",EndOfFile]

Convert the data to expressions:

BinaryDeserialize[ba]

Read in the first line of text of Alice in Wonderland as an array of bytes:

ba=ReadByteArray["ExampleData/Text/AliceInWonderland.txt", EndOfLine]

ByteArrayToString[ba]

Read the next line of text:

ReadByteArray["ExampleData/Text/AliceInWonderland.txt", EndOfLine]

Close the file:

Close["ExampleData/Text/AliceInWonderland.txt"]

Read the first 70 bytes of the United States Constitution:

ba=ReadByteArray["ExampleData/USConstitution.txt", 70]

Convert the bytes to text:

ByteArrayToString[%]

Read until either 70 bytes have been read or an end of line is encountered:

ReadByteArray["ExampleData/USConstitution.txt", {70,EndOfLine}]

Repeat the process:

ReadByteArray["ExampleData/USConstitution.txt", {70,EndOfLine}]

Close the file:

Close["ExampleData/USConstitution.txt"]

Open a system shell using (RefLink[StartProcess, paclet:ref/StartProcess]):

process=StartProcess[$SystemShell]

Clear out any welcome text:

ReadByteArray[process,EndOfBuffer]

Write a command to the process that generates output:

WriteString[process,"echo hello\n"];

Read the available output as a byte array:

ReadByteArray[process,EndOfBuffer]

Convert the bytes to a string and notice that it includes the end-of-line character:

ByteArrayToString[%]

Write the command to the process a second time:

WriteString[process,"echo hello\n"];

This time, read until the end of line is reached:

ReadByteArray[process,EndOfLine]

The byte array no longer encodes an end of line:

ByteArrayToString[%]

Kill the process:

KillProcess[process]

Open a socket connection:

socket = SocketConnect["http://exampledata.wolfram.com"]

Write a request to the socket:

WriteString[socket,"GET /cellularautomaton.gif HTTP/1.0 \n\n"]

Read until the byte sequence corresponding to GIF89 is read:

header=StringToByteArray["GIF89"]

ReadByteArray[socket,header]

Convert the data in the HTTP header to text:

ByteArrayToString[%]//Short

Read all remaining bytes:

ReadByteArray[socket]

Join the bytes with the GIF header and import the images:

ImportByteArray[Join[header,%],"GIF"]

Close the socket:

Close[socket]

OpenersExampleSection2(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)TimeConstraint  (1)

Start a shell and clear the output it prints on startup:

process=StartProcess[$SystemShell]
ReadByteArray[process,EndOfBuffer];

Attempt to read 5 bytes, but force a return after 2 seconds with what has been obtained:

ReadByteArray[process,5,TimeConstraint->2]

Write a command to the shell, causing it to print output:

WriteString[process,"echo hello\n"]

Attempt another time-constrained read, which returns immediately with 5 bytes:

ReadByteArray[process,EndOfLine,TimeConstraint->2]

End the process:

KillProcess[process]

OpenersExampleSection3(Spacer1)Properties & Relations  (5)

(RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src] is effectively equivalent to (RefLink[ByteArray, paclet:ref/ByteArray])[(RefLink[BinaryReadList, paclet:ref/BinaryReadList])[src]]:

ReadByteArray["ExampleData/strings"]==ByteArray[BinaryReadList["ExampleData/strings"]]

(RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src] is effectively equivalent to (RefLink[StringToByteArray, paclet:ref/StringToByteArray])[(RefLink[ReadString, paclet:ref/ReadString])[src], "ISO8859-1"]:

ReadByteArray["ExampleData/spikey.tiff"] ==StringToByteArray[ReadString["ExampleData/spikey.tiff"],"ISO8859-1"]

(RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src,n] reads n bytes at once:

ba=ReadByteArray["ExampleData/strings", 10]

(RefLink[BinaryRead, paclet:ref/BinaryRead])[src] reads one byte at a time:

SetStreamPosition["ExampleData/strings", 0];

table=Table[BinaryRead["ExampleData/strings"], 10]

The data is the same as read in by (RefLink[ReadByteArray, paclet:ref/ReadByteArray]):

ByteArray[table]==ba

Close the stream:

Close["ExampleData/strings"];

(RefLink[ReadByteArray, paclet:ref/ReadByteArray])[src] is effectively equivalent to (RefLink[StringToByteArray, paclet:ref/StringToByteArray])[(RefLink[Import, paclet:ref/Import])[src,"String"],"ISO8859-1"]:

ReadByteArray["ExampleData/strings"]==StringToByteArray[Import["ExampleData/strings","String"],"ISO8859-1"]

(RefLink[ImportByteArray, paclet:ref/ImportByteArray])[ReadByteArray[src]] is effectively equivalent to (RefLink[Import, paclet:ref/Import])[src]:

{ImportByteArray[ReadByteArray["ExampleData/spikey.tiff"]],Import["ExampleData/spikey.tiff"]}

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[BinaryReadList, paclet:ref/BinaryReadList])  ▪  (RefLink[BinaryWrite, paclet:ref/BinaryWrite])  ▪  (RefLink[ReadString, paclet:ref/ReadString])  ▪  (RefLink[ProcessObject, paclet:ref/ProcessObject])  ▪  (RefLink[InputStream, paclet:ref/InputStream])  ▪  (RefLink[LinkRead, paclet:ref/LinkRead])  ▪  (RefLink[ByteArray, paclet:ref/ByteArray])  ▪  (RefLink[ImportByteArray, paclet:ref/ImportByteArray])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Binary Data, paclet:guide/BinaryData])}, {▪(RefLinkPlain[Files, paclet:guide/Files])}, {▪(RefLinkPlain[Network Programming, paclet:guide/NetworkProgramming])}, {▪(RefLinkPlain[Importing and Exporting, paclet:guide/ImportingAndExporting])}, {▪(RefLinkPlain[Low-Level File Operations, paclet:guide/LowLevelFileOperations])}, {▪(RefLinkPlain[External Operations, paclet:guide/ExternalOperations])}, {▪(RefLinkPlain[File Operations, paclet:guide/FileOperations])}, {▪(RefLinkPlain[Direct Control of External Processes, paclet:guide/DirectControlOfExternalProcesses])}})}})