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