
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
82
|
Chapter 2: Strings and Characters
// The input string
string text = "abcdefghijklmnopqrstuvwxyz";
char[] inputBuffer = null;
if ((index + size) < text.Length)
{
// Create the buffer to return (we are not finished).
inputBuffer = text.ToCharArray(index, size);
isLastBuffer = false;
}
else
{
// Create the buffer to return (we are finished).
inputBuffer = text.ToCharArray(index, text.Length - index);
isLastBuffer = true;
}
// Increment the index to the next chunk of data in text.
index += size;
return (inputBuffer);
}
Discussion
In this recipe you use the GetInputBuffer method to pass chunks of data, in this case
character arrays of size six, back to the
ConvertBlocksOfData method. In this method
the chunks of data are fed into the
Convert method, which keeps accepting chunks of
data until the
GetInputBuffer method returns true in its isLastBuffer out parameter.
This signals the
Convert method that it is finished creating the byte array and it is
time to clean up. The result of this is that the
Convert method creates a single contin-
uous
byte array converted to a particular encoding from individual chunks of data in
the form of character arrays.
The
Convert method was chosen because it was designed to be used to encode data
of an unspecified size, as well as data that arrives in chunks ...