# StringAttribute

<a id="stringattribute"></a>

# StringAttribute

```cpp
#include <icy/stun/attributes.h>

class StringAttribute
```

[Defined in src/stun/include/icy/stun/attributes.h:369](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L369)

> **Inherits:** [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute)

Implements a STUN/TURN attribute that holds an arbitrary byte string. Used for Username, Password, Realm, Nonce, Software, Data, and similar attributes.

## List of all members

| Name | Kind | Owner |
|------|------|-------|
| [`StringAttribute`](#stringattribute-1) | `function` | Declared here |
| [`StringAttribute`](#stringattribute-2) | `function` | Declared here |
| [`~StringAttribute`](#stringattribute-3) | `function` | Declared here |
| [`clone`](#clone-17) | `function` | Declared here |
| [`bytes`](#bytes-1) | `function` | Declared here |
| [`setBytes`](#setbytes) | `function` | Declared here |
| [`asString`](#asstring) | `function` | Declared here |
| [`copyBytes`](#copybytes) | `function` | Declared here |
| [`copyBytes`](#copybytes-1) | `function` | Declared here |
| [`getByte`](#getbyte) | `function` | Declared here |
| [`setByte`](#setbyte) | `function` | Declared here |
| [`read`](#read-12) | `function` | Declared here |
| [`write`](#write-41) | `function` | Declared here |
| [`_bytes`](#_bytes-2) | `variable` | Declared here |
| [`~Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`clone`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#clone-12) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`read`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#read-7) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`write`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#write-36) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-17) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#size-12) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`consumePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#consumepadding) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`writePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#writepadding) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`TypeID`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typeid) | `variable` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`create`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#create-10) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring-1) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`_type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_type-1) | `variable` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`_size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_size-3) | `variable` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-2) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`setLength`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#setlength) | `function` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |
| [`Type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-18) | `enum` | Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) |

## Inherited from [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute)

| Kind | Name | Description |
|------|------|-------------|
| `function` | [`~Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-1) `virtual` `inline` |  |
| `function` | [`clone`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#clone-12) `virtual` | Returns a deep copy of this attribute. |
| `function` | [`read`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#read-7) `virtual` | Reads the body (not the type or size) for this type of attribute from the given buffer. |
| `function` | [`write`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#write-36) `virtual` `const` | Writes the body (not the type or size) to the given buffer. |
| `function` | [`type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-17) `const` |  |
| `function` | [`size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#size-12) `const` |  |
| `function` | [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes) `const` `inline` |  |
| `function` | [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes) `const` `inline` |  |
| `function` | [`consumePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#consumepadding) `const` | Advances the reader past any 4-byte alignment padding that follows this attribute's body. |
| `function` | [`writePadding`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#writepadding) `const` | Writes zero-fill padding bytes to align this attribute to a 4-byte boundary. |
| `function` | [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring)  |  |
| `variable` | [`TypeID`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typeid) `static` `constexpr` |  |
| `function` | [`create`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#create-10) `static` | Creates an attribute of the given wire type and body size. Returns nullptr if the type is unknown or the size is invalid. |
| `function` | [`paddingBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddingbytes-1) `static` `inline` `constexpr` | Returns the 4-byte alignment padding required for a body of `size` bytes. |
| `function` | [`paddedBytes`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#paddedbytes-1) `static` `inline` `constexpr` | Returns the body length including 4-byte alignment padding. |
| `function` | [`typeString`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#typestring-1) `static` |  |
| `variable` | [`_type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_type-1)  |  |
| `variable` | [`_size`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#_size-3)  |  |
| `function` | [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute-2)  |  |
| `function` | [`setLength`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#setlength)  | Updates the stored body length. |
| `enum` | [`Type`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#type-18)  |  |

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`StringAttribute`](#stringattribute-1)  |  |
|  | [`StringAttribute`](#stringattribute-2)  | Copy constructor; duplicates stored bytes. |
|  | [`~StringAttribute`](#stringattribute-3) `virtual` |  |
| std::unique_ptr< [`Attribute`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#attribute) > | [`clone`](#clone-17) `virtual` `override` | Returns a deep copy of this attribute. |
| `const char *` | [`bytes`](#bytes-1) `const` `inline` |  |
| `void` | [`setBytes`](#setbytes)  | Replaces the stored bytes with a copy of the given buffer and updates the attribute's reported size. |
| `std::string` | [`asString`](#asstring) `const` |  |
| `void` | [`copyBytes`](#copybytes)  | Copies a null-terminated string into the attribute, using strlen to determine the length. |
| `void` | [`copyBytes`](#copybytes-1)  | Copies an arbitrary block of memory into the attribute. |
| `uint8_t` | [`getByte`](#getbyte) `const` | Returns a single byte from the stored buffer. |
| `void` | [`setByte`](#setbyte)  | Overwrites a single byte in the stored buffer. |
| `void` | [`read`](#read-12) `virtual` `override` | Reads the body (not the type or size) for this type of attribute from the given buffer. |
| `void` | [`write`](#write-41) `virtual` `const` `override` | Writes the body (not the type or size) to the given buffer. |

---

<a id="stringattribute-1"></a>

### StringAttribute

```cpp
StringAttribute(uint16_t type, uint16_t size = 0)
```

[Defined in src/stun/include/icy/stun/attributes.h:374](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L374)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `type` | `uint16_t` | Wire type code for the concrete attribute. |
| `size` | `uint16_t` | Initial body length in bytes (0 for variable-length attributes). |

---

<a id="stringattribute-2"></a>

### StringAttribute

```cpp
StringAttribute(const StringAttribute & r)
```

[Defined in src/stun/include/icy/stun/attributes.h:377](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L377)

Copy constructor; duplicates stored bytes.

---

<a id="stringattribute-3"></a>

### ~StringAttribute

`virtual`

```cpp
virtual ~StringAttribute()
```

[Defined in src/stun/include/icy/stun/attributes.h:378](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L378)

---

<a id="clone-17"></a>

### clone

`virtual` `override`

```cpp
virtual std::unique_ptr< Attribute > clone() override
```

[Defined in src/stun/include/icy/stun/attributes.h:381](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L381)

Returns a deep copy of this attribute.

#### Reimplements

- [`Attribute::clone`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#clone-12)

---

<a id="bytes-1"></a>

### bytes

`const` `inline`

```cpp
inline const char * bytes() const
```

[Defined in src/stun/include/icy/stun/attributes.h:384](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L384)

#### Returns
Pointer to the raw byte buffer.

---

<a id="setbytes"></a>

### setBytes

```cpp
void setBytes(const char * bytes, size_t size)
```

[Defined in src/stun/include/icy/stun/attributes.h:390](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L390)

Replaces the stored bytes with a copy of the given buffer and updates the attribute's reported size.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `bytes` | `const char *` | Source data pointer. |
| `size` | `size_t` | Number of bytes to copy. |

---

<a id="asstring"></a>

### asString

`const`

```cpp
std::string asString() const
```

[Defined in src/stun/include/icy/stun/attributes.h:393](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L393)

#### Returns
The stored bytes as a std::string.

---

<a id="copybytes"></a>

### copyBytes

```cpp
void copyBytes(const char * bytes)
```

[Defined in src/stun/include/icy/stun/attributes.h:398](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L398)

Copies a null-terminated string into the attribute, using strlen to determine the length.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `bytes` | `const char *` | Null-terminated source string. |

---

<a id="copybytes-1"></a>

### copyBytes

```cpp
void copyBytes(const void * bytes, size_t size)
```

[Defined in src/stun/include/icy/stun/attributes.h:403](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L403)

Copies an arbitrary block of memory into the attribute.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `bytes` | `const void *` | Source data pointer. |
| `size` | `size_t` | Number of bytes to copy. |

---

<a id="getbyte"></a>

### getByte

`const`

```cpp
uint8_t getByte(int index) const
```

[Defined in src/stun/include/icy/stun/attributes.h:408](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L408)

Returns a single byte from the stored buffer. 
#### Returns
The byte value at `index`.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `index` | `int` | Zero-based byte offset. |

---

<a id="setbyte"></a>

### setByte

```cpp
void setByte(int index, uint8_t value)
```

[Defined in src/stun/include/icy/stun/attributes.h:413](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L413)

Overwrites a single byte in the stored buffer.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `index` | `int` | Zero-based byte offset. |
| `value` | `uint8_t` | New value to write. |

---

<a id="read-12"></a>

### read

`virtual` `override`

```cpp
virtual void read(BitReader & reader) override
```

[Defined in src/stun/include/icy/stun/attributes.h:415](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L415)

Reads the body (not the type or size) for this type of attribute from the given buffer.

#### Reimplements

- [`Attribute::read`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#read-7)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `reader` | [`BitReader`](https://auscaster.com/icey/docs/api-reference/icy-BitReader.md#bitreader) & | Source bit reader positioned at the attribute body. |

---

<a id="write-41"></a>

### write

`virtual` `const` `override`

```cpp
virtual void write(BitWriter & writer) const override
```

[Defined in src/stun/include/icy/stun/attributes.h:416](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L416)

Writes the body (not the type or size) to the given buffer.

#### Reimplements

- [`Attribute::write`](https://auscaster.com/icey/docs/api-reference/icy-stun-Attribute.md#write-36)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `writer` | [`BitWriter`](https://auscaster.com/icey/docs/api-reference/icy-BitWriter.md#bitwriter) & | Destination bit writer. |

## Private Attributes

| Return | Name | Description |
|--------|------|-------------|
| `std::vector< char >` | [`_bytes`](#_bytes-2)  |  |

---

<a id="_bytes-2"></a>

### _bytes

```cpp
std::vector< char > _bytes
```

[Defined in src/stun/include/icy/stun/attributes.h:419](https://github.com/auscaster/icey/blob/main/src/stun/include/icy/stun/attributes.h#L419)
