Reads UTF-8 encoded characters from the given stream, but does not read
the length.

@return number of characters actually read, or -1 if EOF reached.
357546
public static final int readUTF(DataInput in,
                                    char[] chars, int offset, int length)
        throws IOException
    {
        if (length == 0) {
            return 0;
        }

        int c, c2, c3;

        int charCount = 0;
        while (charCount < length) {
            try {
                c = in.readByte() & 0xff;
            }
            catch (EOFException e) {
                break;
            }
            switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                chars[offset + charCount++] = (char)c;
                break;
            case 12: case 13:
                // 110x xxxx  10xx xxxx
                c2 = in.readByte();
                if ((c2 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] =
                    (char)(((c & 0x1f) << 6) | (c2 & 0x3f));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                c2 = in.readByte();
                c3 = in.readByte();
                if ((c2 & 0xc0) != 0x80 || (c3 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] = (char)(((c & 0x0f) << 12) |
                                                     ((c2 & 0x3f) << 6) |
                                                     (c3 & 0x3f));
                break;
            default:
                // 10xx xxxx,  1111 xxxx
                throw new UTFDataFormatException();
            }
        }

        return (charCount == 0) ? -1 : charCount;
    }
77464
public static final int readUTF(InputStream in,
                                    char[] chars, int offset, int length)
        throws IOException
    {
        if (length == 0) {
            return 0;
        }

        int c, c2, c3;

        int charCount = 0;
        while (charCount < length) {
            c = in.read();
            if (c < 0) {
                break;
            }
            c &= 0xff;
            switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                chars[offset + charCount++] = (char)c;
                break;
            case 12: case 13:
                // 110x xxxx  10xx xxxx
                c2 = in.read();
                if (c2 < 0) {
                    throw new EOFException();
                }
                if ((c2 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] =
                    (char)(((c & 0x1f) << 6) | (c2 & 0x3f));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                c2 = in.read();
                if (c2 < 0) {
                    throw new EOFException();
                }
                c3 = in.read();
                if (c3 < 0) {
                    throw new EOFException();
                }
                if ((c2 & 0xc0) != 0x80 || (c3 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] = (char)(((c & 0x0f) << 12) |
                                                     ((c2 & 0x3f) << 6) |
                                                     (c3 & 0x3f));
                break;
            default:
                // 10xx xxxx,  1111 xxxx
                throw new UTFDataFormatException();
            }
        }

        return (charCount == 0) ? -1 : charCount;
    }
757263
@Nullable
    public static String readUTF(@NotNull final InputStream stream) throws IOException {
        final DataInputStream dataInput = new DataInputStream(stream);
        if (stream instanceof ByteArraySizedInputStream) {
            final ByteArraySizedInputStream sizedStream = (ByteArraySizedInputStream) stream;
            final int streamSize = sizedStream.size();
            if (streamSize >= 2) {
                sizedStream.mark(Integer.MAX_VALUE);
                final int utfLen = dataInput.readUnsignedShort();
                if (utfLen == streamSize - 2) {
                    boolean isAscii = true;
                    final byte[] bytes = sizedStream.toByteArray();
                    for (int i = 0; i < utfLen; ++i) {
                        if ((bytes[i + 2] & 0xff) > 127) {
                            isAscii = false;
                            break;
                        }
                    }
                    if (isAscii) {
                        return fromAsciiByteArray(bytes, 2, utfLen);
                    }
                }
                sizedStream.reset();
            }
        }
        try {
            String result = null;
            StringBuilder builder = null;
            for (; ; ) {
                final String temp;
                try {
                    temp = dataInput.readUTF();
                    if (result != null && result.length() == 0 &&
                        builder != null && builder.length() == 0 && temp.length() == 0) {
                        break;
                    }
                } catch (EOFException e) {
                    break;
                }
                if (result == null) {
                    result = temp;
                } else {
                    if (builder == null) {
                        builder = new StringBuilder();
                        builder.append(result);
                    }
                    builder.append(temp);
                }
            }
            return (builder != null) ? builder.toString() : result;
        } finally {
            dataInput.close();
        }
    }
973419
protected int readByte(InputStream is) throws IOException
   {
      byte[] data = new byte[1];
      if (is.read(data) != data.length)
      {
         throw new EOFException();
      }

      return (MPPUtility.getByte(data, 0));
   }
1267961
private int parseUTF8Char()
        throws IOException
    {
        int ch = read();

        if (ch < 0x80)
            return ch;
        else if ((ch & 0xe0) == 0xc0) {
            int ch1 = read();
            int v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);

            return v;
        }
        else if ((ch & 0xf0) == 0xe0) {
            int ch1 = read();
            int ch2 = read();
            int v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);

            return v;
        }
        else
            throw error("bad utf-8 encoding at " + codeName(ch));
    }
993511
@Override
    public int read()
        throws IOException
    {
        synchronized ( lock )
        {
            ensureOpen();
            if ( next >= length )
                return -1;
            return seq.charAt( next++ );
        }
    }
242081
private static long readVLong(DataInput stream) throws IOException {
    byte firstByte = stream.readByte();
    int len = decodeVIntSize(firstByte);
    if (len == 1) {
      return firstByte;
    }
    long i = 0;
    for (int idx = 0; idx < len-1; idx++) {
      byte b = stream.readByte();
      i = i << 8;
      i = i | (b & 0xFF);
    }
    return (isNegativeVInt(firstByte) ? (i ^ -1L) : i);
  }
1173687
private int parseChar()
        throws IOException
    {
        while (_chunkLength <= 0) {
            if (_isLastChunk)
                return -1;

            int code = read();

            switch (code) {
                case 's':
                case 'x':
                    _isLastChunk = false;

                    _chunkLength = (read() << 8) + read();
                    break;

                case 'S':
                case 'X':
                    _isLastChunk = true;

                    _chunkLength = (read() << 8) + read();
                    break;

                default:
                    throw expect("string", code);
            }

        }

        _chunkLength--;

        return parseUTF8Char();
    }
