---
product: Chilkat
product_version: 11.7.0
language: ckjava
language_label: Java
class: CkByteData
license: free
deprecated: false
reference_html: https://www.chilkatsoft.com/refdoc/javaCkByteDataRef.html
---
# Chilkat CkByteData — Java — v11.7.0

> Chilkat 11.7.0 · language=ckjava · class=CkByteData
> Other versions: replace `11.7.0` in this URL. Other languages: `https://www.chilkatsoft.com/ai-docs/v/11.7.0/<lang>/classes/CkByteData.md`.
> Language-wide rules (install, unlock, error model, strings/bytes): `https://www.chilkatsoft.com/ai-docs/v/11.7.0/ckjava/guide.md`.

## Purpose

Represents a chunk of byte data and provides methods for accessing it, adding to it, or saving and loading from files.

## Object creation, unlocking and error handling

**Object Creation**

```java
CkByteData obj = new CkByteData();
```

Call `Global.UnlockBundle` once at the start of the program, before using this or any other Chilkat class. Any string unlocks a fully functional 30-day trial; a purchased unlock code is used in production. See `guide.md` for the exact code in Java.

How failure is reported depends on the method's return type and is stated in the note under each method below. In every case the diagnostic text is in the `LastErrorText` property of the object whose method was called.

## Member index

| Member | Kind | Signature |
|---|---|---|
| `SecureClear` | property, read/write | `boolean get_SecureClear(); void put_SecureClear(boolean newVal);` |
| `appendChar` | method | `void appendChar(byte ch);` |
| `appendCharN` | method | `void appendCharN(byte ch, int numTimes);` |
| `appendEncoded` | method | `void appendEncoded(String str, String encoding);` |
| `appendFile` | method | `boolean appendFile(String path);` |
| `appendInt` | method | `void appendInt(int intValue, boolean littleEndian);` |
| `appendRandom` | method | `void appendRandom(int numBytes);` |
| `appendShort` | method | `void appendShort(short shortValue, boolean littleEndian);` |
| `appendStr` | method | `void appendStr(String str);` |
| `byteSwap4321` | method | `void byteSwap4321();` |
| `clear` | method | `void clear();` |
| `dropData` | method | `void dropData();` |
| `encode` | method | `void encode(String encoding, CkString str);` |
| `ensureBuffer` | method | `boolean ensureBuffer(int expectedNumBytes);` |
| `getByte` | method | `byte getByte(int byteIndex);` |
| `getChar` | method | `byte getChar(int byteIndex);` |
| `getData` | method | `byte[] getData();` |
| `getDataAt` | method | `byte[] getDataAt(int byteIndex);` |
| `getInt` | method | `int getInt(int byteIndex);` |
| `getRange` | method | `byte[] getRange(int byteIndex, int numBytes);` |
| `getSize` | method | `int getSize();` |
| `is7bit` | method | `boolean is7bit();` |
| `loadFile` | method | `boolean loadFile(String path);` |
| `pad` | method | `void pad(int blockSize, int paddingScheme);` |
| `removeChunk` | method | `void removeChunk(int startIndex, int numBytes);` |
| `replaceChar` | method | `void replaceChar(byte existingByteValue, byte replacementByteValue);` |
| `saveFile` | method | `boolean saveFile(String path);` |
| `shorten` | method | `void shorten(int numBytes);` |
| `to_ws` | method | `byte[] to_ws(String charset);` |
| `unpad` | method | `void unpad(int blockSize, int paddingScheme);` |

Deprecated members are listed at the end of this file with their replacements; do not use them in new code.

## Properties

### CkByteData.SecureClear

`Chilkat 11.7.0 · ckjava · CkByteData · SecureClear · property · read/write · since 9.5.0.73`

```java
boolean get_SecureClear();

void put_SecureClear(boolean newVal);
```

If set to `true`, then memory is always first overwritten with zero bytes prior to deallocation. The default value of this property is `false`.

## Methods

### CkByteData.appendChar

`Chilkat 11.7.0 · ckjava · CkByteData · appendChar · method`

```java
void appendChar(byte ch);
```

Appends a single byte.

### CkByteData.appendCharN

`Chilkat 11.7.0 · ckjava · CkByteData · appendCharN · method`

```java
void appendCharN(byte ch, int numTimes);
```

Appends a single char N times. The equivalent of calling appendChar N times.

### CkByteData.appendEncoded

`Chilkat 11.7.0 · ckjava · CkByteData · appendEncoded · method`

```java
void appendEncoded(String str, String encoding);
```

Appends binary data from an encoded string. The encoding can be specified as `hex`, `base64`, `url`, `quoted-printable`, `modBase64`, `base58`, or `base32`. The input string is decoded from the specified encoding and the binary data is appended to the calling object's content.

### CkByteData.appendFile

`Chilkat 11.7.0 · ckjava · CkByteData · appendFile · method`

```java
boolean appendFile(String path);
```

Opens a file for binary read, appends the file contents, and closes the file.

### CkByteData.appendInt

`Chilkat 11.7.0 · ckjava · CkByteData · appendInt · method`

```java
void appendInt(int intValue, boolean littleEndian);
```

Appends a 32-bit signed integer (4 bytes) to the data already contained in the object. `littleEndian` determines whether the big endian or little endian byte ordering is used.

### CkByteData.appendRandom

`Chilkat 11.7.0 · ckjava · CkByteData · appendRandom · method`

```java
void appendRandom(int numBytes);
```

Appends `numBytes` random bytes to the data already contained within the object.

### CkByteData.appendShort

`Chilkat 11.7.0 · ckjava · CkByteData · appendShort · method`

```java
void appendShort(short shortValue, boolean littleEndian);
```

Appends a 16-bit signed integer (2 bytes) to the data already contained in the object. `littleEndian` determines whether the big endian or little endian byte ordering is used.

### CkByteData.appendStr

`Chilkat 11.7.0 · ckjava · CkByteData · appendStr · method`

```java
void appendStr(String str);
```

Appends a null-terminated string to the data, without including the terminating null.

### CkByteData.byteSwap4321

`Chilkat 11.7.0 · ckjava · CkByteData · byteSwap4321 · method`

```java
void byteSwap4321();
```

4321 byte swaps the data contained within the object.

### CkByteData.clear

`Chilkat 11.7.0 · ckjava · CkByteData · clear · method`

```java
void clear();
```

Clears the CkByteData object of all data. The internal memory is deallocated. To clear without deallocating, call dropData instead.

### CkByteData.dropData

`Chilkat 11.7.0 · ckjava · CkByteData · dropData · method · since 9.5.0.91`

```java
void dropData();
```

Sets the size of the data to 0. Does not deallocate the existing internal buffer. (This is a fast way of `clearing` the CkByteData object, such that the existing data remains in memory and is overwritten on the next append.)

If the SecureClear property is set to `true`, then the internal memory buffer is zeroed out before resetting the size to 0.

### CkByteData.encode

`Chilkat 11.7.0 · ckjava · CkByteData · encode · method`

```java
void encode(String encoding, CkString str);
```

Encodes binary data according to the encoding requested. The encoding can be specified as `hex`, `base64`, `url`, `quoted-printable`, `modBase64`, `base58`, `base32`, `qp-`, `url_rfc1738`, `url_rfc2396`, or `url_rfc3986`.

### CkByteData.ensureBuffer

`Chilkat 11.7.0 · ckjava · CkByteData · ensureBuffer · method`

```java
boolean ensureBuffer(int expectedNumBytes);
```

This method can be called to help optimize internal memory re-allocation. If, for example, many calls will be made to append data, and the total size is approximately known, then this method can be called to pre-allocate the internal buffer to the expected total size.

If the internal buffer is already larger than the `expectedNumBytes` then nothing happens. The existing internal buffer is kept.

Returns true for success, false for failure.

### CkByteData.getByte

`Chilkat 11.7.0 · ckjava · CkByteData · getByte · method`

```java
byte getByte(int byteIndex);
```

Returns the Nth byte of the binary data. The 1st byte is at index 0.

### CkByteData.getChar

`Chilkat 11.7.0 · ckjava · CkByteData · getChar · method`

```java
byte getChar(int byteIndex);
```

Returns the Nth byte of the binary content as a `char`. The 1st byte is at index 0.

### CkByteData.getData

`Chilkat 11.7.0 · ckjava · CkByteData · getData · method`

```java
byte[] getData();
```

Returns a pointer to the internal buffer. Be careful with this method because if additional data is appended, the data within the object may be relocated and the pointer may cease to be valid.

### CkByteData.getDataAt

`Chilkat 11.7.0 · ckjava · CkByteData · getDataAt · method`

```java
byte[] getDataAt(int byteIndex);
```

Same as getData, except it gets a pointer to the data at a byte offset. (0 = the start of buffer)

### CkByteData.getInt

`Chilkat 11.7.0 · ckjava · CkByteData · getInt · method`

```java
int getInt(int byteIndex);
```

Returns the 4-byte integer located at a specific byte index.

### CkByteData.getRange

`Chilkat 11.7.0 · ckjava · CkByteData · getRange · method`

```java
byte[] getRange(int byteIndex, int numBytes);
```

Copies a range of bytes to a separate internal memory buffer and returns the pointer to the bytes. The returned pointer is only valid while the object exists. Also, any subsequent calls to getRange, getRangeStr, or to_s will invalidate the buffer.

### CkByteData.getSize

`Chilkat 11.7.0 · ckjava · CkByteData · getSize · method`

```java
int getSize();
```

Returns the number of bytes in the data buffer.

### CkByteData.is7bit

`Chilkat 11.7.0 · ckjava · CkByteData · is7bit · method`

```java
boolean is7bit();
```

Returns `true` if all the bytes are in the range 0x00 to 0x7F.

### CkByteData.loadFile

`Chilkat 11.7.0 · ckjava · CkByteData · loadFile · method`

```java
boolean loadFile(String path);
```

Equivalent to clear() followed by appendFile().

### CkByteData.pad

`Chilkat 11.7.0 · ckjava · CkByteData · pad · method`

```java
void pad(int blockSize, int paddingScheme);
```

Pads the data to a multiple of the `blockSize` using a cryptographic padding scheme specified by `paddingScheme`. The possible integer values for `paddingScheme` are the same as those listed for the PaddingScheme property of the CkCrypt2 class.

### CkByteData.removeChunk

`Chilkat 11.7.0 · ckjava · CkByteData · removeChunk · method`

```java
void removeChunk(int startIndex, int numBytes);
```

Removes (discards) a range from the data.

### CkByteData.replaceChar

`Chilkat 11.7.0 · ckjava · CkByteData · replaceChar · method`

```java
void replaceChar(byte existingByteValue, byte replacementByteValue);
```

Replaces all occurrences of `existingByteValue` with `replacementByteValue`.

### CkByteData.saveFile

`Chilkat 11.7.0 · ckjava · CkByteData · saveFile · method`

```java
boolean saveFile(String path);
```

Saves the byte data to a file. If the file already exists, it will be overwritten.

### CkByteData.shorten

`Chilkat 11.7.0 · ckjava · CkByteData · shorten · method`

```java
void shorten(int numBytes);
```

Discards N bytes from the end of the data.

### CkByteData.to_ws

`Chilkat 11.7.0 · ckjava · CkByteData · to_ws · method`

```java
byte[] to_ws(String charset);
```

To be documented soon...

### CkByteData.unpad

`Chilkat 11.7.0 · ckjava · CkByteData · unpad · method`

```java
void unpad(int blockSize, int paddingScheme);
```

Unpads the data from a multiple of the `blockSize` to the original data size using a cryptographic padding scheme specified by `paddingScheme`. The possible integer values for `paddingScheme` are the same as those listed for the PaddingScheme property of the CkCrypt2 class.

## Deprecated members — do not use in new code

| Member | Kind | Deprecated in | Use instead |
|---|---|---|---|
| `append` | method | before 11.6.1 | — |
| `appendRange` | method | before 11.6.1 | — |
| `beginsWith` | method | before 11.6.1 | — |
| `equals` | method | before 11.6.1 | — |
| `findBytes` | method | before 11.6.1 | — |
| `getBytes` | method | before 11.6.1 | — |
| `preAllocate` | method | before 11.6.1 | — |

## Related classes

[`CkString`](https://www.chilkatsoft.com/ai-docs/v/11.7.0/ckjava/classes/CkString.md)

