blob: 41394d7c4cdbeb35d855cbd9d95c06d6091ecd3e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# ByteArray
The `ByteArray` module provides a number of utilities for converting between
[`GLib.Bytes`][gbytes] object, `String` values and `Uint8Array` objects.
It was originally based on an ECMAScript 4 proposal that was never adopted, but
now that ES6 has typed arrays, we use the standard `Uint8Array` to represent
byte arrays in GJS.
The primary use for most GJS users will be to exchange bytes between various C
APIs, like reading from an IO stream and then pushing the bytes into a parser.
Actually manipulating bytes in GJS is likely to be pretty slow and fortunately
rarely necessary. An advantage of the GJS and GObject-Introspection setup is
that most of the tasks best done in C, like messing with bytes, can be.
[gbytes]: https://gjs-docs.gnome.org/glib20/glib.bytes
#### Import
> Attention: This module is not available as an ECMAScript Module
The `ByteArray` module is available on the global `imports` object:
```js
const ByteArray = imports.byteArray;
```
### ByteArray.fromString(string, encoding)
> Deprecated: Use [`TextEncoder.encode()`][textencoder-encode] instead
Type:
* Static
Parameters:
* string (`String`) — A string to encode
* encoding (`String`) — Optional encoding of `string`
Returns:
* (`Uint8Array`) — A byte array
Convert a String into a newly constructed `Uint8Array`; this creates a
new `Uint8Array` of the same length as the String, then assigns each
`Uint8Array` entry the corresponding byte value of the String encoded
according to the given encoding (or UTF-8 if not given).
[textencoder-encode]: https://gjs-docs.gnome.org/gjs/encoding.md#textencoder-encode
### ByteArray.toString(byteArray, encoding)
> Deprecated: Use [`TextDecoder.decode()`][textdecoder-decode] instead
Type:
* Static
Parameters:
* byteArray (`Uint8Array`) — A byte array to decode
* encoding (`String`) — Optional encoding of `byteArray`
Returns:
* (`String`) — A string
Converts the `Uint8Array` into a literal string. The bytes are
interpreted according to the given encoding (or UTF-8 if not given).
The resulting string is guaranteed to round-trip back into an identical
ByteArray by passing the result to `ByteArray.fromString()`. In other words,
this check is guaranteed to pass:
```js
const original = ByteArray.fromString('foobar');
const copy = ByteArray.fromString(ByteArray.toString(original));
console.assert(original.every((value, index) => value === copy[index]));
```
[textdecoder-decode]: https://gjs-docs.gnome.org/gjs/encoding.md#textdecoder-decode
### ByteArray.fromGBytes(bytes)
> Deprecated: Use [`GLib.Bytes.toArray()`][gbytes-toarray] instead
Type:
* Static
Parameters:
* bytes (`GLib.Bytes`) — A [`GLib.Bytes`][gbytes] to convert
Returns:
* (`Uint8Array`) — A new byte array
Convert a [`GLib.Bytes`][gbytes] instance into a newly constructed `Uint8Array`.
The contents are copied.
[gbytes]: https://gjs-docs.gnome.org/glib20/glib.bytes
[gbytes-toarray]: https://gjs-docs.gnome.org/gjs/overrides.md#glib-bytes-toarray
### ByteArray.toGBytes(byteArray)
> Deprecated: Use [`new GLib.Bytes()`][gbytes] instead
Type:
* Static
Parameters:
* byteArray (`Uint8Array`) — A byte array to convert
Returns:
* (`GLib.Bytes`) — A new [`GLib.Bytes`][gbytes]
Converts the `Uint8Array` into a [`GLib.Bytes`][gbytes] instance.
The contents are copied.
[gbytes]: https://gjs-docs.gnome.org/glib20/glib.bytes
|