Reads into a character buffer using the correct encoding.
977617
public int read()
    throws IOException
  {
    if (_peek >= 0) {
      int peek = _peek;
      _peek = -1;
      return peek;
    }

    InputStream is = _is;
    
    int ch1 = is.read();
    
    if (ch1 < 0x80) {
      return ch1;
    }
    else if ((ch1 & 0xe0) == 0xc0) {
      int ch2 = is.read();
      
      if (ch2 < 0) {
        return error("unexpected end of file in utf8 character");
      }
      else if ((ch2 & 0xc0) != 0x80) {
        return error("utf-8 character conversion error for '{0}' because second byte is invalid at "
                     + String.format("0x%02x 0x%02x", ch1, ch2));
      }
      
      return ((ch1 & 0x1f) << 6) + (ch2 & 0x3f);
    }
    else if ((ch1 & 0xf0) == 0xe0) {
      int ch2 = is.read();
      int ch3 = is.read();
      
      if (ch2 < 0)
        return error("unexpected end of file in utf8 character");
      else if ((ch2 & 0xc0) != 0x80) {
        return error("illegal utf8 encoding at "
                     + "\\x" + Integer.toHexString(ch1)
                     + "\\x" + Integer.toHexString(ch2)
                     + "\\x" + Integer.toHexString(ch3));
      }
      
      if (ch3 < 0)
        return error("unexpected end of file in utf8 character");
      else if ((ch3 & 0xc0) != 0x80)
        return error("illegal utf8 encoding at "
                     + "\\x" + Integer.toHexString(ch1)
                     + "\\x" + Integer.toHexString(ch2)
                     + "\\x" + Integer.toHexString(ch3));

      int ch = ((ch1 & 0x1f) << 12) + ((ch2 & 0x3f) << 6) + (ch3 & 0x3f);
    
      if (ch == 0xfeff) { // If byte-order-mark, read next character
        // server/1m00
        return read();
      }
      else
        return ch;
    }
    else if ((ch1 & 0xf0) == 0xf0) {
      int ch2 = is.read();
      int ch3 = is.read();
      int ch4 = is.read();

      if (ch2 < 0)
        return error("unexpected end of file in utf8 character");
      else if ((ch2 & 0xc0) != 0x80)
        return error("illegal utf8 encoding at 0x" +
                     Integer.toHexString(ch2));
      
      if (ch3 < 0)
        return error("unexpected end of file in utf8 character");
      else if ((ch3 & 0xc0) != 0x80)
        return error("illegal utf8 encoding at 0x" +
                                          Integer.toHexString(ch3));
      
      if (ch4 < 0)
        return error("unexpected end of file in utf8 character");
      else if ((ch4 & 0xc0) != 0x80)
        return error("illegal utf8 encoding at 0x"
                                          + Integer.toHexString(ch4));
      
      int ch = (((ch1 & 0xf) << 18) +
          ((ch2 & 0x3f) << 12) +
          ((ch3 & 0x3f) << 6) +
          ((ch4 & 0x3f)));

      _peek = 0xdc00 + (ch & 0x3ff);
      
      return 0xd800 + ((ch - 0x10000) / 0x400);
    }
    else {
      return error("illegal utf8 encoding at (0x"
                   + Integer.toHexString(ch1) + ")");
    }
  }
157991
def smartread(path):
    
    with open(path, "rb") as f:
        content = f.read()
        result = chardet.detect(content)
        return content.decode(result["encoding"])
158269
def buffer(self, byte_offset=0):
        """"""
        contents = self.ptr.contents
        ptr = addressof(contents.buffer.contents) + byte_offset
        length = contents.length * 4 - byte_offset
        return buffer((c_char * length).from_address(ptr).raw) \
            if length else None
1279735
private void readChar() throws IOException {
        if ('\n' == lastChar)
        {
            this.colNo = 0;
            this.lineNo++;
        }

        lastChar = reader.read();
        if (-1 == lastChar) return ;

        colNo++;
    }
630143
protected String readUTF()
        throws IOException
    {
        int len = (is.read() << 8) + is.read();

        StringBuffer sb = new StringBuffer();

        while (len > 0) {
            int d1 = is.read();

            if (d1 < 0)
                return sb.toString();
            else if (d1 < 0x80) {
                len--;
                sb.append((char) d1);
            }
            else if ((d1 & 0xe0) == 0xc0) {
                len -= 2;
                sb.append(((d1 & 0x1f) << 6) + (is.read() & 0x3f));
            }
            else if ((d1 & 0xf0) == 0xe0) {
                len -= 3;
                sb.append(((d1 & 0x0f) << 12) +
                    ((is.read() & 0x3f) << 6) +
                    (is.read() & 0x3f));
            }
            else
                throw new IOException("utf-8 encoding error");
        }

        return sb.toString();
    }
1170873
def _read_bytes(fp, buf):
    

    # Do the first read without resizing the input buffer
    offset = 0
    remaining = len(buf)
    while remaining > 0:
        l = fp.readinto((ctypes.c_char * remaining).from_buffer(buf, offset))
        if l is None or l == 0:
            return offset
        offset += l
        remaining -= l
    return offset
173001
public int readUTF8ByByteLength(char []buffer, int offset, int byteLength)
    throws IOException
  {
    int k = 0;
    for (int i = 0; i < byteLength; i++) {
      if (_readLength <= _readOffset) {
        readBuffer();
      }

      int ch = _readBuffer[_readOffset++] & 0xff;

      if (ch < 0x80) {
        buffer[k++] = (char) ch;
      }
      else if ((ch & 0xe0) == 0xc0) {
        int c2 = read();
        i += 1;
        buffer[k++] = (char) (((ch & 0x1f) << 6) + (c2 & 0x3f));
      }
      else {
        int c2 = read();
        int c3 = read();

        i += 2;
        buffer[k++] = (char) (((ch & 0x1f) << 12)
                              + ((c2 & 0x3f) << 6)
                              + ((c3 & 0x3f)));
      }
    }

    return k;
  }
