1 /* |
|
2 * Copyright (c) 2015, 2016, 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 java.net.http; |
|
27 |
|
28 import java.nio.ByteBuffer; |
|
29 import java.nio.CharBuffer; |
|
30 import java.nio.charset.CharacterCodingException; |
|
31 import java.nio.charset.CharsetDecoder; |
|
32 import java.nio.charset.CharsetEncoder; |
|
33 import java.nio.charset.CoderResult; |
|
34 import java.nio.charset.CodingErrorAction; |
|
35 import java.nio.charset.StandardCharsets; |
|
36 |
|
37 import static java.lang.System.Logger.Level.WARNING; |
|
38 import static java.net.http.WSUtils.EMPTY_BYTE_BUFFER; |
|
39 import static java.net.http.WSUtils.logger; |
|
40 import static java.nio.charset.StandardCharsets.UTF_8; |
|
41 |
|
42 /* |
|
43 * A collection of tools for UTF-8 coding. |
|
44 */ |
|
45 final class WSCharsetToolkit { |
|
46 |
|
47 private WSCharsetToolkit() { } |
|
48 |
|
49 static final class Encoder { |
|
50 |
|
51 private final CharsetEncoder encoder = UTF_8.newEncoder(); |
|
52 |
|
53 ByteBuffer encode(CharBuffer in) throws CharacterCodingException { |
|
54 return encoder.encode(in); |
|
55 } |
|
56 |
|
57 // TODO: |
|
58 // ByteBuffer[] encode(CharBuffer in) throws CharacterCodingException { |
|
59 // return encoder.encode(in); |
|
60 // } |
|
61 } |
|
62 |
|
63 static CharBuffer decode(ByteBuffer in) throws CharacterCodingException { |
|
64 return UTF_8.newDecoder().decode(in); |
|
65 } |
|
66 |
|
67 static final class Decoder { |
|
68 |
|
69 private final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); |
|
70 |
|
71 { |
|
72 decoder.onMalformedInput(CodingErrorAction.REPORT); |
|
73 decoder.onUnmappableCharacter(CodingErrorAction.REPORT); |
|
74 } |
|
75 |
|
76 private ByteBuffer leftovers = EMPTY_BYTE_BUFFER; |
|
77 |
|
78 WSShared<CharBuffer> decode(WSShared<ByteBuffer> in, boolean endOfInput) |
|
79 throws CharacterCodingException { |
|
80 ByteBuffer b; |
|
81 int rem = leftovers.remaining(); |
|
82 if (rem != 0) { |
|
83 // TODO: We won't need this wasteful allocation & copying when |
|
84 // JDK-8155222 has been resolved |
|
85 b = ByteBuffer.allocate(rem + in.remaining()); |
|
86 b.put(leftovers).put(in.buffer()).flip(); |
|
87 } else { |
|
88 b = in.buffer(); |
|
89 } |
|
90 CharBuffer out = CharBuffer.allocate(b.remaining()); |
|
91 CoderResult r = decoder.decode(b, out, endOfInput); |
|
92 if (r.isError()) { |
|
93 r.throwException(); |
|
94 } |
|
95 if (b.hasRemaining()) { |
|
96 leftovers = ByteBuffer.allocate(b.remaining()).put(b).flip(); |
|
97 } else { |
|
98 leftovers = EMPTY_BYTE_BUFFER; |
|
99 } |
|
100 // Since it's UTF-8, the assumption is leftovers.remaining() < 4 |
|
101 // (i.e. small). Otherwise a shared buffer should be used |
|
102 if (!(leftovers.remaining() < 4)) { |
|
103 logger.log(WARNING, |
|
104 "The size of decoding leftovers is greater than expected: {0}", |
|
105 leftovers.remaining()); |
|
106 } |
|
107 b.position(b.limit()); // As if we always read to the end |
|
108 in.dispose(); |
|
109 // Decoder promises that in the case of endOfInput == true: |
|
110 // "...any remaining undecoded input will be treated as being |
|
111 // malformed" |
|
112 assert !(endOfInput && leftovers.hasRemaining()) : endOfInput + ", " + leftovers; |
|
113 if (endOfInput) { |
|
114 r = decoder.flush(out); |
|
115 decoder.reset(); |
|
116 if (r.isOverflow()) { |
|
117 // FIXME: for now I know flush() does nothing. But the |
|
118 // implementation of UTF8 decoder might change. And if now |
|
119 // flush() is a no-op, it is not guaranteed to remain so in |
|
120 // the future |
|
121 throw new InternalError("Not yet implemented"); |
|
122 } |
|
123 } |
|
124 out.flip(); |
|
125 return WSShared.wrap(out); |
|
126 } |
|
127 } |
|
128 } |
|