|
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.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 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 |
|
40 final static class CharsetParser extends Parser { |
|
41 private final Charset charset; |
|
42 private int lastSize; |
|
43 private byte[] buffer = new byte[16]; |
|
44 private String lastString; |
|
45 |
|
46 CharsetParser(Charset charset) { |
|
47 this.charset = charset; |
|
48 } |
|
49 |
|
50 @Override |
|
51 public Object parse(RecordingInput input) throws IOException { |
|
52 int size = input.readInt(); |
|
53 ensureSize(size); |
|
54 if (lastSize == size) { |
|
55 boolean equalsLastString = true; |
|
56 for (int i = 0; i < size; i++) { |
|
57 // TODO: No need to read byte per byte |
|
58 byte b = input.readByte(); |
|
59 if (buffer[i] != b) { |
|
60 equalsLastString = false; |
|
61 buffer[i] = b; |
|
62 } |
|
63 } |
|
64 if (equalsLastString) { |
|
65 return lastString; |
|
66 } |
|
67 } else { |
|
68 for (int i = 0; i < size; i++) { |
|
69 buffer[i] = input.readByte(); |
|
70 } |
|
71 } |
|
72 lastString = new String(buffer, 0, size, charset); |
|
73 lastSize = size; |
|
74 return lastString; |
|
75 } |
|
76 |
|
77 @Override |
|
78 public void skip(RecordingInput input) throws IOException { |
|
79 int size = input.readInt(); |
|
80 input.skipBytes(size); |
|
81 } |
|
82 |
|
83 private void ensureSize(int size) { |
|
84 if (buffer.length < size) { |
|
85 buffer = new byte[size]; |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 final static class CharArrayParser extends Parser { |
|
91 private char[] buffer = new char[16]; |
|
92 private int lastSize = -1; |
|
93 private String lastString = null; |
|
94 |
|
95 @Override |
|
96 public Object parse(RecordingInput input) throws IOException { |
|
97 int size = input.readInt(); |
|
98 ensureSize(size); |
|
99 if (lastSize == size) { |
|
100 boolean equalsLastString = true; |
|
101 for (int i = 0; i < size; i++) { |
|
102 char c = input.readChar(); |
|
103 if (buffer[i] != c) { |
|
104 equalsLastString = false; |
|
105 buffer[i] = c; |
|
106 } |
|
107 } |
|
108 if (equalsLastString) { |
|
109 return lastString; |
|
110 } |
|
111 } else { |
|
112 for (int i = 0; i < size; i++) { |
|
113 buffer[i] = input.readChar(); |
|
114 } |
|
115 } |
|
116 lastString = new String(buffer, 0, size); |
|
117 lastSize = size; |
|
118 return lastString; |
|
119 } |
|
120 |
|
121 @Override |
|
122 public void skip(RecordingInput input) throws IOException { |
|
123 int size = input.readInt(); |
|
124 for (int i = 0; i < size; i++) { |
|
125 input.readChar(); |
|
126 } |
|
127 } |
|
128 |
|
129 private void ensureSize(int size) { |
|
130 if (buffer.length < size) { |
|
131 buffer = new char[size]; |
|
132 } |
|
133 } |
|
134 } |
|
135 |
|
136 private final ConstantLookup stringLookup; |
|
137 private final CharArrayParser charArrayParser = new CharArrayParser(); |
|
138 private final CharsetParser utf8parser = new CharsetParser(UTF8); |
|
139 private final CharsetParser latin1parser = new CharsetParser(LATIN1); |
|
140 private final boolean event; |
|
141 |
|
142 public StringParser(ConstantLookup stringLookup, boolean event) { |
|
143 this.stringLookup = stringLookup; |
|
144 this.event = event; |
|
145 } |
|
146 |
|
147 @Override |
|
148 public Object parse(RecordingInput input) throws IOException { |
|
149 byte encoding = input.readByte(); |
|
150 if (encoding == StringEncoding.STRING_ENCODING_CONSTANT_POOL) { |
|
151 long key = input.readLong(); |
|
152 if (event) { |
|
153 return stringLookup.getCurrentResolved(key); |
|
154 } else { |
|
155 return stringLookup.getCurrent(key); |
|
156 } |
|
157 } |
|
158 if (encoding == StringEncoding.STRING_ENCODING_NULL) { |
|
159 return null; |
|
160 } |
|
161 if (encoding == StringEncoding.STRING_ENCODING_EMPTY_STRING) { |
|
162 return ""; |
|
163 } |
|
164 if (encoding == StringEncoding.STRING_ENCODING_CHAR_ARRAY) { |
|
165 return charArrayParser.parse(input); |
|
166 } |
|
167 if (encoding == StringEncoding.STRING_ENCODING_UTF8_BYTE_ARRAY) { |
|
168 return utf8parser.parse(input); |
|
169 } |
|
170 if (encoding == StringEncoding.STRING_ENCODING_LATIN1_BYTE_ARRAY) { |
|
171 return latin1parser.parse(input); |
|
172 } |
|
173 throw new IOException("Unknown string encoding " + encoding); |
|
174 } |
|
175 |
|
176 @Override |
|
177 public void skip(RecordingInput input) throws IOException { |
|
178 byte encoding = input.readByte(); |
|
179 if (encoding == StringEncoding.STRING_ENCODING_CONSTANT_POOL) { |
|
180 input.readLong(); |
|
181 return; |
|
182 } |
|
183 if (encoding == StringEncoding.STRING_ENCODING_EMPTY_STRING) { |
|
184 return; |
|
185 } |
|
186 if (encoding == StringEncoding.STRING_ENCODING_NULL) { |
|
187 return; |
|
188 } |
|
189 if (encoding == StringEncoding.STRING_ENCODING_CHAR_ARRAY) { |
|
190 charArrayParser.skip(input); |
|
191 return; |
|
192 } |
|
193 if (encoding == StringEncoding.STRING_ENCODING_UTF8_BYTE_ARRAY) { |
|
194 utf8parser.skip(input); |
|
195 return; |
|
196 } |
|
197 if (encoding == StringEncoding.STRING_ENCODING_LATIN1_BYTE_ARRAY) { |
|
198 latin1parser.skip(input); |
|
199 return; |
|
200 } |
|
201 throw new IOException("Unknown string encoding " + encoding); |
|
202 } |
|
203 } |