|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QByteArray
public class QByteArray
The QByteArray
class provides an array of bytes. QByteArray
can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray
is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.
In addition to QByteArray
, Qt also provides the QString class to store string data. For most purposes, QString is the class you want to use. It stores 16-bit Unicode characters, making it easy to store non-ASCII/non-Latin-1 characters in your application. Furthermore, QString is used throughout in the Qt API. The two main cases where QByteArray
is appropriate are when you need to store raw binary data, and when memory conservation is critical (e.g., with Qt for Embedded Linux).
One way to initialize a QByteArray
is simply to pass a const char * to its constructor. For example, the following code creates a byte array of size 5 containing the data "Hello":
QByteArray ba = new QByteArray("Hello");Although the
size()
is 5, the byte array also maintains an extra '\0' character at the end so that if a function is used that asks for a pointer to the underlying data (e.g. a call to data()
), the data pointed to is guaranteed to be '\0'-terminated. QByteArray
makes a deep copy of the const char * data, so you can modify it later without experiencing side effects. (If for performance reasons you don't want to take a deep copy of the character data, use QByteArray::fromRawData() instead.)
Another approach is to set the size of the array using resize()
and to initialize the data byte per byte. QByteArray
uses 0-based indexes, just like C++ arrays. To access the byte at a particular index position, you can use operator[](). On non-const byte arrays, operator[]() returns a reference to a byte that can be used on the left side of an assignment. For example:
QByteArray ba; ba.resize(5); ba[0] = 0x3c; ba[1] = 0xb8; ba[2] = 0x64; ba[3] = 0x18; ba[4] = 0xca;For read-only access, an alternative syntax is to use
at()
: for (int i = 0; i < ba.size(); ++i) { if (ba.at(i) >= 'a' && ba.at(i) <= 'f') System.out.println("Found character in range [a-f]"); }
at()
can be faster than operator[](), because it never causes a deep copy to occur. To extract many bytes at a time, use left()
, right()
, or mid()
.
A QByteArray
can embed '\0' bytes. The size()
function always returns the size of the whole array, including embedded '\0' bytes. If you want to obtain the length of the data up to and excluding the first '\0' character, call qstrlen() on the byte array.
After a call to resize()
, newly allocated bytes have undefined values. To set all the bytes to a particular value, call fill()
.
To obtain a pointer to the actual character data, call data()
or constData(). These functions return a pointer to the beginning of the data. The pointer is guaranteed to remain valid until a non-const function is called on the QByteArray
. It is also guaranteed that the data ends with a '\0' byte. This '\0' byte is automatically provided by QByteArray
and is not counted in size()
.
QByteArray
provides the following basic functions for modifying the byte data: append()
, prepend()
, insert()
, replace()
, and remove()
. For example:
QByteArray x = new QByteArray("and"); x.prepend("rock "); // x == "rock and" x.append(" roll"); // x == "rock and roll" x.replace(5, 3, new QByteArray("&")); // x == "rock & roll"The
replace()
and remove()
functions' first two arguments are the position from which to start erasing and the number of bytes that should be erased. When you append()
data to a non-empty array, the array will be reallocated and the new data copied to it. You can avoid this behavior by calling reserve()
, which preallocates a certain amount of memory. You can also call capacity()
to find out how much memory QByteArray
actually allocated. Data appended to an empty array is not copied.
A frequent requirement is to remove whitespace characters from a byte array ('\n', '\t', ' ', etc.). If you want to remove whitespace from both ends of a QByteArray
, use trimmed()
. If you want to remove whitespace from both ends and replace multiple consecutive whitespaces with a single space character within the byte array, use simplified()
.
If you want to find all occurrences of a particular character or substring in a QByteArray
, use indexOf()
or lastIndexOf()
. The former searches forward starting from a given index position, the latter searches backward. Both return the index position of the character or substring if they find it; otherwise, they return -1. For example, here's a typical loop that finds all occurrences of a particular substring:
QByteArray ba = new QByteArray("We must be <b>bold</b>, very <b>bold</b>"); int j = 0; while ((j = ba.indexOf("<b>", j)) != -1) { System.out.println("Found <b> tag at index position " + j); ++j; }If you simply want to check whether a
QByteArray
contains a particular character or substring, use contains()
. If you want to find out how many times a particular character or substring occurs in the byte array, use count()
. If you want to replace all occurrences of a particular value with another, use one of the two-parameter replace()
overloads. QByteArrays can be compared using overloaded operators such as operator<(), operator<=(), operator==(), operator>=(), and so on. The comparison is based exclusively on the numeric values of the characters and is very fast, but is not what a human would expect. QString::localeAwareCompare() is a better choice for sorting user-interface strings.
For historical reasons, QByteArray
distinguishes between a null byte array and an empty byte array. A null byte array is a byte array that is initialized using QByteArray
's default constructor or by passing (const char *)0 to the constructor. An empty byte array is any byte array with size 0. A null byte array is always empty, but an empty byte array isn't necessarily null:
new QByteArray().isNull(); // returns true new QByteArray().isEmpty(); // returns true new QByteArray("").isNull(); // returns false new QByteArray("").isEmpty(); // returns true new QByteArray("abc").isNull(); // returns false new QByteArray("abc").isEmpty(); // returns falseAll functions except
isNull()
treat null byte arrays the same as empty byte arrays. For example, data()
returns a pointer to a '\0' character for a null byte array (not a null pointer), and QByteArray() compares equal to QByteArray
(""). We recommend that you always use isEmpty()
and avoid isNull()
.QByteArray
, the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects functions that support a case insensitive option or that compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only ASCII characters. (If $LC_CTYPE is set, most Unix systems do "the right thing".) Functions that this affects include contains()
, indexOf()
, lastIndexOf()
, operator<(), operator<=(), operator>(), operator>=(), toLower()
and toUpper()
. This issue does not apply to QStrings since they represent characters using Unicode.
QBitArray
.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QByteArray()
|
|
QByteArray(byte[] data)
Constructs a byte array containing the bytes of array data. |
|
QByteArray(int size,
byte c)
Constructs a byte array of size size with every byte set to character ch. |
|
QByteArray(QByteArray arg__1)
Constructs a copy of other. |
|
QByteArray(java.lang.String s)
Constructs a byte array initialized with the string str. |
Method Summary | |
---|---|
QByteArray |
append(byte other)
This function appends other to the end of this QByteArray. |
QByteArray |
append(QByteArray other)
This function appends the contents of other to the end of this QByteArray. |
QByteArray |
append(java.lang.String other)
This function appends the contents of other to the end of this QByteArray. |
byte |
at(int i)
Returns the character at index position i in the byte array. |
int |
capacity()
Returns the maximum number of bytes that can be stored in the byte array without forcing a reallocation. |
void |
chop(int n)
Removes n bytes from the end of the byte array. |
void |
clear()
Clears the contents of the byte array and makes it empty. |
QByteArray |
clone()
This method is reimplemented for internal reasons |
boolean |
contains(byte c)
Returns true if the byte array contains the character ch; otherwise returns false. |
boolean |
contains(QByteArray a)
Returns true if the byte array contains an occurrence of the byte array ba; otherwise returns false. |
boolean |
contains(java.lang.String str)
Returns true there is at least one occurrence of str in this QByteArray. |
int |
count(byte c)
Returns the number of occurrences of character ch in the byte array. |
int |
count(QByteArray a)
Returns the number of (potentially overlapping) occurrences of byte array ba in this byte array. |
int |
count(java.lang.String str)
Returns the number of occurrences of str in this QByteArray. |
QNativePointer |
data()
Returns a pointer to the data stored in the byte array. |
boolean |
endsWith(byte c)
Returns true if this byte array ends with character ch; otherwise returns false. |
boolean |
endsWith(QByteArray a)
Returns true if this byte array ends with byte array ba; otherwise returns false. |
boolean |
endsWith(java.lang.String str)
Returns true if this QByteArray ends with str; otherwise, returns false. |
QByteArray |
fill(byte b)
Sets every byte in the byte array to character b. |
QByteArray |
fill(byte b,
int size)
Sets every byte in the byte array to character b. |
static QByteArray |
fromBase64(QByteArray base64)
Returns a decoded copy of the Base64 array base64. |
static QByteArray |
fromHex(QByteArray hexEncoded)
Returns a decoded copy of the hex encoded array hexEncoded. |
static QByteArray |
fromPercentEncoding(QByteArray pctEncoded)
Returns a decoded copy of the URI/URL-style percent-encoded input. |
static QByteArray |
fromPercentEncoding(QByteArray pctEncoded,
byte percent)
Returns a decoded copy of the URI/URL-style percent-encoded input. |
int |
indexOf(byte c)
Returns the index position of the first occurrence of the character ch in the byte array, searching forward from index position from. |
int |
indexOf(byte c,
int from)
Returns the index position of the first occurrence of the character ch in the byte array, searching forward from index position from. |
int |
indexOf(QByteArray a)
Returns the index position of the first occurrence of the byte array ba in this byte array, searching forward from index position from. |
int |
indexOf(QByteArray a,
int from)
Returns the index position of the first occurrence of the byte array ba in this byte array, searching forward from index position from. |
int |
indexOf(java.lang.String s)
Returns the index position of the first occurrence of the string str in the byte array, searching forward from index position from. |
int |
indexOf(java.lang.String s,
int from)
Returns the index position of the first occurrence of the string str in the byte array, searching forward from index position from. |
QByteArray |
insert(int i,
byte b)
Inserts byte b at index position i in the byte array. |
QByteArray |
insert(int i,
QByteArray ba)
Inserts the byte array ba at index position i and returns a reference to this byte array. |
QByteArray |
insert(int i,
java.lang.String str)
Inserts String str at index position i in the byte array. |
boolean |
isEmpty()
Returns true if the byte array has size 0; otherwise returns false. |
boolean |
isNull()
Returns true if this byte array is null; otherwise returns false. |
int |
lastIndexOf(byte c)
Returns the index position of the last occurrence of character ch in the byte array, searching backward from index position from. |
int |
lastIndexOf(byte c,
int from)
Returns the index position of the last occurrence of character ch in the byte array, searching backward from index position from. |
int |
lastIndexOf(QByteArray a)
Returns the index position of the last occurrence of the byte array ba in this byte array, searching backward from index position from. |
int |
lastIndexOf(QByteArray a,
int from)
Returns the index position of the last occurrence of the byte array ba in this byte array, searching backward from index position from. |
int |
lastIndexOf(java.lang.String s)
Returns the index position of the last occurrence of the string str in the byte array, searching backward from index position from. |
int |
lastIndexOf(java.lang.String s,
int from)
Returns the index position of the last occurrence of the string str in the byte array, searching backward from index position from. |
QByteArray |
left(int len)
Returns a byte array that contains the leftmost len bytes of this byte array. |
QByteArray |
leftJustified(int width)
Returns a byte array of size width that contains this byte array padded by the fill character. |
QByteArray |
leftJustified(int width,
byte fill)
Returns a byte array of size width that contains this byte array padded by the fill character. |
QByteArray |
leftJustified(int width,
byte fill,
boolean truncate)
Returns a byte array of size width that contains this byte array padded by the fill character. |
int |
length()
Same as size() . |
QByteArray |
mid(int index)
Returns a byte array containing len bytes from this byte array, starting at position pos. |
QByteArray |
mid(int index,
int len)
Returns a byte array containing len bytes from this byte array, starting at position pos. |
static QByteArray |
number(double arg__1)
Returns a byte array that contains the printed value of n, formatted in format f with precision prec. |
static QByteArray |
number(double arg__1,
byte f)
Returns a byte array that contains the printed value of n, formatted in format f with precision prec. |
static QByteArray |
number(double arg__1,
byte f,
int prec)
Returns a byte array that contains the printed value of n, formatted in format f with precision prec. |
static QByteArray |
number(int arg__1)
Returns a byte array containing the string equivalent of the number n to base base (10 by default). |
static QByteArray |
number(int arg__1,
int base)
Returns a byte array containing the string equivalent of the number n to base base (10 by default). |
static QByteArray |
number(long arg__1)
See also. toLongLong(). |
static QByteArray |
number(long arg__1,
int base)
Returns a byte array containing the string equivalent of the number arg__1 to base base. |
QByteArray |
prepend(byte other)
Prepends other to this QByteArray, and returns the resulting array. |
QByteArray |
prepend(QByteArray other)
Prepends other to this QByteArray, and returns the resulting array. |
QByteArray |
prepend(java.lang.String str)
Prepends str to this QByteArray, and returns the resulting array. |
void |
readFrom(QDataStream arg__1)
|
QByteArray |
remove(int pos,
int len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the array. |
QByteArray |
replace(byte before,
byte after)
Replaces every occurrence of before with after. |
QByteArray |
replace(byte before,
QByteArray after)
Replaces every occurrence of before with after. |
QByteArray |
replace(byte before,
java.lang.String after)
Replaces every occurrence of before with after. |
QByteArray |
replace(int pos,
int len,
QByteArray after)
Replaces every occurrence of before with after. |
QByteArray |
replace(QByteArray before,
QByteArray after)
Replaces every occurrence of before with after. |
QByteArray |
replace(QByteArray before,
java.lang.String after)
Replaces every occurrence of the byte array before with the string after. |
QByteArray |
replace(java.lang.String before,
QByteArray after)
Replaces every occurrence of before with after. |
QByteArray |
replace(java.lang.String before,
java.lang.String after)
Replaces every occurrence of before with after. |
void |
reserve(int size)
Attempts to allocate memory for at least size bytes. |
void |
resize(int size)
Sets the size of the byte array to size bytes. |
QByteArray |
right(int len)
Returns a byte array that contains the rightmost len bytes of this byte array. |
QByteArray |
rightJustified(int width)
Returns a byte array of size width that contains the fill character followed by this byte array. |
QByteArray |
rightJustified(int width,
byte fill)
Returns a byte array of size width that contains the fill character followed by this byte array. |
QByteArray |
rightJustified(int width,
byte fill,
boolean truncate)
Returns a byte array of size width that contains the fill character followed by this byte array. |
QByteArray |
set(QByteArray other)
This function sets the contents of this QByteArray equal to other. |
QByteArray |
setNum(char n)
See also. toUShort(). |
QByteArray |
setNum(char n,
int base)
See also. toShort(). |
QByteArray |
setNum(double n)
This is an overloaded function provided for convenience. |
QByteArray |
setNum(double n,
char f)
This is an overloaded function provided for convenience. |
QByteArray |
setNum(double n,
char f,
int prec)
Sets the byte array to the printed value of n, formatted in format f with precision prec, and returns a reference to the byte array. |
QByteArray |
setNum(float n)
Sets the byte array to the printed value of n, formatted in format f with precision 6, and returns a reference to the byte array. |
QByteArray |
setNum(float n,
char f)
Sets the byte array to the printed value of n, formatted in format f with precision 6, and returns a reference to the byte array. |
QByteArray |
setNum(float n,
char f,
int prec)
Sets the byte array to the printed value of n, formatted in format f with precision prec, and returns a reference to the byte array. |
QByteArray |
setNum(int n)
Sets the array to the printed value of n |
QByteArray |
setNum(int n,
int base)
Sets the array to the printed value of n using base as base. |
QByteArray |
setNum(long n)
This is an overloaded function provided for convenience. |
QByteArray |
setNum(long n,
int base)
This is an overloaded function provided for convenience. |
QByteArray |
setNum(short n)
This is an overloaded function provided for convenience. |
QByteArray |
setNum(short n,
int base)
This is an overloaded function provided for convenience. |
QByteArray |
simplified()
Returns a byte array that has whitespace removed from the start and the end, and which has each sequence of internal whitespace replaced with a single space. |
int |
size()
Returns the number of bytes in this byte array. |
java.util.List |
split(byte sep)
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays. |
void |
squeeze()
Releases any memory not required to store the array's data. |
boolean |
startsWith(byte c)
Returns true if this byte array starts with character ch; otherwise returns false. |
boolean |
startsWith(QByteArray a)
Returns true if this byte array starts with byte array ba; otherwise returns false. |
boolean |
startsWith(java.lang.String str)
Returns true if this byte array starts with string str; otherwise returns false. |
QByteArray |
toBase64()
Returns a copy of the byte array, encoded as Base64. |
byte[] |
toByteArray()
Returns the contents of this QByteArray. |
char |
toChar()
Returns the byte array converted to a char. |
char |
toChar(int base)
Returns the byte array converted to an char using base base, which is 10 by default and must be between 2 and 36, or 0. |
double |
toDouble()
Returns the byte array converted to an double. |
float |
toFloat()
Returns the byte array converted to an float. |
QByteArray |
toHex()
Returns a hex encoded copy of the byte array. |
int |
toInt()
Returns the byte array converted to an int using base 10. |
int |
toInt(int base)
Returns the byte array converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. |
QByteArray |
toLower()
Returns a lowercase copy of the byte array. |
QByteArray |
toPercentEncoding()
Returns a URI/URL-style percent-encoded copy of this byte array. |
QByteArray |
toPercentEncoding(QByteArray exclude)
Returns a URI/URL-style percent-encoded copy of this byte array. |
QByteArray |
toPercentEncoding(QByteArray exclude,
QByteArray include)
Returns a URI/URL-style percent-encoded copy of this byte array. |
QByteArray |
toPercentEncoding(QByteArray exclude,
QByteArray include,
byte percent)
Returns a URI/URL-style percent-encoded copy of this byte array. |
java.lang.String |
toString()
|
QByteArray |
toUpper()
Returns an uppercase copy of the byte array. |
QByteArray |
trimmed()
Returns a byte array that has whitespace removed from the start and the end. |
void |
truncate(int pos)
Truncates the byte array at index position pos. |
void |
writeTo(QDataStream arg__1)
|
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.lang.Comparable |
---|
compareTo |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QByteArray()
public QByteArray(QByteArray arg__1)
This operation takes constant time, because QByteArray
is implicitly shared. This makes returning a QByteArray
from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.
public QByteArray(int size, byte c)
fill()
.
public QByteArray(java.lang.String s)
QByteArray makes a deep copy of the string data.
public QByteArray(byte[] data)
If data is null, a null byte array is constructed.
QByteArray
makes a deep copy of the string data.
Method Detail |
---|
public final byte at(int i)
i must be a valid index position in the byte array (i.e., 0 <= i < size()
).
public final int capacity()
The sole purpose of this function is to provide a means of fine tuning QByteArray
's memory usage. In general, you will rarely ever need to call this function. If you want to know how many bytes are in the byte array, call size()
.
reserve()
, and squeeze()
.
public final void chop(int n)
If n is greater than size()
, the result is an empty byte array.
Example:
QByteArray ba = new QByteArray("STARTTLS\r\n"); ba.chop(2); // ba == "STARTTLS"
truncate()
, resize()
, and left()
.
public final void clear()
resize()
, and isEmpty()
.
public final boolean contains(byte c)
public final boolean contains(QByteArray a)
indexOf()
, and count()
.
public final int count(byte c)
contains()
, and indexOf()
.
public final int count(QByteArray a)
contains()
, and indexOf()
.
public final QNativePointer data()
Example:
QByteArray ba = new QByteArray("Hello world"); QNativePointer data = ba.data(); int i = 0; while (data.byteAt(i) != 0) { System.out.println("[" + data.byteAt(i) + "]"); ++i; }The pointer remains valid as long as the byte array isn't reallocated or destroyed. For read-only access, constData() is faster because it never causes a deep copy to occur.
This function is mostly useful to pass a byte array to a function that accepts a const char *.
Note: A QByteArray
can store any byte values including '\0's, but most functions that take char * arguments assume that the data ends at the first '\0' they encounter.
public final boolean endsWith(byte c)
public final boolean endsWith(QByteArray a)
Example:
QByteArray url = new QByteArray("http://www.trolltech.com/index.html"); if (url.endsWith(".html")) System.out.println("foo"); // ...
startsWith()
, and right()
.
public final int indexOf(byte c)
Example:
QByteArray ba = new QByteArray("ABCBA"); ba.indexOf("B"); // returns 1 ba.indexOf("B", 1); // returns 1 ba.indexOf("B", 2); // returns 3 ba.indexOf("X"); // returns -1
lastIndexOf()
, and contains()
.
public final int indexOf(byte c, int from)
Example:
QByteArray ba = new QByteArray("ABCBA"); ba.indexOf("B"); // returns 1 ba.indexOf("B", 1); // returns 1 ba.indexOf("B", 2); // returns 3 ba.indexOf("X"); // returns -1
lastIndexOf()
, and contains()
.
public final int indexOf(QByteArray a)
Example:
QByteArray x = new QByteArray("sticky question"); QByteArray y = new QByteArray("sti"); x.indexOf(y); // returns 0 x.indexOf(y, 1); // returns 10 x.indexOf(y, 10); // returns 10 x.indexOf(y, 11); // returns -1
lastIndexOf()
, contains()
, and count()
.
public final int indexOf(QByteArray a, int from)
Example:
QByteArray x = new QByteArray("sticky question"); QByteArray y = new QByteArray("sti"); x.indexOf(y); // returns 0 x.indexOf(y, 1); // returns 10 x.indexOf(y, 10); // returns 10 x.indexOf(y, 11); // returns -1
lastIndexOf()
, contains()
, and count()
.
public final int indexOf(java.lang.String s)
The Unicode data is converted into 8-bit characters using QString::toAscii().
If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString::toAscii() (or QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) explicitly if you want to convert the data to const char *.
public final int indexOf(java.lang.String s, int from)
The Unicode data is converted into 8-bit characters using QString::toAscii().
If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString::toAscii() (or QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) explicitly if you want to convert the data to const char *.
public final boolean isEmpty()
Example:
new QByteArray().isEmpty(); // returns true new QByteArray("").isEmpty(); // returns true new QByteArray("abc").isEmpty(); // returns false
size()
.
public final boolean isNull()
Example:
new QByteArray().isNull(); // returns true new QByteArray("").isNull(); // returns false new QByteArray("abc").isNull(); // returns falseQt makes a distinction between null byte arrays and empty byte arrays for historical reasons. For most applications, what matters is whether or not a byte array contains any data, and this can be determined using
isEmpty()
. isEmpty()
.
public final int lastIndexOf(byte c)
size()
- 1) byte. Returns -1 if ch could not be found. Example:
QByteArray ba = new QByteArray("ABCBA"); ba.lastIndexOf("B"); // returns 3 ba.lastIndexOf("B", 3); // returns 3 ba.lastIndexOf("B", 2); // returns 1 ba.lastIndexOf("X"); // returns -1
indexOf()
, and contains()
.
public final int lastIndexOf(byte c, int from)
size()
- 1) byte. Returns -1 if ch could not be found. Example:
QByteArray ba = new QByteArray("ABCBA"); ba.lastIndexOf("B"); // returns 3 ba.lastIndexOf("B", 3); // returns 3 ba.lastIndexOf("B", 2); // returns 1 ba.lastIndexOf("X"); // returns -1
indexOf()
, and contains()
.
public final int lastIndexOf(QByteArray a)
Example:
QByteArray x = new QByteArray("crazy azimuths"); QByteArray y = new QByteArray("azy"); x.lastIndexOf(y); // returns 6 x.lastIndexOf(y, 6); // returns 6 x.lastIndexOf(y, 5); // returns 2 x.lastIndexOf(y, 1); // returns -1
indexOf()
, contains()
, and count()
.
public final int lastIndexOf(QByteArray a, int from)
Example:
QByteArray x = new QByteArray("crazy azimuths"); QByteArray y = new QByteArray("azy"); x.lastIndexOf(y); // returns 6 x.lastIndexOf(y, 6); // returns 6 x.lastIndexOf(y, 5); // returns 2 x.lastIndexOf(y, 1); // returns -1
indexOf()
, contains()
, and count()
.
public final int lastIndexOf(java.lang.String s)
size()
- 1) byte. Returns -1 if str could not be found. The Unicode data is converted into 8-bit characters using QString::toAscii().
If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString::toAscii() (or QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) explicitly if you want to convert the data to const char *.
public final int lastIndexOf(java.lang.String s, int from)
size()
- 1) byte. Returns -1 if str could not be found. The Unicode data is converted into 8-bit characters using QString::toAscii().
If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString::toAscii() (or QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) explicitly if you want to convert the data to const char *.
public final QByteArray left(int len)
The entire byte array is returned if len is greater than size()
.
Example:
QByteArray x = new QByteArray("Pineapple"); QByteArray y = x.left(4); // y == "Pine"
right()
, mid()
, startsWith()
, and truncate()
.
public final QByteArray leftJustified(int width, byte fill)
If truncate is false and the size()
of the byte array is more than width, then the returned byte array is a copy of this byte array.
If truncate is true and the size()
of the byte array is more than width, then any bytes in a copy of the byte array after position width are removed, and the copy is returned.
Example:
QByteArray x = new QByteArray("apple"); QByteArray y = x.leftJustified(8, (byte)'.', false); // y == "apple..."
rightJustified()
.
public final QByteArray leftJustified(int width)
If truncate is false and the size()
of the byte array is more than width, then the returned byte array is a copy of this byte array.
If truncate is true and the size()
of the byte array is more than width, then any bytes in a copy of the byte array after position width are removed, and the copy is returned.
Example:
QByteArray x = new QByteArray("apple"); QByteArray y = x.leftJustified(8, (byte)'.', false); // y == "apple..."
rightJustified()
.
public final QByteArray leftJustified(int width, byte fill, boolean truncate)
If truncate is false and the size()
of the byte array is more than width, then the returned byte array is a copy of this byte array.
If truncate is true and the size()
of the byte array is more than width, then any bytes in a copy of the byte array after position width are removed, and the copy is returned.
Example:
QByteArray x = new QByteArray("apple"); QByteArray y = x.leftJustified(8, (byte)'.', false); // y == "apple..."
rightJustified()
.
public final int length()
size()
.
public final QByteArray mid(int index)
If len is -1 (the default), or pos + len >= size()
, returns a byte array containing all bytes starting at position pos until the end of the byte array.
Example:
QByteArray x = new QByteArray("Five pineapples"); QByteArray y = x.mid(5, 4); // y == "pine" QByteArray z = x.mid(5); // z == "pineapples"
left()
, and right()
.
public final QByteArray mid(int index, int len)
If len is -1 (the default), or pos + len >= size()
, returns a byte array containing all bytes starting at position pos until the end of the byte array.
Example:
QByteArray x = new QByteArray("Five pineapples"); QByteArray y = x.mid(5, 4); // y == "pine" QByteArray z = x.mid(5); // z == "pineapples"
left()
, and right()
.
public final void writeTo(QDataStream arg__1)
public final void readFrom(QDataStream arg__1)
public final void reserve(int size)
resize()
often you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QByteArray
will be a bit slower. The sole purpose of this function is to provide a means of fine tuning QByteArray
's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the byte array, call resize()
.
squeeze()
, and capacity()
.
public final void resize(int size)
If size is greater than the current size, the byte array is extended to make it size bytes with the extra bytes added to the end. The new bytes are uninitialized.
If size is less than the current size, bytes are removed from the end.
size()
.
public final QByteArray right(int len)
The entire byte array is returned if len is greater than size()
.
Example:
QByteArray x = new QByteArray("Pineapple"); QByteArray y = x.right(5); // y == "apple"
endsWith()
, left()
, and mid()
.
public final QByteArray rightJustified(int width, byte fill)
If truncate is false and the size of the byte array is more than width, then the returned byte array is a copy of this byte array.
If truncate is true and the size of the byte array is more than width, then the resulting byte array is truncated at position width.
Example:
QByteArray x = new QByteArray("apple"); QByteArray y = x.rightJustified(8, (byte)'.', false); // y == "...apple"
leftJustified()
.
public final QByteArray rightJustified(int width)
If truncate is false and the size of the byte array is more than width, then the returned byte array is a copy of this byte array.
If truncate is true and the size of the byte array is more than width, then the resulting byte array is truncated at position width.
Example:
QByteArray x = new QByteArray("apple"); QByteArray y = x.rightJustified(8, (byte)'.', false); // y == "...apple"
leftJustified()
.
public final QByteArray rightJustified(int width, byte fill, boolean truncate)
If truncate is false and the size of the byte array is more than width, then the returned byte array is a copy of this byte array.
If truncate is true and the size of the byte array is more than width, then the resulting byte array is truncated at position width.
Example:
QByteArray x = new QByteArray("apple"); QByteArray y = x.rightJustified(8, (byte)'.', false); // y == "...apple"
leftJustified()
.
public final QByteArray simplified()
Whitespace means any character for which the standard C++ isspace() function returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.
Example:
QByteArray ba = new QByteArray(" lots\t of\nwhitespace\r\n "); ba = ba.simplified(); // ba == "lots of whitespace";
trimmed()
.
public final int size()
The last byte in the byte array is at position size()
- 1. In addition, QByteArray
ensures that the byte at position size()
is always '\0', so that you can use the return value of data()
and constData() as arguments to functions that expect '\0'-terminated strings.
Example:
QByteArray ba = new QByteArray("Hello"); int n = ba.size(); // n == 5 byte a = ba.at(0); // returns 'H' byte b = ba.at(4); // returns 'o' byte c = ba.at(5); // returns '\0'
isEmpty()
, and resize()
.
public final java.util.List split(byte sep)
split()
returns a single-element list containing this byte array.
public final void squeeze()
The sole purpose of this function is to provide a means of fine tuning QByteArray
's memory usage. In general, you will rarely ever need to call this function.
reserve()
, and capacity()
.
public final boolean startsWith(byte c)
public final boolean startsWith(QByteArray a)
Example:
QByteArray url = new QByteArray("ftp://ftp.trolltech.com/"); if (url.startsWith("ftp:")) System.out.println("foo"); // ...
endsWith()
, and left()
.
public final QByteArray toBase64()
QByteArray text = new QByteArray("Qt is great!"); text.toBase64(); // returns "UXQgaXMgZ3JlYXQh"The algorithm used to encode Base64-encoded data is defined in RFC 2045.
fromBase64()
.
public final QByteArray toHex()
fromHex()
.
public final QByteArray toLower()
Example:
QByteArray x = new QByteArray("TROlltECH"); QByteArray y = x.toLower(); // y == "trolltech"
toUpper()
, and 8-bit Character Comparisons
.
public final QByteArray toPercentEncoding(QByteArray exclude, QByteArray include)
By default, this function will encode all characters that are not one of the following:
ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~"
To prevent characters from being encoded pass them to exclude. To force characters to be encoded pass them to include. The percent character is always encoded.
Example:
QByteArray text = "{a fishy string?}"; QByteArray ba = text.toPercentEncoding("{}", "s"); qDebug(ba.constData()); // prints "{a fi%73hy %73tring%3F}"The hex encoding uses the numbers 0-9 and the uppercase letters A-F.
fromPercentEncoding()
, and QUrl::toPercentEncoding()
.
public final QByteArray toPercentEncoding(QByteArray exclude)
By default, this function will encode all characters that are not one of the following:
ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~"
To prevent characters from being encoded pass them to exclude. To force characters to be encoded pass them to include. The percent character is always encoded.
Example:
QByteArray text = "{a fishy string?}"; QByteArray ba = text.toPercentEncoding("{}", "s"); qDebug(ba.constData()); // prints "{a fi%73hy %73tring%3F}"The hex encoding uses the numbers 0-9 and the uppercase letters A-F.
fromPercentEncoding()
, and QUrl::toPercentEncoding()
.
public final QByteArray toPercentEncoding()
By default, this function will encode all characters that are not one of the following:
ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~"
To prevent characters from being encoded pass them to exclude. To force characters to be encoded pass them to include. The percent character is always encoded.
Example:
QByteArray text = "{a fishy string?}"; QByteArray ba = text.toPercentEncoding("{}", "s"); qDebug(ba.constData()); // prints "{a fi%73hy %73tring%3F}"The hex encoding uses the numbers 0-9 and the uppercase letters A-F.
fromPercentEncoding()
, and QUrl::toPercentEncoding()
.
public final QByteArray toPercentEncoding(QByteArray exclude, QByteArray include, byte percent)
By default, this function will encode all characters that are not one of the following:
ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~"
To prevent characters from being encoded pass them to exclude. To force characters to be encoded pass them to include. The percent character is always encoded.
Example:
QByteArray text = "{a fishy string?}"; QByteArray ba = text.toPercentEncoding("{}", "s"); qDebug(ba.constData()); // prints "{a fi%73hy %73tring%3F}"The hex encoding uses the numbers 0-9 and the uppercase letters A-F.
fromPercentEncoding()
, and QUrl::toPercentEncoding()
.
public final QByteArray toUpper()
Example:
QByteArray x = new QByteArray("TROlltECH"); QByteArray y = x.toUpper(); // y == "TROLLTECH"
toLower()
, and 8-bit Character Comparisons
.
public final QByteArray trimmed()
Whitespace means any character for which the standard C++ isspace() function returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.
Example:
QByteArray ba = new QByteArray(" lots\t of\nwhitespace\r\n "); ba = ba.trimmed(); // ba == "lots\t of\nwhitespace";Unlike
simplified()
, trimmed()
leaves internal whitespace alone. simplified()
.
public final void truncate(int pos)
If pos is beyond the end of the array, nothing happens.
Example:
QByteArray ba = new QByteArray("Stockholm"); ba.truncate(5); // ba == "Stock"
chop()
, resize()
, and left()
.
public static QByteArray fromBase64(QByteArray base64)
For example:
QByteArray text = QByteArray.fromBase64(new QByteArray("UXQgaXMgZ3JlYXQh")); text.data(); // returns "Qt is great!"The algorithm used to decode Base64-encoded data is defined in RFC 2045.
toBase64()
.
public static QByteArray fromHex(QByteArray hexEncoded)
For example:
QByteArray text = QByteArray.fromHex(new QByteArray("517420697320677265617421")); text.data(); // returns "Qt is great!"
toHex()
.
public static QByteArray fromPercentEncoding(QByteArray pctEncoded)
For example:
QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33"); text.data(); // returns "Qt is great!"
toPercentEncoding()
, and QUrl::fromPercentEncoding()
.
public static QByteArray fromPercentEncoding(QByteArray pctEncoded, byte percent)
For example:
QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33"); text.data(); // returns "Qt is great!"
toPercentEncoding()
, and QUrl::fromPercentEncoding()
.
public static QByteArray number(double arg__1, byte f)
Argument n is formatted according to the f format specified, which is g by default, and can be any of the following:
e | format as [-]9.9e[+|-]999 |
E | format as [-]9.9E[+|-]999 |
f | format as [-]9.9 |
g | use e or f format, whichever is the most concise |
G | use E or f format, whichever is the most concise |
QByteArray ba = QByteArray.number(12.3456, (byte)'E', 3); // ba == 1.235E+01Note: The format of the number is not localized; the default C locale is used irrespective of the user's locale.
toDouble()
.
public static QByteArray number(double arg__1)
Argument n is formatted according to the f format specified, which is g by default, and can be any of the following:
e | format as [-]9.9e[+|-]999 |
E | format as [-]9.9E[+|-]999 |
f | format as [-]9.9 |
g | use e or f format, whichever is the most concise |
G | use E or f format, whichever is the most concise |
QByteArray ba = QByteArray.number(12.3456, (byte)'E', 3); // ba == 1.235E+01Note: The format of the number is not localized; the default C locale is used irrespective of the user's locale.
toDouble()
.
public static QByteArray number(double arg__1, byte f, int prec)
Argument n is formatted according to the f format specified, which is g by default, and can be any of the following:
e | format as [-]9.9e[+|-]999 |
E | format as [-]9.9E[+|-]999 |
f | format as [-]9.9 |
g | use e or f format, whichever is the most concise |
G | use E or f format, whichever is the most concise |
QByteArray ba = QByteArray.number(12.3456, (byte)'E', 3); // ba == 1.235E+01Note: The format of the number is not localized; the default C locale is used irrespective of the user's locale.
toDouble()
.
public static QByteArray number(int arg__1)
Example:
int n = 63; QByteArray.number(n); // returns "63" QByteArray.number(n, 16); // returns "3f" QByteArray.number(n, 16).toUpper(); // returns "3F"Note: The format of the number is not localized; the default C locale is used irrespective of the user's locale.
setNum()
, and toInt()
.
public static QByteArray number(int arg__1, int base)
Example:
int n = 63; QByteArray.number(n); // returns "63" QByteArray.number(n, 16); // returns "3f" QByteArray.number(n, 16).toUpper(); // returns "3F"Note: The format of the number is not localized; the default C locale is used irrespective of the user's locale.
setNum()
, and toInt()
.
public static QByteArray number(long arg__1)
public static QByteArray number(long arg__1, int base)
Example:
int n = 63;
QByteArray.number(n, 16); // returns "3f"
QByteArray.number(n, 16).upper(); // returns "3F"
public java.lang.String toString()
toString
in class java.lang.Object
public final boolean contains(java.lang.String str)
public final int count(java.lang.String str)
public final boolean endsWith(java.lang.String str)
public final QByteArray prepend(java.lang.String str)
public final QByteArray replace(QByteArray before, java.lang.String after)
public final QByteArray replace(java.lang.String before, java.lang.String after)
public final boolean startsWith(java.lang.String str)
public final byte[] toByteArray()
public final QByteArray set(QByteArray other)
public final char toChar(int base) throws java.lang.NumberFormatException
java.lang.NumberFormatException
public char toChar()
public final int toInt(int base) throws java.lang.NumberFormatException
If base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.
The function throws NumberFormatException if conversion fails.
QByteArray str = new QByteArray("FF");
int hex = str.toInt(16); // hex == 255,
int dec = str.toInt(10); // throws NumberFormatException
java.lang.NumberFormatException
public int toInt()
public final double toDouble() throws java.lang.NumberFormatException
java.lang.NumberFormatException
public final float toFloat() throws java.lang.NumberFormatException
java.lang.NumberFormatException
public final QByteArray append(java.lang.String other)
public final QByteArray append(QByteArray other)
public final QByteArray append(byte other)
public final QByteArray fill(byte b, int size)
Example:
QByteArray ba = new QByteArray("Istanbul");
ba.fill("o");
// ba == "oooooooo"
ba.fill("X", 2);
// ba == "XX"
public QByteArray fill(byte b)
public final QByteArray insert(int i, QByteArray ba)
Example:
QByteArray ba = new QByteArray("Meal");
ba.insert(1, QByteArray("ontr"));
// ba == "Montreal"
public final QByteArray insert(int i, byte b)
public final QByteArray insert(int i, java.lang.String str)
public final QByteArray prepend(QByteArray other)
public final QByteArray prepend(byte other)
public final QByteArray remove(int pos, int len)
If pos is out of range, nothing happens. If pos is valid, but pos + len is larger than the size of the array, the array is truncated at position pos.
Example:
QByteArray ba = new QByteArray("Montreal");
ba.remove(1, 4);
// ba == "Meal"
public final QByteArray replace(byte before, QByteArray after)
public final QByteArray replace(int pos, int len, QByteArray after)
public final QByteArray replace(byte before, java.lang.String after)
public final QByteArray replace(byte before, byte after)
public final QByteArray replace(QByteArray before, QByteArray after)
public final QByteArray replace(java.lang.String before, QByteArray after)
public final QByteArray setNum(int n, int base)
public QByteArray setNum(int n)
public final QByteArray setNum(char n, int base)
public QByteArray setNum(char n)
public final QByteArray setNum(float n, char f, int prec)
public final QByteArray setNum(float n, char f)
public final QByteArray setNum(float n)
public final QByteArray setNum(long n, int base)
public QByteArray setNum(long n)
public final QByteArray setNum(double n, char f, int prec)
public final QByteArray setNum(double n, char f)
public final QByteArray setNum(double n)
public final QByteArray setNum(short n, int base)
public QByteArray setNum(short n)
public QByteArray clone()
clone
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |