object-input-stream 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/classes.ts ADDED
@@ -0,0 +1,175 @@
1
+ import type { Serializable, SerializableCtor, Enum, ObjectInputStream, ExternalizableCtor } from '.';
2
+ import { InvalidObjectException } from './exceptions';
3
+
4
+ export namespace java {
5
+ export namespace lang {
6
+ export class Byte implements Serializable {
7
+ static readonly serialVersionUID: bigint = -7183698231559129828n;
8
+ value: number = 0;
9
+ readResolve() { return this.value; }
10
+ }
11
+ export class Short implements Serializable {
12
+ static readonly serialVersionUID: bigint = 7515723908773894738n;
13
+ value: number = 0;
14
+ readResolve() { return this.value; }
15
+ }
16
+ export class Integer implements Serializable {
17
+ static readonly serialVersionUID: bigint = 1360826667806852920n;
18
+ value: number = 0;
19
+ readResolve() { return this.value; }
20
+ }
21
+ export class Long implements Serializable {
22
+ static readonly serialVersionUID: bigint = 4290774380558885855n;
23
+ value: bigint = 0n;
24
+ readResolve() { return this.value; }
25
+ }
26
+ export class Float implements Serializable {
27
+ static readonly serialVersionUID: bigint = -2671257302660747028n;
28
+ value: number = 0;
29
+ readResolve() { return this.value; }
30
+ }
31
+ export class Double implements Serializable {
32
+ static readonly serialVersionUID: bigint = -9172774392245257468n;
33
+ value: number = 0;
34
+ readResolve() { return this.value; }
35
+ }
36
+ export class Character implements Serializable {
37
+ static readonly serialVersionUID: bigint = 3786198910865385080n;
38
+ value: string = '\0';
39
+ readResolve() { return this.value; }
40
+ }
41
+ export class Boolean implements Serializable {
42
+ static readonly serialVersionUID: bigint = -3665804199014368530n;
43
+ value: boolean = false;
44
+ readResolve() { return this.value; }
45
+ }
46
+ }
47
+
48
+ export namespace util {
49
+ export class ArrayList extends Array implements Serializable {
50
+ static readonly serialVersionUID: bigint = 8683452581122892189n;
51
+ readObject(ois: ObjectInputStream): void {
52
+ // Read size from fields
53
+ const size = ois.readFields().get("size");
54
+ if (typeof size !== "number" || size < 0)
55
+ throw new InvalidObjectException("Invalid size: " + size)
56
+
57
+ // Read and ignore capacity
58
+ ois.readInt();
59
+
60
+ // Read all items
61
+ for (let i=0; i<size; i++)
62
+ this.push(ois.readObject());
63
+ }
64
+ }
65
+
66
+ export class LinkedList extends Array implements Serializable {
67
+ static readonly serialVersionUID: bigint = 876323262645176354n;
68
+ readObject(ois: ObjectInputStream): void {
69
+ ois.readFields();
70
+
71
+ const size = ois.readInt();
72
+ for (let i=0; i<size; i++)
73
+ this.push(ois.readObject());
74
+ }
75
+ }
76
+
77
+ export class ArrayDeque extends Array implements Serializable {
78
+ static readonly serialVersionUID: bigint = 2340985798034038923n;
79
+ readObject(ois: ObjectInputStream): void {
80
+ ois.readFields();
81
+
82
+ const size = ois.readInt();
83
+ for (let i=0; i<size; i++)
84
+ this.push(ois.readObject());
85
+ }
86
+ }
87
+
88
+ export class HashSet extends Set implements Serializable {
89
+ static readonly serialVersionUID: bigint = -5024744406713321676n;
90
+ readObject(ois: ObjectInputStream): void {
91
+ ois.readFields(); // None
92
+ ois.readInt(); // Capacity
93
+ ois.readFloat(); // Load factor
94
+
95
+ const size = ois.readInt();
96
+ for (let i=0; i<size; i++)
97
+ this.add(ois.readObject());
98
+ }
99
+ }
100
+
101
+ export class LinkedHashSet extends HashSet {
102
+ static readonly serialVersionUID: bigint = -2851667679971038690n;
103
+ }
104
+
105
+ export class TreeSet extends Set implements Serializable {
106
+ static readonly serialVersionUID: bigint = -2479143000061671589n;
107
+ readObject(ois: ObjectInputStream): void {
108
+ ois.readFields();
109
+ ois.readObject(); // Comparator
110
+
111
+ const size = ois.readInt();
112
+ for (let i=0; i<size; i++)
113
+ this.add(ois.readObject());
114
+ }
115
+ }
116
+
117
+ export class HashMap extends Map implements Serializable {
118
+ static readonly serialVersionUID: bigint = 362498820763181265n;
119
+ readObject(ois: ObjectInputStream): void {
120
+ ois.readFields();
121
+ ois.readInt();
122
+
123
+ const size = ois.readInt();
124
+ for (let i=0; i<size; i++) {
125
+ const key = ois.readObject();
126
+ const val = ois.readObject();
127
+ this.set(key, val);
128
+ }
129
+ }
130
+ }
131
+
132
+ export class LinkedHashMap extends HashMap {
133
+ static readonly serialVersionUID: bigint = 3801124242820219131n;
134
+ }
135
+
136
+ export class TreeMap extends Map implements Serializable {
137
+ static readonly serialVersionUID: bigint = 919286545866124006n;
138
+ readObject(ois: ObjectInputStream): void {
139
+ ois.readFields();
140
+
141
+ const size = ois.readInt();
142
+ for (let i=0; i<size; i++) {
143
+ const key = ois.readObject();
144
+ const val = ois.readObject();
145
+ this.set(key, val);
146
+ }
147
+ }
148
+ }
149
+ }
150
+ }
151
+
152
+ export const builtinSerializables = new Map<string, SerializableCtor>([
153
+ ["java.lang.Byte", java.lang.Byte],
154
+ ["java.lang.Short", java.lang.Short],
155
+ ["java.lang.Integer", java.lang.Integer],
156
+ ["java.lang.Long", java.lang.Long],
157
+ ["java.lang.Float", java.lang.Float],
158
+ ["java.lang.Double", java.lang.Double],
159
+ ["java.lang.Character", java.lang.Character],
160
+ ["java.lang.Boolean", java.lang.Boolean],
161
+
162
+ ["java.util.ArrayList", java.util.ArrayList],
163
+ ["java.util.LinkedList", java.util.LinkedList],
164
+ ["java.util.ArrayDeque", java.util.ArrayDeque],
165
+ ["java.util.HashSet", java.util.HashSet],
166
+ ["java.util.LinkedHashSet", java.util.LinkedHashSet],
167
+ ["java.util.TreeSet", java.util.TreeSet],
168
+ ["java.util.HashMap", java.util.HashMap],
169
+ ["java.util.LinkedHashMap", java.util.LinkedHashMap],
170
+ ["java.util.TreeMap", java.util.TreeMap],
171
+ ]);
172
+
173
+ export const builtinExternalizables = new Map<string, ExternalizableCtor>([]);
174
+ export const builtinEnums = new Map<string, Enum>([]);
175
+ export const builtinClasses = new Map<string, any>([]);
@@ -0,0 +1,84 @@
1
+ export class JavaException extends Error {
2
+ name = "JavaException";
3
+ }
4
+ export class IOException extends JavaException {
5
+ name = "IOException";
6
+ }
7
+ export class ObjectStreamException extends IOException {
8
+ name = "ObjectStreamException";
9
+ }
10
+ export class StreamCorruptedException extends ObjectStreamException {
11
+ name = "StreamCorruptedException";
12
+ }
13
+ export class EOFException extends IOException {
14
+ name = "EOFException";
15
+ }
16
+ export class UTFDataFormatException extends IOException {
17
+ name = "UTFDataFormatException";
18
+ }
19
+ export class RuntimeException extends JavaException {
20
+ name = "RuntimeException";
21
+ }
22
+ export class IllegalStateException extends RuntimeException {
23
+ name = "IllegalStateException";
24
+ }
25
+ export class IndexOutOfBoundsException extends RuntimeException {
26
+ name = "IndexOutOfBoundsException";
27
+ }
28
+ export class ClassCastException extends RuntimeException {
29
+ name = "ClassCastException";
30
+ }
31
+ export class NullPointerException extends RuntimeException {
32
+ name = "NullPointerException";
33
+ }
34
+ export class OptionalDataException extends ObjectStreamException {
35
+ length: number;
36
+ eof: boolean;
37
+ constructor(detail: number | boolean) {
38
+ if (typeof detail === "number") {
39
+ super("inside block. remaining bytes " + detail);
40
+ this.length = detail;
41
+ this.eof = false;
42
+ } else if (typeof detail === "boolean") {
43
+ super("eof");
44
+ this.length = 0;
45
+ this.eof = detail;
46
+ } else {
47
+ throw new Error("unreachable");
48
+ }
49
+ this.name = "OptionalDataException";
50
+ }
51
+ }
52
+ export class InvalidClassException extends ObjectStreamException {
53
+ name = "InvalidClassException";
54
+ cname: string | null
55
+ constructor(cname: string | null, reason: string) {
56
+ super(cname ?? "<unnamed>: " + reason);
57
+ this.cname = cname;
58
+ }
59
+ }
60
+ export class ReflectiveOperationException extends JavaException {
61
+ name = "ReflectiveOperationException";
62
+ }
63
+ export class ClassNotFoundException extends ReflectiveOperationException {
64
+ name = "ClassNotFoundException";
65
+ }
66
+ export class NotActiveException extends ObjectStreamException {
67
+ name = "NotActiveException";
68
+ }
69
+ export class InvalidObjectException extends ObjectStreamException {
70
+ name = "InvalidObjectException";
71
+ }
72
+ export class WriteAbortedException extends ObjectStreamException {
73
+ name = "WriteAbortedException";
74
+ detail: any;
75
+ constructor(message: string, detail: any) {
76
+ super(message + ": " + detail);
77
+ this.detail = detail;
78
+ }
79
+ }
80
+ export class InternalError extends JavaException {
81
+ name = "InternalError";
82
+ }
83
+
84
+ export class NotImplementedError extends Error {} // TODO remove before publishing
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ import ObjectInputStream from './object-input-stream';
2
+ export default ObjectInputStream;
3
+
4
+ export {
5
+ ObjectInputStream,
6
+ OisOptions,
7
+ SerializableCtor,
8
+ Serializable,
9
+ ExternalizableCtor,
10
+ Enum,
11
+ Externalizable,
12
+ ObjectStreamClass,
13
+ FieldDesc,
14
+ } from './object-input-stream';
15
+
16
+ export * as exceptions from './exceptions';
17
+ export * as ast from './ast';
18
+ export * as classes from './classes';
19
+ export * as internal from './internal';
@@ -0,0 +1,10 @@
1
+ export {
2
+ ByteArray,
3
+ HandleTable,
4
+ InvocationHandler,
5
+ BaseProxy,
6
+ BaseFallbackClass,
7
+ BaseFallbackSerializable,
8
+ BaseFallbackExternalizable,
9
+ BaseFallbackEnum,
10
+ } from './object-input-stream';