Qt Jambi Home

com.trolltech.qt.core
Class QByteArray

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QByteArray
All Implemented Interfaces:
QtJambiInterface, java.lang.Comparable

public class QByteArray
extends QtJambiObject
implements java.lang.Comparable

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 Qtopia Core).

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("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')
            cout << "Found character in range [a-f]" << endl;
    }

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("and");
    x.prepend("rock ");         // x == "rock and"
    x.append(" roll");          // x == "rock and roll"
    x.replace(5, 3, "&");       // 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("We must be <b>bold</b>, very <b>bold</b>");
    int j = 0;
    while ((j = ba.indexOf("<b>", j)) != -1) {
        cout << "Found <b> tag at index position " << j << endl;
        ++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:

    QByteArray().isNull();          // returns true
    QByteArray().isEmpty();         // returns true

    QByteArray("").isNull();        // returns false
    QByteArray("").isEmpty();       // returns true

    QByteArray("abc").isNull();     // returns false
    QByteArray("abc").isEmpty();    // returns false

All 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.

Note on 8-bit Character Comparisons

In 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.

See Also:
QString, QBitArray

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Constructor Summary
QByteArray()
          Constructs an empty byte array.
QByteArray(byte[] data)
          Constructs a new QByteArray with the contents of data.
QByteArray(int size, byte c)
          Constructs a byte array of size size with every byte set to character c.
QByteArray(QByteArray arg__1)
          Constructs a copy of arg__1.
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.
 int compareTo(java.lang.Object other)
          
 boolean contains(byte c)
          Returns true if the byte array contains the character c; otherwise returns false.
 boolean contains(QByteArray a)
          Returns true if the byte array contains an occurrence of the byte array a; 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 c in the byte array.
 int count(QByteArray a)
          Returns the number of (potentially overlapping) occurrences of byte array a 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 c; otherwise returns false.
 boolean endsWith(QByteArray a)
          Returns true if this byte array ends with byte array a; otherwise returns false.
 boolean endsWith(java.lang.String str)
          Returns true if this QByteArray ends with str; otherwise, returns false.
 boolean equals(java.lang.Object other)
          
 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 fromNativePointer(QNativePointer nativePointer)
          This function returns the QByteArray instance pointed to by nativePointer
 int hashCode()
          
 int indexOf(byte c)
          Equivalent to indexOf(c, 0).
 int indexOf(byte c, int from)
          Returns the index position of the first occurrence of the character c in the byte array, searching forward from index position from.
 int indexOf(QByteArray a)
          Equivalent to indexOf(a, 0).
 int indexOf(QByteArray a, int from)
          Returns the index position of the first occurrence of the byte array a in this byte array, searching forward from index position from.
 int indexOf(java.lang.String s)
          Equivalent to indexOf(s, 0).
 int indexOf(java.lang.String s, int from)
          Returns the index position of the first occurrence of the string s 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)
          Equivalent to lastIndexOf(c, -1).
 int lastIndexOf(byte c, int from)
          Returns the index position of the last occurrence of character c in the byte array, searching backward from index position from.
 int lastIndexOf(QByteArray a)
          Equivalent to lastIndexOf(a, -1).
 int lastIndexOf(QByteArray a, int from)
          Returns the index position of the last occurrence of the byte array a in this byte array, searching backward from index position from.
 int lastIndexOf(java.lang.String s)
          Equivalent to lastIndexOf(s, -1).
 int lastIndexOf(java.lang.String s, int from)
          Returns the index position of the last occurrence of the string s 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)
          Equivalent to leftJustified(width, ' ', false).
 QByteArray leftJustified(int width, byte fill)
          Equivalent to leftJustified(width, fill, false).
 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)
          Equivalent to mid(index, -1).
 QByteArray mid(int index, int len)
          Returns a byte array containing len bytes from this byte array, starting at position index.
static QNativePointer nativePointerArray(QByteArray[] array)
          This function returns a QNativePointer that is pointing to the specified QByteArray array.
static QByteArray number(double arg__1)
          Equivalent to number(arg__1, 'g', 6).
static QByteArray number(double arg__1, byte f)
          Equivalent to number(arg__1, f, 6).
