forked from amazon-ion/ion-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteArrayBytecodeGenerator11.kt
More file actions
100 lines (87 loc) · 3.72 KB
/
ByteArrayBytecodeGenerator11.kt
File metadata and controls
100 lines (87 loc) · 3.72 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.bytecode.bin11
import com.amazon.ion.Decimal
import com.amazon.ion.IonException
import com.amazon.ion.Timestamp
import com.amazon.ion.bytecode.BytecodeEmitter
import com.amazon.ion.bytecode.BytecodeGenerator
import com.amazon.ion.bytecode.bin11.bytearray.OpcodeHandlerTable
import com.amazon.ion.bytecode.bin11.bytearray.PrimitiveDecoder.readFixedIntAsBigInteger
import com.amazon.ion.bytecode.bin11.bytearray.ShortTimestampDecoder
import com.amazon.ion.bytecode.util.AppendableConstantPoolView
import com.amazon.ion.bytecode.util.ByteSlice
import com.amazon.ion.bytecode.util.BytecodeBuffer
import com.amazon.ion.bytecode.util.unsignedToInt
import com.amazon.ion.impl.bin.utf8.Utf8StringDecoder
import com.amazon.ion.impl.bin.utf8.Utf8StringDecoderPool
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import java.math.BigInteger
import java.nio.ByteBuffer
@SuppressFBWarnings("EI_EXPOSE_REP2", justification = "constructor does not make a defensive copy of source as a performance optimization")
internal class ByteArrayBytecodeGenerator11(
private val source: ByteArray,
private var currentPosition: Int,
) : BytecodeGenerator {
private val utf8Decoder: Utf8StringDecoder = Utf8StringDecoderPool.getInstance().orCreate
override fun refill(
destination: BytecodeBuffer,
constantPool: AppendableConstantPoolView,
macroSrc: IntArray,
macroIndices: IntArray,
symTab: Array<String?>
) {
var opcode = 0
while (currentPosition < source.size && !isSystemValue(opcode)) {
opcode = source[currentPosition++].unsignedToInt()
val handler = OpcodeHandlerTable.handler(opcode)
currentPosition += handler.convertOpcodeToBytecode(
opcode,
source,
currentPosition,
destination,
constantPool,
macroSrc,
macroIndices,
symTab
)
}
if (currentPosition < source.size) {
BytecodeEmitter.emitRefill(destination)
} else {
BytecodeEmitter.emitEndOfInput(destination)
}
}
override fun readBigIntegerReference(position: Int, length: Int): BigInteger {
return readFixedIntAsBigInteger(source, position, length)
}
override fun readDecimalReference(position: Int, length: Int): Decimal {
TODO("Not yet implemented")
}
override fun readShortTimestampReference(position: Int, opcode: Int): Timestamp {
return ShortTimestampDecoder.readTimestamp(source, position, opcode)
}
override fun readTimestampReference(position: Int, length: Int): Timestamp {
TODO("Not yet implemented")
}
override fun readTextReference(position: Int, length: Int): String {
val buffer = ByteBuffer.wrap(source, position, length)
return utf8Decoder.decode(buffer, length)
}
override fun readBytesReference(position: Int, length: Int): ByteSlice {
return ByteSlice(source, position, position + length)
}
override fun ionMinorVersion(): Int {
return 1
}
override fun getGeneratorForMinorVersion(minorVersion: Int): BytecodeGenerator {
return when (minorVersion) {
1 -> ByteArrayBytecodeGenerator11(source, currentPosition)
// TODO: update with ByteArrayBytecodeGenerator10 once it implements BytecodeGenerator
else -> throw IonException("Minor version $minorVersion not yet implemented for ByteArray-backed data sources.")
}
}
private fun isSystemValue(opcode: Int): Boolean {
return opcode in 0xE0..0xE8
}
}