|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.jfr.internal.consumer; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.nio.charset.Charset; |
|
30 |
|
31 import jdk.jfr.internal.consumer.Parser; |
|
32 import jdk.jfr.internal.consumer.RecordingInput; |
|
33 import jdk.jfr.internal.consumer.StringEncoding; |
|
34 |
|
35 public final class StringParser extends Parser { |
|
36 private final static Charset UTF8 = Charset.forName("UTF-8"); |
|
37 private final static Charset LATIN1 = Charset.forName("ISO-8859-1"); |
|
38 |
|
39 final static class CharsetParser extends Parser { |
|
40 private final Charset charset; |
|
41 private int lastSize; |
|
42 private byte[] buffer = new byte[16]; |
|
43 private String lastString; |
|
44 |
|
45 CharsetParser(Charset charset) { |
|
46 this.charset = charset; |
|
47 } |
|
48 |
|
49 @Override |
|
50 public Object parse(RecordingInput input) throws IOException { |
|
51 int size = input.readInt(); |
|
52 ensureSize(size); |
|
53 if (lastSize == size) { |
|
54 boolean equalsLastString = true; |
|
55 for (int i = 0; i < size; i++) { |
|
56 // TODO: No need to read byte per byte |
|
57 byte b = input.readByte(); |
|
58 if (buffer[i] != b) { |
|
59 equalsLastString = false; |
|
60 buffer[i] = b; |
|
61 } |
|
62 } |
|
63 if (equalsLastString) { |
|
64 return lastString; |
|
65 } |
|
66 } else { |
|
67 for (int i = 0; i < size; i++) { |
|
68 buffer[i] = input.readByte(); |
|
69 } |
|
70 } |
|
71 lastString = new String(buffer, 0, size, charset); |
|
72 lastSize = size; |
|
73 return lastString; |
|
74 } |
|
75 |
|
76 @Override |
|
77 public void skip(RecordingInput input) throws IOException { |
|
78 int size = input.readInt(); |
|
79 input.skipBytes(size); |
|
80 } |
|
81 |
|
82 private void ensureSize(int size) { |
|
83 if (buffer.length < size) { |
|
84 buffer = new byte[size]; |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 final static class CharArrayParser extends Parser { |
|
90 private char[] buffer = new char[16]; |
|
91 private int lastSize = -1; |
|
92 private String lastString = null; |
|
93 |
|
94 @Override |
|
95 public Object parse(RecordingInput input) throws IOException { |
|
96 int size = input.readInt(); |
|
97 ensureSize(size); |
|
98 if (lastSize == size) { |
|
99 boolean equalsLastString = true; |
|
100 for (int i = 0; i < size; i++) { |
|
101 char c = input.readChar(); |
|
102 if (buffer[i] != c) { |
|
103 equalsLastString = false; |
|
104 buffer[i] = c; |
|
105 } |
|
106 } |
|
107 if (equalsLastString) { |
|
108 return lastString; |
|
109 } |
|
110 } else { |
|
111 for (int i = 0; i < size; i++) { |
|
112 buffer[i] = input.readChar(); |
|
113 } |
|
114 } |
|
115 lastString = new String(buffer, 0, size); |
|
116 lastSize = size; |
|
117 return lastString; |
|
118 } |
|
119 |
|
120 @Override |
|
121 public void skip(RecordingInput input) throws IOException { |
|
122 int size = input.readInt(); |
|
123 for (int i = 0; i < size; i++) { |
|
124 input.readChar(); |
|
125 } |
|
126 } |
|
127 |
|
128 private void ensureSize(int size) { |
|
129 if (buffer.length < size) { |
|
130 buffer = new char[size]; |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 private final ConstantLookup stringLookup; |
|
136 private final CharArrayParser charArrayParser = new CharArrayParser(); |
|
137 private final CharsetParser utf8parser = new CharsetParser(UTF8); |
|
138 private final CharsetParser latin1parser = new CharsetParser(LATIN1); |
|
139 private final boolean event; |
|
140 |
|
141 public StringParser(ConstantLookup stringLookup, boolean event) { |
|
142 this.stringLookup = stringLookup; |
|
143 this.event = event; |
|
144 } |
|
145 |
|
146 @Override |
|
147 public Object parse(RecordingInput input) throws IOException { |
|
148 byte encoding = input.readByte(); |
|
149 if (encoding == StringEncoding.STRING_ENCODING_CONSTANT_POOL) { |
|
150 long key = input.readLong(); |
|
151 if (event) { |
|
152 return stringLookup.getCurrentResolved(key); |
|
153 } else { |
|
154 return stringLookup.getCurrent(key); |
|
155 } |
|
156 } |
|
157 if (encoding == StringEncoding.STRING_ENCODING_NULL) { |
|
158 return null; |
|
159 } |
|
160 if (encoding == StringEncoding.STRING_ENCODING_EMPTY_STRING) { |
|
161 return ""; |
|
162 } |
|
163 if (encoding == StringEncoding.STRING_ENCODING_CHAR_ARRAY) { |
|
164 return charArrayParser.parse(input); |
|
165 } |
|
166 if (encoding == StringEncoding.STRING_ENCODING_UTF8_BYTE_ARRAY) { |
|
167 return utf8parser.parse(input); |
|
168 } |
|
169 if (encoding == StringEncoding.STRING_ENCODING_LATIN1_BYTE_ARRAY) { |
|
170 return latin1parser.parse(input); |
|
171 } |
|
172 throw new IOException("Unknown string encoding " + encoding); |
|
173 } |
|
174 |
|
175 @Override |
|
176 public void skip(RecordingInput input) throws IOException { |
|
177 byte encoding = input.readByte(); |
|
178 if (encoding == StringEncoding.STRING_ENCODING_CONSTANT_POOL) { |
|
179 input.readLong(); |
|
180 return; |
|
181 } |
|
182 if (encoding == StringEncoding.STRING_ENCODING_EMPTY_STRING) { |
|
183 return; |
|
184 } |
|
185 if (encoding == StringEncoding.STRING_ENCODING_NULL) { |
|
186 return; |
|
187 } |
|
188 if (encoding == StringEncoding.STRING_ENCODING_CHAR_ARRAY) { |
|
189 charArrayParser.skip(input); |
|
190 return; |
|
191 } |
|
192 if (encoding == StringEncoding.STRING_ENCODING_UTF8_BYTE_ARRAY) { |
|
193 utf8parser.skip(input); |
|
194 return; |
|
195 } |
|
196 if (encoding == StringEncoding.STRING_ENCODING_LATIN1_BYTE_ARRAY) { |
|
197 latin1parser.skip(input); |
|
198 return; |
|
199 } |
|
200 throw new IOException("Unknown string encoding " + encoding); |
|
201 } |
|
202 } |