static QByteArray number(double arg__1, byte f, int prec)
          Returns a byte array that contains the printed value of arg__1, formatted in format f with precision prec.
static QByteArray number(int arg__1)
          Equivalent to number(arg__1, 10).
static QByteArray number(int arg__1, int base)
          Returns a byte array containing the string equivalent of the number arg__1 to base base (10 by default).
static QByteArray number(long arg__1)
          Equivalent to number(arg__1, 10).
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)
          Reads a QByteArray from 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)
          Equivalent to rightJustified(width, ' ', false).
 QByteArray rightJustified(int width, byte fill)
          Equivalent to rightJustified(width, fill, false).
 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(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<QByteArray> 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 c; otherwise returns false.
 boolean startsWith(QByteArray a)
          Returns true if this byte array starts with byte array a; 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.
 java.lang.String toString()
          Returns a string representation of this QByteArray.
 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)
          Writes thisQByteArray to arg__1.
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
 
Methods inherited from class java.lang.Object
clone, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QByteArray

public QByteArray(QByteArray arg__1)

Constructs a copy of 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.

See Also:
operator=

QByteArray

public QByteArray(int size,
                  byte c)

Constructs a byte array of size size with every byte set to character c.

See Also:
fill

QByteArray

public QByteArray()

Constructs an empty byte array.

See Also:
isEmpty

QByteArray

public QByteArray(java.lang.String s)
Constructs a byte array initialized with the string str.

QByteArray makes a deep copy of the string data.


QByteArray

public QByteArray(byte[] data)
Constructs a new QByteArray with the contents of data.

Method Detail

at

public final byte at(int i)

Returns the character at index position i in the byte array.

i must be a valid index position in the byte array (i.e., 0 <= i < size).

See Also:
operator[]

capacity

public final int capacity()

Returns the maximum number of bytes that can be stored in the byte array without forcing a reallocation.

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.

See Also:
reserve, squeeze

chop

public final void chop(int n)

Removes n bytes from the end of the byte array.

If n is greater than size, the result is an empty byte array.

Example:

    QByteArray ba("STARTTLS\r\n");
    ba.chop(2);                 // ba == "STARTTLS"

See Also:
truncate, resize, left

clear

public final void clear()

Clears the contents of the byte array and makes it empty.

See Also:
resize, isEmpty

contains

public final boolean contains(byte c)

Returns true if the byte array contains the character c; otherwise returns false.


contains

public final boolean contains(QByteArray a)

Returns true if the byte array contains an occurrence of the byte array a; otherwise returns false.

See Also:
indexOf, count

count

public final int count(byte c)

Returns the number of occurrences of character c in the byte array.

See Also:
contains, indexOf

count

public final int count(QByteArray a)

Returns the number of (potentially overlapping) occurrences of byte array a in this byte array.

See Also:
contains, indexOf

data

public final QNativePointer data()

Returns a pointer to the data stored in the byte array. The pointer can be used to access and modify the bytes that compose the array. The data is '\0'-terminated.

Example:

    QByteArray ba("Hello world");
    char *data = ba.data();
    while (*data) {
        cout << "[" << *data << "]" << endl;
        ++data;
    }

The pointer remains valid as long as the byte array isn't reallocated. 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.

See Also:
constData, operator[]

endsWith

public final boolean endsWith(QByteArray a)

Returns true if this byte array ends with byte array a; otherwise returns false.

Example:

    QByteArray url("http://www.trolltech.com/index.html");
    if (url.endsWith(".html"))
        ...

See Also:
startsWith, right

endsWith

public final boolean endsWith(byte c)

Returns true if this byte array ends with character c; otherwise returns false.


indexOf

public final int indexOf(byte c)

Equivalent to indexOf(c, 0).


indexOf

public final int indexOf(byte c,
                         int from)

Returns the index position of the first occurrence of the character c in the byte array, searching forward from index position from. Returns -1 if c could not be found.

Example:

    QByteArray ba("ABCBA");
    ba.indexOf("B");            // returns 1
    ba.indexOf("B", 1);         // returns 1
    ba.indexOf("B", 2);         // returns 3
    ba.indexOf("X");            // returns -1

See Also:
lastIndexOf, contains

indexOf

public final int indexOf(QByteArray a)

Equivalent to indexOf(a, 0).


indexOf

public final int indexOf(QByteArray a,
                         int from)

Returns the index position of the first occurrence of the byte array a in this byte array, searching forward from index position from. Returns -1 if a could not be found.

Example:

    QByteArray x("sticky question");
    QByteArray y("sti");
    x.indexOf(y);               // returns 0
    x.indexOf(y, 1);            // returns 10
    x.indexOf(y, 10);           // returns 10
    x.indexOf(y, 11);           // returns -1

See Also:
lastIndexOf, contains, count

indexOf

public final int indexOf(java.lang.String s)

Equivalent to indexOf(s, 0).


indexOf

public final int indexOf(java.lang.String s,
                         int from)

Returns the index position of the first occurrence of the string s in the byte array, searching forward from index position from. Returns -1 if s 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 *.


isEmpty

public final boolean isEmpty()

Returns true if the byte array has size 0; otherwise returns false.

Example:

    QByteArray().isEmpty();         // returns true
    QByteArray("").isEmpty();       // returns true
    QByteArray("abc").isEmpty();    // returns false

See Also:
size

isNull

public final boolean isNull()

Returns true if this byte array is null; otherwise returns false.

Example:

    QByteArray().isNull();          // returns true
    QByteArray("").isNull();        // returns false
    QByteArray("abc").isNull();     // returns false

Qt 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.

See Also:
isEmpty

lastIndexOf

public final int lastIndexOf(byte c)

Equivalent to lastIndexOf(c, -1).


lastIndexOf

public final int lastIndexOf(byte c,
                             int from)

Returns the index position of the last occurrence of character c in the byte array, searching backward from index position from. If from is -1 (the default), the search starts at the last (size - 1) byte. Returns -1 if c could not be found.

Example:

    QByteArray ba("ABCBA");
    ba.lastIndexOf("B");        // returns 3
    ba.lastIndexOf("B", 3);     // returns 3
    ba.lastIndexOf("B", 2);     // returns 1
    ba.lastIndexOf("X");        // returns -1

See Also:
indexOf, contains

lastIndexOf

public final int lastIndexOf(java.lang.String s)

Equivalent to lastIndexOf(s, -1).


lastIndexOf

public final int lastIndexOf(java.lang.String s,
                             int from)

Returns the index position of the last occurrence of the string s in the byte array, searching backward from index position from. If from is -1 (the default), the search starts at the last (size - 1) byte. Returns -1 if s 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 *.


lastIndexOf

public final int lastIndexOf(QByteArray a)

Equivalent to lastIndexOf(a, -1).


lastIndexOf

public final int lastIndexOf(QByteArray a,
                             int from)

Returns the index position of the last occurrence of the byte array a in this byte array, searching backward from index position from. If from is -1 (the default), the search starts at the last byte. Returns -1 if a could not be found.

Example:

    QByteArray x("crazy azimuths");
    QByteArray y("azy");
    x.lastIndexOf(y);           // returns 6
    x.lastIndexOf(y, 6);        // returns 6
    x.lastIndexOf(y, 5);        // returns 2
    x.lastIndexOf(y, 1);        // returns -1

See Also:
indexOf, contains, count

left

public final QByteArray left(int len)

Returns a byte array that contains the leftmost len bytes of this byte array.

The entire byte array is returned if len is greater than size.

Example:

    QByteArray x("Pineapple");
    QByteArray y = x.left(4);
    // y == "Pine"

See Also:
right, mid, startsWith, truncate

leftJustified

public final QByteArray leftJustified(int width,
                                      byte fill)

Equivalent to leftJustified(width, fill, false).


leftJustified

public final QByteArray leftJustified(int width)

Equivalent to leftJustified(width, ' ', false).


leftJustified

public final 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.

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("apple");
    QByteArray y = x.leftJustified(8, '.');   // y == "apple..."

See Also:
rightJustified

length

public final int length()

Same as size.


mid

public final QByteArray mid(int index)

Equivalent to mid(index, -1).


mid