649523
public static void readbytes_into(InputStream input, byte[] buffer, int offset, int length) throws IOException {
		while (length > 0) {
			int read = input.read(buffer, offset, length);
			if (read == -1)
				throw new IOException("expected more bytes in input stream");
			offset += read;
			length -= read;
		}
	}
1345030
public int read(CharBuffer buf, int length) throws IOException
  {
    int len = buf.length();

    buf.length(len + length);
    
    int readLength = read(buf.buffer(), len, length);
    
    if (readLength < 0) {
      buf.length(len);
    }
    else if (readLength < length) {
      buf.length(len + readLength);
    }

    return length;
  }
49115
def _encode_buffer(string, f):
	""""""
	if isinstance(string, str):
		string = string.encode()
	f.write(str(len(string)).encode())
	f.write(_TYPE_SEP)
	f.write(string)
391992
function read_raw_string(buf, encoding) {
  var len = buf.readUInt16BE();
  return buf.toString(encoding || 'utf8', len);
}
573554
public int read(char []buf, int offset, int length)
    throws IOException
  {
    for (int i = 0; i < length; i++) {
      int ch = is.read();

      if (ch < 0)
        return i > 0 ? i : -1;
      else {
        buf[offset + i] = (char) ch;
      }
    }
      
    return length;
  }
1022521
private static CharSequence decode0(final CharSequence s) {
    final int length = s.length();
    final CharBuffer cb = CharBuffer.allocate(length);

    for (int i = 0; i < length; ) {
      final char c = s.charAt(i);

      // Not encoded?
      if (c != '%') {
        cb.append(c);
        i++;
        continue;
      }

      // UTF8 - 1 Byte
      int b1 = decodePercent(s, length, i);
      if (b1 == INVALID) {
        return null;
      }
      i += 3;
      final int n = utf8Length(b1);
      if (n == INVALID) {
        return null;
      }
      if (n == 1) {
        cb.append((char) b1);
        continue;
      }

      // UTF8 - 2 Bytes
      final int b2 = decodePercent(s, length, i);
      if (b2 == INVALID) {
        return null;
      }
      i += 3;
      if (n == 2) {
        final int cp = utf8Read2(b1, b2);
        if (cp == INVALID) {
          return null;
        }
        cb.append((char) cp);
        continue;
      }

      // UTF8 - 3 Bytes
      final int b3 = decodePercent(s, length, i);
      if (b3 == INVALID) {
        return null;
      }
      i += 3;
      if (n == 3) {
        final int cp = utf8Read3(b1, b2, b3);
        if (cp == INVALID) {
          return null;
        }
        cb.append((char) cp);
        continue;
      }

      // UTF8 - 4 Bytes
      final int b4 = decodePercent(s, length, i);
      if (b4 == INVALID) {
        return null;
      }
      i += 3;
      final int cp = utf8Read4(b1, b2, b3, b4);
      if (cp == INVALID) {
        return null;
      }
      final int offset = cp - MIN_SUPPLEMENTARY_CODE_POINT;
      cb.append((char) ((offset >>> 10) + MIN_HIGH_SURROGATE));
      cb.append((char) ((offset & 0x3ff) + MIN_LOW_SURROGATE));
    }

    cb.flip();
    return cb;
  }
984109
public static Reader getReadEncoding(InputStream is, String encoding)
    throws UnsupportedEncodingException
  {
    return getReadFactory(encoding).create(is);
  }
1143484
def read_utf(self, offset, len):
        """"""
        try:
            result = self.data[offset:offset + len].decode('utf-8')
        except UnicodeDecodeError:
            result = str('')
        return result
329207
def Read(self):
    
    input_string = self._file_object.readline()

    if isinstance(input_string, py2to3.BYTES_TYPE):
      try:
        input_string = codecs.decode(input_string, self._encoding, self._errors)
      except UnicodeDecodeError:
        if self._errors == 'strict':
          logging.error(
              'Unable to properly read input due to encoding error. '
              'Switching to error tolerant encoding which can result in '
              'non Basic Latin (C0) characters to be replaced with "?" or '
              '"\\ufffd".')
          self._errors = 'replace'

        input_string = codecs.decode(input_string, self._encoding, self._errors)

    return input_string
372323
public static CharBuffer bytesToChars(CharsetDecoder decoder, ByteBuffer bytes, CharBuffer chars) {
    assert decoder.malformedInputAction() == CodingErrorAction.REPORT;

    chars = clearAndEnsureCapacity(chars, (int) (bytes.remaining() * decoder.maxCharsPerByte()));

    bytes.mark();
    decoder.reset();
    CoderResult cr = decoder.decode(bytes, chars, true);
    if (cr.isError()) {
      bytes.reset();
      try {
        cr.throwException();
      } catch (CharacterCodingException e) {
        throw new RuntimeException("Input cannot be mapped to bytes using encoding "
            + decoder.charset().name() + ": " + Arrays.toString(toArray(bytes)), e);
      }
    }

    assert cr.isUnderflow();  // This should be guaranteed by ensuring max. capacity.
    cr = decoder.flush(chars);
    assert cr.isUnderflow();

    chars.flip();
    bytes.reset();

    return chars;
  }