< prev index next >

src/java.base/share/classes/sun/nio/cs/StreamDecoder.java

Print this page


   1 /*
   2  * Copyright (c) 2001, 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 /*
  27  */
  28 
  29 package sun.nio.cs;
  30 
  31 import java.io.FileInputStream;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import java.io.UnsupportedEncodingException;
  35 import java.io.Reader;
  36 import java.nio.ByteBuffer;
  37 import java.nio.CharBuffer;
  38 import java.nio.channels.FileChannel;
  39 import java.nio.channels.ReadableByteChannel;
  40 import java.nio.charset.Charset;
  41 import java.nio.charset.CharsetDecoder;
  42 import java.nio.charset.CoderResult;
  43 import java.nio.charset.CodingErrorAction;
  44 import java.nio.charset.IllegalCharsetNameException;
  45 
  46 public class StreamDecoder extends Reader
  47 {
  48 
  49     private static final int MIN_BYTE_BUFFER_SIZE = 32;
  50     private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
  51 
  52     private volatile boolean closed;
  53 
  54     private void ensureOpen() throws IOException {
  55         if (closed)
  56             throw new IOException("Stream closed");
  57     }
  58 
  59     // In order to handle surrogates properly we must never try to produce
  60     // fewer than two characters at a time.  If we're only asked to return one
  61     // character then the other is saved here to be returned later.
  62     //
  63     private boolean haveLeftoverChar = false;
  64     private char leftoverChar;


 183                     return (n == 0) ? -1 : n;
 184                 cbuf[off] = (char)c;
 185                 return n + 1;
 186             }
 187 
 188             return n + implRead(cbuf, off, off + len);
 189         }
 190     }
 191 
 192     public boolean ready() throws IOException {
 193         synchronized (lock) {
 194             ensureOpen();
 195             return haveLeftoverChar || implReady();
 196         }
 197     }
 198 
 199     public void close() throws IOException {
 200         synchronized (lock) {
 201             if (closed)
 202                 return;
 203             try {
 204                 implClose();
 205             } finally {
 206                 closed = true;
 207             }
 208         }
 209     }
 210 
 211     private boolean isOpen() {
 212         return !closed;
 213     }
 214 
 215 
 216     // -- Charset-based stream decoder impl --
 217 
 218     // In the early stages of the build we haven't yet built the NIO native
 219     // code, so guard against that by catching the first UnsatisfiedLinkError
 220     // and setting this flag so that later attempts fail quickly.
 221     //
 222     private static volatile boolean channelsAvailable = true;
 223 
 224     private static FileChannel getChannel(FileInputStream in) {
 225         if (!channelsAvailable)
 226             return null;
 227         try {


   1 /*
   2  * Copyright (c) 2001, 2011, 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 /*
  27  */
  28 
  29 package sun.nio.cs;
  30 
  31 import java.io.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;
  34 import java.nio.charset.*;










  35 
  36 public class StreamDecoder extends Reader
  37 {
  38 
  39     private static final int MIN_BYTE_BUFFER_SIZE = 32;
  40     private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
  41 
  42     private volatile boolean closed;
  43 
  44     private void ensureOpen() throws IOException {
  45         if (closed)
  46             throw new IOException("Stream closed");
  47     }
  48 
  49     // In order to handle surrogates properly we must never try to produce
  50     // fewer than two characters at a time.  If we're only asked to return one
  51     // character then the other is saved here to be returned later.
  52     //
  53     private boolean haveLeftoverChar = false;
  54     private char leftoverChar;


 173                     return (n == 0) ? -1 : n;
 174                 cbuf[off] = (char)c;
 175                 return n + 1;
 176             }
 177 
 178             return n + implRead(cbuf, off, off + len);
 179         }
 180     }
 181 
 182     public boolean ready() throws IOException {
 183         synchronized (lock) {
 184             ensureOpen();
 185             return haveLeftoverChar || implReady();
 186         }
 187     }
 188 
 189     public void close() throws IOException {
 190         synchronized (lock) {
 191             if (closed)
 192                 return;
 193             implClose();
 194             closed = true;



 195         }
 196     }
 197 
 198     private boolean isOpen() {
 199         return !closed;
 200     }
 201 
 202 
 203     // -- Charset-based stream decoder impl --
 204 
 205     // In the early stages of the build we haven't yet built the NIO native
 206     // code, so guard against that by catching the first UnsatisfiedLinkError
 207     // and setting this flag so that later attempts fail quickly.
 208     //
 209     private static volatile boolean channelsAvailable = true;
 210 
 211     private static FileChannel getChannel(FileInputStream in) {
 212         if (!channelsAvailable)
 213             return null;
 214         try {


< prev index next >