public final QByteArray mid(int index,
                            int len)

Returns a byte array containing len bytes from this byte array, starting at position index.

If len is -1 (the default), or index + len >= size, returns a byte array containing all bytes starting at position index until the end of the byte array.

Example:

    QByteArray x("Five pineapples");
    QByteArray y = x.mid(5, 4);     // y == "pine"
    QByteArray z = x.mid(5);        // z == "pineapples"

See Also:
left, right

writeTo

public final void writeTo(QDataStream arg__1)
Writes thisQByteArray to arg__1.


readFrom

public final void readFrom(QDataStream arg__1)
Reads a QByteArray from arg__1.


reserve

public final void reserve(int size)

Attempts to allocate memory for at least size bytes. If you know in advance how large the byte array will be, you can call this function, and if you call 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.

See Also:
squeeze, capacity

resize

public final void resize(int size)

Sets the size of the byte array to size bytes.

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.

See Also:
size

right

public final QByteArray right(int len)

Returns a byte array that contains the rightmost len bytes of this byte array.

The entire byte array is returned if len is greater than size.

Example:

    QByteArray x("Pineapple");
    QByteArray y = x.right(5);
    // y == "apple"

See Also:
endsWith, left, mid

rightJustified

public final QByteArray rightJustified(int width,
                                       byte fill)

Equivalent to rightJustified(width, fill, false).


rightJustified

public final QByteArray rightJustified(int width)

Equivalent to rightJustified(width, ' ', false).


rightJustified

public final 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.

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("apple");
    QByteArray y = x.rightJustified(8, '.');    // y == "...apple"

See Also:
leftJustified

simplified

public final 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.

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("  lots\t of\nwhitespace\r\n ");
    ba = ba.simplified();
    // ba == "lots of whitespace";

See Also:
trimmed

size

public final int size()

Returns the number of bytes in this byte array.

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("Hello");
    int n = ba.size();          // n == 5
    ba.data()[0];               // returns 'H'
    ba.data()[4];               // returns 'o'
    ba.data()[5];               // returns '\0'

See Also:
isEmpty, resize

split

public final java.util.List<QByteArray> split(byte sep)

Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays. If sep does not match anywhere in the byte array, split returns a single-element list containing this byte array.


squeeze

public final void squeeze()

Releases any memory not required to store the array's data.

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.

See Also:
reserve, capacity

startsWith

public final boolean startsWith(byte c)

Returns true if this byte array starts with character c; otherwise returns false.


startsWith

public final boolean startsWith(QByteArray a)

Returns true if this byte array starts with byte array a; otherwise returns false.

Example:

    QByteArray url("ftp://ftp.trolltech.com/");
    if (url.startsWith("ftp:"))
        ...

See Also:
endsWith, left

toBase64

public final QByteArray toBase64()

Returns a copy of the byte array, encoded as Base64.

    QByteArray text("Qt is great!");
    text.toBase64();        // returns "UXQgaXMgZ3JlYXQh"

The algorithm used to encode Base64-encoded data is defined in RFC 2045.

See Also:
fromBase64

toHex

public final QByteArray toHex()

Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and the letters a-f.

See Also:
fromHex

toLower

public final QByteArray toLower()

Returns a lowercase copy of the byte array. The bytearray is interpreted as a Latin-1 encoded string.

Example:

    QByteArray x("TROlltECH");
    QByteArray y = x.toLower();
    // y == "trolltech"

See Also:
toUpper, Note on 8-bit character comparisons

toUpper

public final QByteArray toUpper()

Returns an uppercase copy of the byte array. The bytearray is interpreted as a Latin-1 encoded string.

Example:

    QByteArray x("TROlltECH");
    QByteArray y = x.toUpper();
    // y == "TROLLTECH"

See Also:
toLower, Note on 8-bit character comparisons

trimmed

public final QByteArray trimmed()

Returns a byte array that has whitespace removed from the start and the end.

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("  lots\t of\nwhitespace\r\n ");
    ba = ba.trimmed();
    // ba == "lots\t of\nwhitespace";

Unlike simplified, trimmed leaves internal whitespace alone.

See Also:
simplified

truncate

public final void truncate(int pos)

Truncates the byte array at index position pos.

If pos is beyond the end of the array, nothing happens.

Example:

    QByteArray ba("Stockholm");
    ba.truncate(5);             // ba == "Stock"

See Also:
chop, resize, left

fromBase64

public static QByteArray fromBase64(QByteArray base64)

Returns a decoded copy of the Base64 array base64. Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

For example:

    QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh");
    text.data();            // returns "Qt is great!"

The algorithm used to decode Base64-encoded data is defined in RFC 2045.

See Also:
toBase64

fromHex

public static QByteArray fromHex(QByteArray hexEncoded)

Returns a decoded copy of the hex encoded array hexEncoded. Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

For example:

    QByteArray text = QByteArray::fromHex("517420697320677265617421");
    text.data();            // returns "Qt is great!"

See Also:
toHex

number

public static QByteArray number(double arg__1,
                                byte f)

Equivalent to number(arg__1, f, 6).


number

public static QByteArray number(double arg__1)

Equivalent to number(arg__1, 'g', 6).


number

public static QByteArray number(double arg__1,
                                byte f,
                                int prec)

Returns a byte array that contains the printed value of arg__1, formatted in format f with precision prec.

Argument arg__1 is formatted according to the f format specified, which is g by default, and can be any of the following:

FormatMeaning
eformat as [-]9.9e[+|-]999
Eformat as [-]9.9E[+|-]999
fformat as [-]9.9
guse e or f format, whichever is the most concise
Guse E or f format, whichever is the most concise

With 'e', 'E', and 'f', prec is the number of digits after the decimal point. With 'g' and 'G', prec is the maximum number of significant digits (trailing zeroes are omitted).

    QByteArray ba = QByteArray::number(12.3456, 'E', 3);
    // ba == 1.235E+01

See Also:
toDouble

number

public static QByteArray number(long arg__1)

Equivalent to number(arg__1, 10).


number

public static QByteArray number(long arg__1,
                                int base)
Returns a byte array containing the string equivalent of the number arg__1 to base base. The base can be any value between 2 and 36.

Example:

    int n = 63;
    QByteArray.number(n, 16);          // returns "3f"
    QByteArray.number(n, 16).upper();  // returns "3F"


number

public static QByteArray number(int arg__1)

Equivalent to number(arg__1, 10).


number

public static QByteArray number(int arg__1,
                                int base)

Returns a byte array containing the string equivalent of the number arg__1 to base base (10 by default). The base can be any value between 2 and 36.

Example:

    int n = 63;
    QByteArray::number(n);              // returns "63"
    QByteArray::number(n, 16);          // returns "3f"
    QByteArray::number(n, 16).toUpper();  // returns "3F"

See Also:
setNum, toInt

fromNativePointer

public static QByteArray fromNativePointer(QNativePointer nativePointer)
This function returns the QByteArray instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

nativePointerArray

public static QNativePointer nativePointerArray(QByteArray[] array)
This function returns a QNativePointer that is pointing to the specified QByteArray array.

Parameters:
array - the array that the returned pointer will point to.
Returns:
a QNativePointer that is pointing to the specified array.

equals

public boolean equals(java.lang.Object other)

Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(java.lang.Object other)

Specified by:
compareTo in interface java.lang.Comparable

hashCode

public int hashCode()

Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Returns a string representation of this QByteArray.

Overrides:
toString in class java.lang.Object

toByteArray

public final byte[] toByteArray()
Returns the contents of this QByteArray.


toChar

public final char toChar(int base)
                  throws java.lang.NumberFormatException
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.

Throws:
java.lang.NumberFormatException

toChar

public char toChar()
Returns the byte array converted to a char.


toInt

public final int toInt(int base)
                throws java.lang.NumberFormatException
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.

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

Throws:
java.lang.NumberFormatException

toInt

public int toInt()
Returns the byte array converted to an int using base 10.


toDouble

public final double toDouble()
                      throws java.lang.NumberFormatException
Returns the byte array converted to an double.

Throws:
java.lang.NumberFormatException

toFloat

public final float toFloat()
                    throws java.lang.NumberFormatException
Returns the byte array converted to an float.