589083
public String readType()
        throws IOException
    {
        int code = read();

        if (code != 't') {
            _peek = code;
            return "";
        }

        _isLastChunk = true;
        _chunkLength = (read() << 8) + read();

        _sbuf.setLength(0);
        int ch;
        while ((ch = parseChar()) >= 0)
            _sbuf.append((char) ch);

        return _sbuf.toString();
    }
390785
public static String read(InputStream in) throws IOException {
        InputStreamReader isReader = new InputStreamReader(in, "UTF-8");
        BufferedReader reader = new BufferedReader(isReader);
        StringBuffer out = new StringBuffer();
        char[] c = new char[4096];
        for (int n; (n = reader.read(c)) != -1;) {
            out.append(new String(c, 0, n));
        }
        reader.close();

        return out.toString();
    }
407156
@Override
    public int read() throws IOException {
        if (newLineWasRead) {
            line += 1;
            column = 1;
            newLineWasRead = false;
        }

        int charRead = super.read();
        if (charRead > -1) {
            char c = (char)charRead;
            // found a \r or \n, like on Mac or Unix
            // could also be Windows' \r\n
            if (c == '\r' || c == '\n') {
                newLineWasRead = true;
                if (c == '\r') {
                    mark(1);
                    c = (char)super.read();
                    // check if we have \r\n like on Windows
                    // if it's not \r\n we reset, otherwise, the \n is just consummed
                    if (c != '\n') {
                        reset();
                    }
                }
            } else {
                column += 1;
            }
        }

        return charRead;
    }
329795
@Override
	public int read() throws IOException {
		int ch = super.read();
		if (!escaped && charsToEscape.contains((char) ch)) {
			// special case for non-displayable characters
			switch (ch) {
			case '\n':
				ch = 'n';
				break;
			case '\r':
				ch = 'r';
				break;
			case '\t':
				ch = 't';
				break;
			}
			super.unread(ch);
			escaped = true;
			ch = '\\';
		} else {
			escaped = false;
		}
		return ch;
	}
324122
public static String readFromInputStream(InputStream inputStream) {
        try {
            return IOUtils.toString(inputStream, Charset.forName("utf-8")).trim();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
146579
public static int utf8Bytes(final CharSequence seq) {
        if (seq instanceof AsciiString) {
            return seq.length();
        }
        int seqLength = seq.length();
        int i = 0;
        // ASCII fast path
        while (i < seqLength && seq.charAt(i) < 0x80) {
            ++i;
        }
        // !ASCII is packed in a separate method to let the ASCII case be smaller
        return i < seqLength ? i + utf8Bytes(seq, i, seqLength) : i;
    }
1251022
public int readChar()
        throws IOException
    {
        if (_chunkLength > 0) {
            _chunkLength--;
            if (_chunkLength == 0 && _isLastChunk)
                _chunkLength = END_OF_DATA;

            int ch = parseUTF8Char();
            return ch;
        }
        else if (_chunkLength == END_OF_DATA) {
            _chunkLength = 0;
            return -1;
        }

        int tag = read();

        switch (tag) {
            case 'N':
                return -1;

            case 'S':
            case 's':
            case 'X':
            case 'x':
                _isLastChunk = tag == 'S' || tag == 'X';
                _chunkLength = (read() << 8) + read();

                _chunkLength--;
                int value = parseUTF8Char();

                // special code so successive read byte won't
                // be read as a single object.
                if (_chunkLength == 0 && _isLastChunk)
                    _chunkLength = END_OF_DATA;

                return value;

            default:
                throw new IOException("expected 'S' at " + (char) tag);
        }
    }
53137
public int read() throws java.io.IOException {
    checkClosed();
    try {
      if (limit > 0 && apos >= limit) {
        return -1;
      }
      if (buffer == null || bpos >= buffer.length) {
        buffer = lo.read(bsize);
        bpos = 0;
      }

      // Handle EOF
      if (bpos >= buffer.length) {
        return -1;
      }

      int ret = (buffer[bpos] & 0x7F);
      if ((buffer[bpos] & 0x80) == 0x80) {
        ret |= 0x80;
      }

      bpos++;
      apos++;

      return ret;
    } catch (SQLException se) {
      throw new IOException(se.toString());
    }
  }
1151157
public int read() throws IOException {
    // Read the next non-whitespace character
    int x;
    do {
      x = in.read();
      if (x == -1) {
        return -1;
      }
    } while (Character.isWhitespace((char) x));
    charCount++;

    // The '=' sign is just padding
    if (x == '=') {
      return -1; // effective end of stream
    }

    // Convert from raw form to 6-bit form
    x = ints[x];

    // Calculate which character we're decoding now
    int mode = (charCount - 1) % 4;

    // First char save all six bits, go for another
    if (mode == 0) {
      carryOver = x & 63;
      return read();
    }
    // Second char use previous six bits and first two new bits,
    // save last four bits
    else if (mode == 1) {
      int decoded = ((carryOver << 2) + (x >> 4)) & 255;
      carryOver = x & 15;
      return decoded;
    }
    // Third char use previous four bits and first four new bits,
    // save last two bits
    else if (mode == 2) {
      int decoded = ((carryOver << 4) + (x >> 2)) & 255;
      carryOver = x & 3;
      return decoded;
    }
    // Fourth char use previous two bits and all six new bits
    else if (mode == 3) {
      int decoded = ((carryOver << 6) + x) & 255;
      return decoded;
    }
    return -1; // can't actually reach this line
  }