1 /*
   2  * Copyright (c) 2012, 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.internal.util.xml.impl;
  27 
  28 import java.io.Reader;
  29 import java.io.InputStream;
  30 import java.io.IOException;
  31 
  32 /**
  33  * UTF-16 encoded stream reader.
  34  */
  35 public class ReaderUTF16
  36         extends Reader
  37 {
  38         private InputStream is;
  39         private char        bo;
  40 
  41         /**
  42          * Constructor.
  43          *
  44          * Byte order argument can be: 'l' for little-endian or 'b' for big-endian.
  45          *
  46          * @param is A byte input stream.
  47          * @param bo A byte order in the input stream. 
  48          */
  49         public ReaderUTF16(InputStream is, char bo)
  50         {
  51                 switch (bo) {
  52                 case 'l':
  53                         break;
  54 
  55                 case 'b':
  56                         break;
  57 
  58                 default:
  59                         throw new IllegalArgumentException("");
  60                 }
  61                 this.bo = bo;
  62                 this.is = is;
  63         }
  64 
  65         /**
  66          * Reads characters into a portion of an array.
  67          *
  68          * @param cbuf Destination buffer.
  69          * @param off Offset at which to start storing characters.
  70          * @param len Maximum number of characters to read.
  71          * @exception IOException If any IO errors occur.
  72          */
  73         public int read(char[] cbuf, int off, int len)
  74                 throws IOException
  75         {
  76                 int  num = 0;
  77                 int  val;
  78                 if (bo == 'b') {
  79                         while (num < len) {
  80                                 if ((val = is.read()) < 0)
  81                                         return (num != 0)? num: -1;
  82                                 cbuf[off++] = (char)((val << 8) | (is.read() & 0xff));
  83                                 num++;
  84                         }
  85                 } else {
  86                         while (num < len) {
  87                                 if ((val = is.read()) < 0)
  88                                         return (num != 0)? num: -1;
  89                                 cbuf[off++] = (char)((is.read() << 8) | (val & 0xff));
  90                                 num++;
  91                         }
  92                 }
  93                 return num;
  94         }
  95 
  96         /**
  97          * Reads a single character.
  98          *
  99          * @return The character read, as an integer in the range 0 to 65535 
 100          *      (0x0000-0xffff), or -1 if the end of the stream has been reached.
 101          * @exception IOException If any IO errors occur.
 102          */
 103         public int read()
 104                 throws IOException
 105         {
 106                 int  val;
 107                 if ((val = is.read()) < 0)
 108                         return -1;
 109                 if (bo == 'b') {
 110                         val = (char)((val << 8) | (is.read() & 0xff));
 111                 } else {
 112                         val = (char)((is.read() << 8) | (val & 0xff));
 113                 }
 114                 return val;
 115         }
 116 
 117         /**
 118          * Closes the stream.
 119          *
 120          * @exception IOException If any IO errors occur.
 121          */
 122         public void close()
 123                 throws IOException
 124         {
 125                 is.close();
 126         }
 127 }