Throws:
java.lang.NumberFormatException

set

public QByteArray set(QByteArray other)
This function sets the contents of this QByteArray equal to other.


append

public final QByteArray append(java.lang.String other)
This function appends the contents of other to the end of this QByteArray.


append

public final QByteArray append(QByteArray other)
This function appends the contents of other to the end of this QByteArray.


append

public final QByteArray append(byte other)
This function appends other to the end of this QByteArray.


contains

public boolean contains(java.lang.String str)
Returns true there is at least one occurrence of str in this QByteArray.


count

public int count(java.lang.String str)
Returns the number of occurrences of str in this QByteArray.


endsWith

public boolean endsWith(java.lang.String str)
Returns true if this QByteArray ends with str; otherwise, returns false.


fill

public final QByteArray fill(byte b,
                             int size)
Sets every byte in the byte array to character b. If size is different from -1 (the default), the byte array is resized to size size beforehand.

Example:

    QByteArray ba = new QByteArray("Istanbul");
    ba.fill("o");
    // ba == "oooooooo"

    ba.fill("X", 2);
    // ba == "XX"


fill

public QByteArray fill(byte b)
Sets every byte in the byte array to character b.


insert

public final QByteArray insert(int i,
                               QByteArray ba)
Inserts the byte array ba at index position i and returns a reference to this byte array.

Example:

    QByteArray ba = new QByteArray("Meal");
    ba.insert(1, QByteArray("ontr"));
    // ba == "Montreal"


insert

public final QByteArray insert(int i,
                               byte b)
Inserts byte b at index position i in the byte array. If i is greater than size(), the array is first extended using resize().


insert

public final QByteArray insert(int i,
                               java.lang.String str)
Inserts String str at index position i in the byte array. If i is greater than size(), the array is first extended using resize().


prepend

public final QByteArray prepend(QByteArray other)
Prepends other to this QByteArray, and returns the resulting array.


prepend

public final QByteArray prepend(byte other)
Prepends other to this QByteArray, and returns the resulting array.


prepend

public QByteArray prepend(java.lang.String str)
Prepends str to this QByteArray, and returns the resulting array.


remove

public final QByteArray remove(int pos,
                               int len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the array.

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"


replace

public QByteArray replace(QByteArray before,
                          java.lang.String after)
Replaces every occurrence of the byte array before with the string after.


replace

public final QByteArray replace(byte before,
                                QByteArray after)
Replaces every occurrence of before with after.


replace

public QByteArray replace(java.lang.String before,
                          java.lang.String after)
Replaces every occurrence of before with after.


replace

public final QByteArray replace(int pos,
                                int len,
                                QByteArray after)
Replaces every occurrence of before with after.


replace

public final QByteArray replace(byte before,
                                java.lang.String after)
Replaces every occurrence of before with after.


replace

public final QByteArray replace(byte before,
                                byte after)
Replaces every occurrence of before with after.


replace

public final QByteArray replace(QByteArray before,
                                QByteArray after)
Replaces every occurrence of before with after.


replace

public final QByteArray replace(java.lang.String before,
                                QByteArray after)
Replaces every occurrence of before with after.


setNum

public final QByteArray setNum(int n,
                               int base)
Sets the array to the printed value of n using base as base.


setNum

public QByteArray setNum(int n)
Sets the array to the printed value of n


setNum

public final 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.


setNum

public final 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.


setNum

public final 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.


setNum

public final QByteArray setNum(long n,
                               int base)
This is an overloaded function provided for convenience.


setNum

public QByteArray setNum(long n)
This is an overloaded function provided for convenience.


setNum

public final 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.


setNum

public final QByteArray setNum(double n,
                               char f)
This is an overloaded function provided for convenience.


setNum

public final QByteArray setNum(double n)
This is an overloaded function provided for convenience.


setNum

public final QByteArray setNum(short n,
                               int base)
This is an overloaded function provided for convenience.


setNum

public QByteArray setNum(short n)
This is an overloaded function provided for convenience.


startsWith

public boolean startsWith(java.lang.String str)
Returns true if this byte array starts with string str; otherwise returns false.


Qt Jambi Home