• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wuwen5 / hessian / 17177490298

23 Aug 2025 03:42PM UTC coverage: 68.416% (+11.8%) from 56.631%
17177490298

push

github

web-flow
refactor: fix sonar S2184 and code clean (#29)

1781 of 2793 branches covered (63.77%)

Branch coverage included in aggregate %.

4152 of 5879 relevant lines covered (70.62%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.12
hessian2-codec/src/main/java/io/github/wuwen5/hessian/io/BasicDeserializer.java
1
/*
2
 * Copyright (c) 2001-2008 Caucho Technology, Inc.  All rights reserved.
3
 *
4
 * The Apache Software License, Version 1.1
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in
15
 *    the documentation and/or other materials provided with the
16
 *    distribution.
17
 *
18
 * 3. The end-user documentation included with the redistribution, if
19
 *    any, must include the following acknowlegement:
20
 *       "This product includes software developed by the
21
 *        Caucho Technology (http://www.caucho.com/)."
22
 *    Alternately, this acknowlegement may appear in the software itself,
23
 *    if and wherever such third-party acknowlegements normally appear.
24
 *
25
 * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
26
 *    endorse or promote products derived from this software without prior
27
 *    written permission. For written permission, please contact
28
 *    info@caucho.com.
29
 *
30
 * 5. Products derived from this software may not be called "Resin"
31
 *    nor may "Resin" appear in their names without prior written
32
 *    permission of Caucho Technology.
33
 *
34
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37
 * DISCLAIMED.  IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
 *
46
 * @author Scott Ferguson
47
 */
48

49
package io.github.wuwen5.hessian.io;
50

51
import java.io.IOException;
52
import java.util.ArrayList;
53
import java.util.Date;
54
import java.util.List;
55

56
/**
57
 * Serializing an object for known object types.
58
 */
59
public class BasicDeserializer extends BaseDeserializer {
60
    public static final int NULL = BasicSerializer.NULL;
61
    public static final int BOOLEAN = BasicSerializer.BOOLEAN;
62
    public static final int BYTE = BasicSerializer.BYTE;
63
    public static final int SHORT = BasicSerializer.SHORT;
64
    public static final int INTEGER = BasicSerializer.INTEGER;
65
    public static final int LONG = BasicSerializer.LONG;
66
    public static final int FLOAT = BasicSerializer.FLOAT;
67
    public static final int DOUBLE = BasicSerializer.DOUBLE;
68
    public static final int CHARACTER = BasicSerializer.CHARACTER;
69
    public static final int CHARACTER_OBJECT = BasicSerializer.CHARACTER_OBJECT;
70
    public static final int STRING = BasicSerializer.STRING;
71
    public static final int DATE = BasicSerializer.DATE;
72
    public static final int NUMBER = BasicSerializer.NUMBER;
73
    public static final int OBJECT = BasicSerializer.OBJECT;
74

75
    public static final int BOOLEAN_ARRAY = BasicSerializer.BOOLEAN_ARRAY;
76
    public static final int BYTE_ARRAY = BasicSerializer.BYTE_ARRAY;
77
    public static final int SHORT_ARRAY = BasicSerializer.SHORT_ARRAY;
78
    public static final int INTEGER_ARRAY = BasicSerializer.INTEGER_ARRAY;
79
    public static final int LONG_ARRAY = BasicSerializer.LONG_ARRAY;
80
    public static final int FLOAT_ARRAY = BasicSerializer.FLOAT_ARRAY;
81
    public static final int DOUBLE_ARRAY = BasicSerializer.DOUBLE_ARRAY;
82
    public static final int CHARACTER_ARRAY = BasicSerializer.CHARACTER_ARRAY;
83
    public static final int STRING_ARRAY = BasicSerializer.STRING_ARRAY;
84
    public static final int OBJECT_ARRAY = BasicSerializer.OBJECT_ARRAY;
85

86
    private final int code;
87

88
    public BasicDeserializer(int code) {
2✔
89
        this.code = code;
3✔
90
    }
1✔
91

92
    @Override
93
    public Class<?> getType() {
94
        switch (code) {
3!
95
            case NULL:
96
                return void.class;
×
97
            case BOOLEAN:
98
                return Boolean.class;
2✔
99
            case BYTE:
100
                return Byte.class;
×
101
            case SHORT:
102
                return Short.class;
×
103
            case INTEGER:
104
                return Integer.class;
×
105
            case LONG:
106
                return Long.class;
×
107
            case FLOAT:
108
                return Float.class;
×
109
            case DOUBLE:
110
                return Double.class;
×
111
            case CHARACTER:
112
            case CHARACTER_OBJECT:
113
                return Character.class;
2✔
114
            case STRING:
115
                return String.class;
×
116
            case DATE:
117
                return Date.class;
×
118
            case NUMBER:
119
                return Number.class;
×
120
            case OBJECT:
121
                return Object.class;
×
122

123
            case BOOLEAN_ARRAY:
124
                return boolean[].class;
2✔
125
            case BYTE_ARRAY:
126
                return byte[].class;
×
127
            case SHORT_ARRAY:
128
                return short[].class;
2✔
129
            case INTEGER_ARRAY:
130
                return int[].class;
2✔
131
            case LONG_ARRAY:
132
                return long[].class;
2✔
133
            case FLOAT_ARRAY:
134
                return float[].class;
2✔
135
            case DOUBLE_ARRAY:
136
                return double[].class;
2✔
137
            case CHARACTER_ARRAY:
138
                return char[].class;
×
139
            case STRING_ARRAY:
140
                return String[].class;
2✔
141
            case OBJECT_ARRAY:
142
                return Object[].class;
×
143
            default:
144
                throw new UnsupportedOperationException();
×
145
        }
146
    }
147

148
    @Override
149
    public Object readObject(AbstractHessianDecoder in) throws IOException {
150
        switch (code) {
3!
151
            case NULL:
152
                // hessian/3490
153
                in.readObject();
×
154

155
                return null;
×
156

157
            case BOOLEAN:
158
                return Boolean.valueOf(in.readBoolean());
4✔
159

160
            case BYTE:
161
                return Byte.valueOf((byte) in.readInt());
5✔
162

163
            case SHORT:
164
                return Short.valueOf((short) in.readInt());
5✔
165

166
            case INTEGER:
167
                return Integer.valueOf(in.readInt());
4✔
168

169
            case LONG:
170
                return Long.valueOf(in.readLong());
4✔
171

172
            case FLOAT:
173
                return Float.valueOf((float) in.readDouble());
5✔
174

175
            case DOUBLE:
176
                return Double.valueOf(in.readDouble());
4✔
177

178
            case STRING:
179
                return in.readString();
3✔
180

181
            case OBJECT:
182
            case NUMBER:
183
                return in.readObject();
3✔
184

185
            case CHARACTER: {
186
                String s = in.readString();
3✔
187
                if (s == null || s.isEmpty()) {
5!
188
                    return Character.valueOf((char) 0);
×
189
                } else {
190
                    return Character.valueOf(s.charAt(0));
5✔
191
                }
192
            }
193

194
            case CHARACTER_OBJECT: {
195
                String s = in.readString();
3✔
196
                if (s == null || s.isEmpty()) {
5!
197
                    return null;
×
198
                } else {
199
                    return Character.valueOf(s.charAt(0));
5✔
200
                }
201
            }
202
            case DATE:
203
                return new Date(in.readUTCDate());
6✔
204
            case BYTE_ARRAY:
205
                return in.readBytes();
3✔
206
            case CHARACTER_ARRAY: {
207
                String s = in.readString();
3✔
208

209
                if (s == null) {
2!
210
                    return null;
×
211
                } else {
212
                    int len = s.length();
3✔
213
                    char[] chars = new char[len];
3✔
214
                    s.getChars(0, len, chars, 0);
6✔
215
                    return chars;
2✔
216
                }
217
            }
218

219
            case BOOLEAN_ARRAY:
220
            case SHORT_ARRAY:
221
            case INTEGER_ARRAY:
222
            case LONG_ARRAY:
223
            case FLOAT_ARRAY:
224
            case DOUBLE_ARRAY:
225
            case STRING_ARRAY: {
226
                int i = in.readListStart();
×
227

228
                switch (i) {
×
229
                    case 'N':
230
                        return null;
×
231

232
                    case 0x10:
233
                    case 0x11:
234
                    case 0x12:
235
                    case 0x13:
236
                    case 0x14:
237
                    case 0x15:
238
                    case 0x16:
239
                    case 0x17:
240
                    case 0x18:
241
                    case 0x19:
242
                    case 0x1a:
243
                    case 0x1b:
244
                    case 0x1c:
245
                    case 0x1d:
246
                    case 0x1e:
247
                    case 0x1f:
248
                        int length = i - 0x10;
×
249
                        in.readInt();
×
250

251
                        return readLengthList(in, length);
×
252

253
                    default:
254
                        in.readType();
×
255
                        length = in.readLength();
×
256

257
                        return readList(in, length);
×
258
                }
259
            }
260

261
            default:
262
                throw new UnsupportedOperationException();
×
263
        }
264
    }
265

266
    @Override
267
    public Object readList(AbstractHessianDecoder in, int length) throws IOException {
268
        switch (code) {
×
269
            case BOOLEAN_ARRAY: {
270
                if (length >= 0) {
×
271
                    boolean[] data = new boolean[length];
×
272

273
                    in.addRef(data);
×
274

275
                    for (int i = 0; i < data.length; i++) {
×
276
                        data[i] = in.readBoolean();
×
277
                    }
278

279
                    in.readEnd();
×
280

281
                    return data;
×
282
                } else {
283
                    List<Boolean> list = new ArrayList<>();
×
284

285
                    while (!in.isEnd()) {
×
286
                        list.add(Boolean.valueOf(in.readBoolean()));
×
287
                    }
288

289
                    in.readEnd();
×
290

291
                    boolean[] data = new boolean[list.size()];
×
292

293
                    in.addRef(data);
×
294

295
                    for (int i = 0; i < data.length; i++) {
×
296
                        data[i] = list.get(i);
×
297
                    }
298

299
                    return data;
×
300
                }
301
            }
302

303
            case SHORT_ARRAY: {
304
                if (length >= 0) {
×
305
                    short[] data = new short[length];
×
306

307
                    in.addRef(data);
×
308

309
                    for (int i = 0; i < data.length; i++) {
×
310
                        data[i] = (short) in.readInt();
×
311
                    }
312

313
                    in.readEnd();
×
314

315
                    return data;
×
316
                } else {
317
                    List<Short> list = new ArrayList<>();
×
318

319
                    while (!in.isEnd()) {
×
320
                        list.add(Short.valueOf((short) in.readInt()));
×
321
                    }
322

323
                    in.readEnd();
×
324

325
                    short[] data = new short[list.size()];
×
326
                    for (int i = 0; i < data.length; i++) {
×
327
                        data[i] = list.get(i).shortValue();
×
328
                    }
329

330
                    in.addRef(data);
×
331

332
                    return data;
×
333
                }
334
            }
335

336
            case INTEGER_ARRAY: {
337
                if (length >= 0) {
×
338
                    int[] data = new int[length];
×
339

340
                    in.addRef(data);
×
341

342
                    for (int i = 0; i < data.length; i++) {
×
343
                        data[i] = in.readInt();
×
344
                    }
345

346
                    in.readEnd();
×
347

348
                    return data;
×
349
                } else {
350
                    List<Integer> list = new ArrayList<>();
×
351

352
                    while (!in.isEnd()) {
×
353
                        list.add(Integer.valueOf(in.readInt()));
×
354
                    }
355

356
                    in.readEnd();
×
357

358
                    int[] data = new int[list.size()];
×
359
                    for (int i = 0; i < data.length; i++) {
×
360
                        data[i] = list.get(i).intValue();
×
361
                    }
362

363
                    in.addRef(data);
×
364

365
                    return data;
×
366
                }
367
            }
368

369
            case LONG_ARRAY: {
370
                if (length >= 0) {
×
371
                    long[] data = new long[length];
×
372

373
                    in.addRef(data);
×
374

375
                    for (int i = 0; i < data.length; i++) {
×
376
                        data[i] = in.readLong();
×
377
                    }
378

379
                    in.readEnd();
×
380

381
                    return data;
×
382
                } else {
383
                    List<Long> list = new ArrayList<>();
×
384

385
                    while (!in.isEnd()) {
×
386
                        list.add(in.readLong());
×
387
                    }
388

389
                    in.readEnd();
×
390

391
                    long[] data = new long[list.size()];
×
392
                    for (int i = 0; i < data.length; i++) {
×
393
                        data[i] = list.get(i);
×
394
                    }
395

396
                    in.addRef(data);
×
397

398
                    return data;
×
399
                }
400
            }
401

402
            case FLOAT_ARRAY: {
403
                if (length >= 0) {
×
404
                    float[] data = new float[length];
×
405
                    in.addRef(data);
×
406

407
                    for (int i = 0; i < data.length; i++) {
×
408
                        data[i] = (float) in.readDouble();
×
409
                    }
410

411
                    in.readEnd();
×
412

413
                    return data;
×
414
                } else {
415
                    List<Float> list = new ArrayList<>();
×
416

417
                    while (!in.isEnd()) {
×
418
                        list.add((float) in.readDouble());
×
419
                    }
420

421
                    in.readEnd();
×
422

423
                    float[] data = new float[list.size()];
×
424
                    for (int i = 0; i < data.length; i++) {
×
425
                        data[i] = list.get(i);
×
426
                    }
427

428
                    in.addRef(data);
×
429

430
                    return data;
×
431
                }
432
            }
433

434
            case DOUBLE_ARRAY: {
435
                if (length >= 0) {
×
436
                    double[] data = new double[length];
×
437
                    in.addRef(data);
×
438

439
                    for (int i = 0; i < data.length; i++) {
×
440
                        data[i] = in.readDouble();
×
441
                    }
442

443
                    in.readEnd();
×
444

445
                    return data;
×
446
                } else {
447
                    List<Double> list = new ArrayList<>();
×
448

449
                    while (!in.isEnd()) {
×
450
                        list.add(in.readDouble());
×
451
                    }
452

453
                    in.readEnd();
×
454

455
                    double[] data = new double[list.size()];
×
456
                    in.addRef(data);
×
457
                    for (int i = 0; i < data.length; i++) {
×
458
                        data[i] = list.get(i);
×
459
                    }
460

461
                    return data;
×
462
                }
463
            }
464

465
            case STRING_ARRAY: {
466
                if (length >= 0) {
×
467
                    String[] data = new String[length];
×
468
                    in.addRef(data);
×
469

470
                    for (int i = 0; i < data.length; i++) {
×
471
                        data[i] = in.readString();
×
472
                    }
473

474
                    in.readEnd();
×
475

476
                    return data;
×
477
                } else {
478
                    List<String> list = new ArrayList<>();
×
479

480
                    while (!in.isEnd()) {
×
481
                        list.add(in.readString());
×
482
                    }
483

484
                    in.readEnd();
×
485

486
                    String[] data = new String[list.size()];
×
487
                    in.addRef(data);
×
488
                    for (int i = 0; i < data.length; i++) {
×
489
                        data[i] = list.get(i);
×
490
                    }
491

492
                    return data;
×
493
                }
494
            }
495

496
            case OBJECT_ARRAY: {
497
                if (length >= 0) {
×
498
                    Object[] data = new Object[length];
×
499
                    in.addRef(data);
×
500

501
                    for (int i = 0; i < data.length; i++) {
×
502
                        data[i] = in.readObject();
×
503
                    }
504

505
                    in.readEnd();
×
506

507
                    return data;
×
508
                } else {
509
                    List<Object> list = new ArrayList<>();
×
510

511
                    // XXX: potential issues here
512
                    in.addRef(list);
×
513

514
                    while (!in.isEnd()) {
×
515
                        list.add(in.readObject());
×
516
                    }
517

518
                    in.readEnd();
×
519

520
                    Object[] data = new Object[list.size()];
×
521
                    for (int i = 0; i < data.length; i++) {
×
522
                        data[i] = list.get(i);
×
523
                    }
524

525
                    return data;
×
526
                }
527
            }
528

529
            default:
530
                throw new UnsupportedOperationException(String.valueOf(this));
×
531
        }
532
    }
533

534
    @Override
535
    public Object readLengthList(AbstractHessianDecoder in, int length) throws IOException {
536
        switch (code) {
3!
537
            case BOOLEAN_ARRAY: {
538
                boolean[] data = new boolean[length];
3✔
539

540
                in.addRef(data);
4✔
541

542
                for (int i = 0; i < data.length; i++) {
8✔
543
                    data[i] = in.readBoolean();
5✔
544
                }
545

546
                return data;
2✔
547
            }
548

549
            case SHORT_ARRAY: {
550
                short[] data = new short[length];
3✔
551

552
                in.addRef(data);
4✔
553

554
                for (int i = 0; i < data.length; i++) {
8✔
555
                    data[i] = (short) in.readInt();
6✔
556
                }
557

558
                return data;
2✔
559
            }
560

561
            case INTEGER_ARRAY: {
562
                int[] data = new int[length];
3✔
563

564
                in.addRef(data);
4✔
565

566
                for (int i = 0; i < data.length; i++) {
8✔
567
                    data[i] = in.readInt();
5✔
568
                }
569

570
                return data;
2✔
571
            }
572

573
            case LONG_ARRAY: {
574
                long[] data = new long[length];
3✔
575

576
                in.addRef(data);
4✔
577

578
                for (int i = 0; i < data.length; i++) {
8✔
579
                    data[i] = in.readLong();
5✔
580
                }
581

582
                return data;
2✔
583
            }
584

585
            case FLOAT_ARRAY: {
586
                float[] data = new float[length];
3✔
587
                in.addRef(data);
4✔
588

589
                for (int i = 0; i < data.length; i++) {
8✔
590
                    data[i] = (float) in.readDouble();
6✔
591
                }
592

593
                return data;
2✔
594
            }
595

596
            case DOUBLE_ARRAY: {
597
                double[] data = new double[length];
3✔
598
                in.addRef(data);
4✔
599

600
                for (int i = 0; i < data.length; i++) {
8✔
601
                    data[i] = in.readDouble();
5✔
602
                }
603

604
                return data;
2✔
605
            }
606

607
            case STRING_ARRAY: {
608
                String[] data = new String[length];
3✔
609
                in.addRef(data);
4✔
610

611
                for (int i = 0; i < data.length; i++) {
8✔
612
                    data[i] = in.readString();
5✔
613
                }
614

615
                return data;
2✔
616
            }
617

618
            case OBJECT_ARRAY: {
619
                Object[] data = new Object[length];
×
620
                in.addRef(data);
×
621

622
                for (int i = 0; i < data.length; i++) {
×
623
                    data[i] = in.readObject();
×
624
                }
625

626
                return data;
×
627
            }
628

629
            default:
630
                throw new UnsupportedOperationException(String.valueOf(this));
×
631
        }
632
    }
633

634
    @Override
635
    public String toString() {
636
        return getClass().getSimpleName() + "[" + code + "]";
×
637
    }
638
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc