Reads into a character buffer using the correct encoding.

@param cbuf character buffer receiving the data.
@param off starting offset into the buffer.
@param len number of characters to read.

@return the number of characters read or -1 on end of file.
195635
public int read(char []cbuf, int off, int len)
    throws IOException
  {
    int i = 0;

    for (i = 0; i < len; i++) {
      int ch = is.read();

      if (ch < 0)
        return i == 0 ? -1 : i;

      switch (ch) {
      case -1:
        return i == 0 ? -1 : i;
        
      case 130: // unicode 8218
        cbuf[off + i] = ',';
        break;
        
      case 131: // unicode 402
        cbuf[off + i] = 'f';
        break;
        
      case 132: // unicode 8222
        cbuf[off + i] = '"';
        break;
        
      case 133: // unicode 8230 "..."
        cbuf[off + i] = (char) 8230;
        break;
        
      case 134: // unicode 8224 (dagger)
        cbuf[off + i] = '+';
        break;
        
      case 135: // unicode 8225 (double dagger)
        cbuf[off + i] = '+';
        break;
        
      case 136: // unicode 710
        cbuf[off + i] = '^';
        break;
        
      case 137: // unicode 8240 (per-mille 0/00)
        cbuf[off + i] = (char) 8240;
        break;
        
      case 138: // unicode 352
        cbuf[off + i] = 'S';
        break;
        
      case 139: // unicode 8249
        cbuf[off + i] = '<';
        break;
        
      case 140: // unicode 338 (OE)
        cbuf[off + i] = 'O';
        break;
        
      case 145: // unicode 8216
      case 146: // unicode 8217
        cbuf[off + i] = '\'';
        break;
        
      case 147: // unicode 8220
      case 148: // unicode 8221
        cbuf[off + i] = (char) '"';
        break;
        
      case 149: // unicode 8226 (bullet)
        cbuf[off + i] = (char) '*';
        break;
        
      case 150: // unicode 8211
      case 151: // unicode 8212
        cbuf[off + i] = (char) '-';
        break;
        
      case 152: // unicode 732
        cbuf[off + i] = (char) '~';
        break;
        
      case 153: // unicode 8482 (trademark)
        cbuf[off + i] = (char) 8482;
        break;
        
      case 154: // unicode 353
        cbuf[off + i] = 's';
        break;
        
      case 155: // unicode 8250
        cbuf[off + i] = '>';
        break;
        
      case 156: // unicode 339 (oe)
        cbuf[off + i] = 'o';
        break;
        
      case 376: // unicode 376 (Y with umlaut)
        cbuf[off + i] = 'Y';
        break;
        
      default:
        cbuf[off + i] = (char) ch;
      }
    }

    return i;
  }
436664
@Override
  public int write(OutputStreamWithBuffer os, char []cbuf, int off, int len)
    throws IOException
  {
    byte []buffer = os.buffer();
    int length = os.offset();
    int capacity = buffer.length;
    int tail = off + len;
    int head = off;

    while (off < tail) {
      while (capacity - length <= 4) {
        buffer = os.nextBuffer(length);
        length = os.offset();
      }
      
      char ch = cbuf[off++];

      if (ch < 0x80) {
        buffer[length++] = (byte) ch;
      }
      else if (ch < 0x800) {
        buffer[length++] = (byte) (0xc0 + (ch >> 6));
        buffer[length++] = (byte) (0x80 + (ch & 0x3f));
      }
      else if (ch < 0xd800 || 0xdfff < ch) {
        // server/0815
        buffer[length++] = (byte) (0xe0 + (ch >> 12));
        buffer[length++] = (byte) (0x80 + ((ch >> 6) & 0x3f));
        buffer[length++] = (byte) (0x80 + (ch & 0x3f));
      }
      else if (off == tail) {
        off--;
        break;
      }
      else {
        char ch2 = cbuf[off++];
        
        int v = 0x10000 + (ch & 0x3ff) * 0x400 + (ch2 & 0x3ff);
          
        buffer[length++] = (byte) (0xf0 + (v >> 18));
        buffer[length++] = (byte) (0x80 + ((v >> 12) & 0x3f));
        buffer[length++] = (byte) (0x80 + ((v >> 6) & 0x3f));
        buffer[length++] = (byte) (0x80 + (v & 0x3f));
      }
    }

    os.offset(length);
    
    return off - head;
  }
685502
@Override
    public void write(char cbuf[], int off, int len) {
        buf.append(cbuf, off, len);
    }
992456
public int read(char buff[], int off, int len) throws IOException
    {
        int b = read();
        if ( b == -1 )
            return -1;  // EOF, not zero read apparently
        else {
            buff[off]=(char)b;
            return 1;
        }
    }
640463
public int read(byte[] b, int off, int len) throws IOException {
        if (position == size) {
            return -1; // EOF
        }
        if (position < buffer.length) {
            int toCopy = (int)Math.min(size - position,
                Math.min(len, buffer.length - position));
            System.arraycopy(buffer, (int)position, b, off, toCopy);
            if (toCopy > 0) {
                position += toCopy;
            }
            return toCopy;
        }
        // into disk zone
        int read = diskStream.read(b,off,len);
        if(read>0) {
            position += read;
        }
        return read;
    }
477034
private int read1(byte b[], int off, int len)
  throws IOException {
    int avail = count-pos;
    if( avail <= 0 ) {
      if(len>=buf.length) {
        // read a chunk to user buffer directly; avoid one copy
        int nread = readChecksumChunk(b, off, len);
        return nread;
      } else {
        // read a chunk into the local buffer
        fill();
        if( count <= 0 ) {
          return -1;
        } else {
          avail = count;
        }
      }
    }
    
    // copy content of the local buffer to the user buffer
    int cnt = (avail < len) ? avail : len;
    System.arraycopy(buf, pos, b, off, cnt);
    pos += cnt;
    return cnt;    
  }
1032220
public int readInt(int off) {
		this.buffer.rewind();
		return ((buffer.get(off++) & 0xFF) << 24)
				| ((buffer.get(off++) & 0xFF) << 16)
				| ((buffer.get(off++) & 0xFF) << 8)
				| (buffer.get(off++) & 0xFF);
	}
63632
@Override
	public int read(byte[] b, int off, int len) throws IOException {
		// Read from the queue
		synchronized(file) {
			while(true) {
				long fileLen=file.getLength();
				if(fileLen>=1) {
					long pos=file.getFirstIndex();
					file.file.seek(pos+16);
					int readSize=fileLen>len?len:(int)fileLen;
					// When at the end of the file, read the remaining bytes
					if((pos+readSize)>file.maxFifoLength) readSize=(int)(file.maxFifoLength-pos);

					// Read as many bytes as currently available
					int totalRead=file.file.read(b, off, readSize);
					if(totalRead==-1) throw new EOFException("Unexpected EOF");
					addStats(totalRead);
					long newFirstIndex=pos+totalRead;
					while(newFirstIndex>=file.maxFifoLength) newFirstIndex-=file.maxFifoLength;
					file.setFirstIndex(newFirstIndex);
					file.setLength(fileLen-totalRead);
					file.notify();
					return totalRead;
				}
				try {
					file.wait();
				} catch(InterruptedException err) {
					InterruptedIOException ioErr=new InterruptedIOException();
					ioErr.initCause(err);
					throw ioErr;
				}
			}
		}
	}
618178
public static int CJKIndexOf(String str, int offset)
    {
        for ( int j = offset; j < str.length(); j++ ) {
            if ( isCJKChar(str.charAt(j)) ) {
                return j;
            }
        }
        
        return -1;
    }
844602
public int readUnsignedShortAsInt(int off) {
    	this.buffer.position(off);
        int b1 = (0x000000FF & (this.buffer.get()));
        int b2 = (0x000000FF & (this.buffer.get()));
        int val = b1 << 8 | b2;
        return val;
    }
1243915
public int write(OutputStreamWithBuffer os, char []cbuf, int off, int len)
    throws IOException
  {
    for (int i = 0; i < len; i++) {
      char ch = cbuf[off + i];

      if (ch < 0x80)
        os.write(ch);
      else {
        os.write('\\');
        os.write('u');

        int b = (ch >> 12) & 0xf;
        os.write(b < 10 ? b + '0' : b + 'a' - 10);
        b = (ch >> 8) & 0xf;
        os.write(b < 10 ? b + '0' : b + 'a' - 10);
        b = (ch >> 4) & 0xf;
        os.write(b < 10 ? b + '0' : b + 'a' - 10);
        b = ch & 0xf;
        os.write(b < 10 ? b + '0' : b + 'a' - 10);
      }
    }
    
    return len;
  }
652842
public int read(char []cbuf, int off, int len)
    throws IOException
  {
    int i = 0;

    for (i = 0; i < len; i++) {
      int ch1 = is.read();
      int ch2 = is.read();

      if (ch1 < 0)
        return i == 0 ? -1 : i;

      cbuf[off + i] = (char) ((ch2 << 8) + ch1);
    }

    return i;
  }
418346
private int read(final int off, final int len) throws IOException {
        if (len == 0) {
            return 0;
        }
        if (inputStream != null) {
            // Wrapped InputStream
            return inputStream.read(buf, off, len);
        } else {
            // Wrapped ByteBuffer
            final int bytesRemainingInBuf = byteBuffer != null ? byteBuffer.remaining() : buf.length - off;
            final int bytesRead = Math.max(0, Math.min(len, bytesRemainingInBuf));
            if (bytesRead == 0) {
                // Return -1, as per InputStream#read() contract
                return -1;
            }
            if (byteBuffer != null) {
                // Copy from the ByteBuffer into the byte array
                final int byteBufPositionBefore = byteBuffer.position();
                try {
                    byteBuffer.get(buf, off, bytesRead);
                } catch (final BufferUnderflowException e) {
                    // Should not happen
                    throw new IOException("Buffer underflow", e);
                }
                return byteBuffer.position() - byteBufPositionBefore;
            } else {
                // Nothing to read, since ByteBuffer is backed with an array
                return bytesRead;
            }
        }
    }
1207808
public int read(byte[] b, int off, int len) throws IOException {
        checkForClosed();
        if (b == null) {
            throw new NullPointerException();
        } else if ((off < 0) || (len < 0) || ((off + len) > b.length)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        loadBytesAsNeeded();
        int bytesImmediatelyAvailable = bytesLeftInBuffer();
        if (len <= bytesImmediatelyAvailable) {
            System.arraycopy(_bytes, _currentBufferPosition, b, off, len);
            _currentBufferPosition += len;
            return len;
        }

        int bytesRead = EOF;
        if (bytesImmediatelyAvailable > 0) {
            bytesRead = read(b, off, bytesImmediatelyAvailable);
            if (bytesRead != EOF) {
                int furtherBytesRead = read(b, off + bytesRead, len - bytesRead);
                if (furtherBytesRead != EOF) {
                    bytesRead += furtherBytesRead;
                }
            }
        }
        return bytesRead;
    }
562598
public int read(byte b[], int off, int len) throws IOException {
	if (off + len > b.length) {
	    len = b.length - off;
	}
	if (len < 0) {
	    return -1;
	}
	if (len == 0) {
	    return 0;
	}
	int n = blob.read(b, off, pos, len);
	if (n > 0) {
	    pos += n;
	    return n;
	}
	return -1;
    }
151370
private void writeCharsToOut(char[] cbuf, int len) throws IOException {
		if(len > 0) {
			if(len == 1) {
				out.write(cbuf[0]);
			} else {
				out.write(cbuf, 0, len);
			}
		}
	}
889212
@Override
	public int read(byte[] b, int off, int len) throws IOException {
		if (currentEntry != null) {
			if (currentFileSize == currentEntry.getSize()) {
				return -1;
			} else if ((currentEntry.getSize() - currentFileSize) < len) {
				len = (int) (currentEntry.getSize() - currentFileSize);
			}
		}

		int br = super.read(b, off, len);

		if (br != -1) {
			if (currentEntry != null) {
				currentFileSize += br;
			}

			bytesRead += br;
		}

		return br;
	}