writestream.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,382 行 · 第 1/2 页

JAVA
1,382
字号
    int length = 0;    int exp = 10;    if (i >= 1000000000)      length = 9;    else {      for (; i >= exp; length++)        exp = 10 * exp;    }    byte []buffer = _writeBuffer;    int writeLength = this._writeLength;        if (writeLength + length < buffer.length) {      writeLength += length;      this._writeLength = writeLength + 1;            for (int j = 0; j <= length; j++) {        buffer[writeLength - j] = (byte) (i % 10 + '0');        i = i / 10;      }      return;    }        if (_bytes == null)      _bytes = new byte[32];    int j = 31;        while (i > 0) {      _bytes[--j] = (byte) ((i % 10) + '0');      i /= 10;    }    write(_bytes, j, 31 - j);  }  /**   * Prints a long.   */  public final void print(long i) throws IOException  {    if (i == 0x8000000000000000L) {      print("-9223372036854775808");      return;    }        if (_bytes == null)      _bytes = new byte[32];    if (i < 0) {      write('-');      i = -i;    } else if (i == 0) {      write('0');      return;    }    int j = 31;        while (i > 0) {      _bytes[--j] = (byte) ((i % 10) + '0');      i /= 10;    }    write(_bytes, j, 31 - j);  }  /**   * Prints a float.   */  public final void print(float f) throws IOException  {    print(String.valueOf(f));  }  /**   * Prints an double   */  public final void print(double d) throws IOException  {    print(String.valueOf(d));  }  /**   * Prints a double, converted by String.valueOf()   */  public final void print(Object o) throws IOException  {    if (o == null)      print("null");    else if (o instanceof VfsWriteObject)      ((VfsWriteObject) o).print(this);    else      print(o.toString());  }  /**   * Prints a newline   */  public final void println() throws IOException  {    write(newlineBytes, 0, newlineBytes.length);    if (flushOnNewline)      flush();  }  /**   * Prints a character buffer followed by a newline.   */  public final void println(char []buf, int offset, int length)    throws IOException  {    print(buf, offset, length);    write(newlineBytes, 0, newlineBytes.length);    if (flushOnNewline)      flush();  }  /**   * Prints a string buffer followed by a newline.   */  public final void println(String string) throws IOException  {    print(string);    write(newlineBytes, 0, newlineBytes.length);    if (flushOnNewline)      flush();  }  /**   * Prints a boolean followed by a newline.   */  public final void println(boolean b) throws IOException  {    println(b ? "true" : "false");  }  /**   * Prints a char followed by a newline.   */  public final void println(char ch) throws IOException  {    write(ch);    write(newlineBytes, 0, newlineBytes.length);    if (flushOnNewline)      flush();  }  /**   * Prints an integer followed by a newline.   */  public final void println(int i) throws IOException  {    print(i);    write(newlineBytes, 0, newlineBytes.length);    if (flushOnNewline)      flush();  }  /**   * Prints a long followed by a newline.   */  public final void println(long l) throws IOException  {    print(l);    write(newlineBytes, 0, newlineBytes.length);    if (flushOnNewline)      flush();  }  /**   * Prints a float followed by a newline.   */  public final void println(float f) throws IOException  {    println(String.valueOf(f));  }  /**   * Prints a double followed by a newline.   */  public final void println(double d) throws IOException  {    println(String.valueOf(d));  }  /**   * Prints an object, converted to a string, followed by a newline.   */  public final void println(Object o) throws IOException  {    if (o == null)      println("null");    else      println(o.toString());  }  /**   * Returns a printWriter writing to this stream.   */  public PrintWriter getPrintWriter()  {    /*    if (_writer == null)      _writer = new StreamWriter();    */    if (_printWriter == null)      _printWriter = new StreamPrintWriter(this);    /*    else      _printWriter.setWriter(_writer);    */    return _printWriter;  }  /**   * Logs a line to the stream.  log is essentially println, but   * it doesn't throw an exception and it always flushes the output.   */  public final void log(String string)  {    try {      synchronized (this) {        println(string);        flush();      }    } catch (Exception e) {    }  }  public final void log(Throwable exn)  {    try {      PrintWriter out = getPrintWriter();      synchronized (this) {        if (exn != null) {          exn.printStackTrace(out);          flush();        }      }    } catch (Throwable e) {      e.printStackTrace();    }  }  /**   * Writes the contents of a JDK stream.  Essentially this will copy   * <code>source</code> to the current stream.   *   * @param source InputStream to read.   */  public long writeStream(InputStream source) throws IOException  {    if (source == null)      return 0;    int len;    int length = _writeBuffer.length;    long outputLength = 0;    if (_writeLength >= length) {      int tmplen = _writeLength;      _writeLength = 0;      _source.write(_writeBuffer, 0, tmplen, false);      _position += tmplen;      outputLength += tmplen;    }    while ((len = source.read(_writeBuffer,			      _writeLength,			      length - _writeLength)) >= 0) {      _writeLength += len;      outputLength += len;            if (length <= _writeLength) {	int tmplen = _writeLength;	_writeLength = 0;	_source.write(_writeBuffer, 0, tmplen, false);	_position += tmplen;      }    }    if (flushOnNewline || _implicitFlush)      flush();    return outputLength;  }  /**   * Writes the contents of a JDK reader.  Essentially this will copy   * <code>source</code> to the current stream.   *   * @param source InputStream to read.   */  public void writeStream(Reader reader) throws IOException  {    if (reader == null)      return;    if (chars == null)      chars = new char[256];    int len;    while ((len = reader.read(chars, 0, chars.length)) > 0) {      print(chars, 0, len);    }  }  /**   * Writes the contents of a JDK stream.  Essentially this will copy   * <code>source</code> to the current stream.   *   * @param source InputStream to read.   */  public void writeStream(InputStream source, int totalLength)    throws IOException  {    if (source == null)      return;    int len;    int length = _writeBuffer.length;    if (length <= _writeLength) {      int tmplen = _writeLength;      _writeLength = 0;      _source.write(_writeBuffer, 0, tmplen, false);      _position += tmplen;    }    while (totalLength > 0) {      int sublen = length - _writeLength;            if (totalLength < sublen)        sublen = totalLength;            sublen = source.read(_writeBuffer, _writeLength, sublen);      if (sublen < 0)        break;            _writeLength += sublen;      totalLength -= sublen;      if (length <= _writeLength) {	int tmplen = _writeLength;	_writeLength = 0;	_source.write(_writeBuffer, 0, tmplen, false);	_position += tmplen;      }    }    if (flushOnNewline || _implicitFlush)      flush();  }  /**   * Writes the contents of a JDK stream.  Essentially this will copy   * <code>source</code> to the current stream.   *   * @param source InputStream to read.   */  public void writeStream(StreamImpl source) throws IOException  {    if (source == null)      return;    int len;    int length = _writeBuffer.length;    if (_writeLength >= length) {      int tmplen = _writeLength;      _writeLength = 0;      this._source.write(_writeBuffer, 0, tmplen, false);      _position += tmplen;    }    while ((len = source.read(_writeBuffer,			      _writeLength,			      length - _writeLength)) >= 0) {      _writeLength += len;      if (_writeLength >= length) {	int tmplen = _writeLength;	_writeLength = 0;	this._source.write(_writeBuffer, 0, tmplen, false);	_position += tmplen;      }    }    if (flushOnNewline || _implicitFlush)      flush();  }  /**   * Copies a file to the stream.   *   * @param path Path of the file to copy.   */  public void writeFile(Path path) throws IOException  {    StreamImpl is = path.openReadImpl();    try {      if (is != null)	writeStream(is);    } finally {      if (is != null)	is.close();    }  }  /**   * Disables close.  Sometimes an application will pass a stream   * to a client that may close the stream at an inappropriate time.   * Setting disable close gives the calling routine control over closing   * the stream.   */  public void setDisableClose(boolean disableClose)  {    this._disableClose = disableClose;  }  public boolean getDisableClose()  {    return _disableClose;  }  /**   * Disables close of the underlying source.   */  public void setDisableCloseSource(boolean disableClose)  {    _isDisableCloseSource = disableClose;  }  /**   * Returns true if the stream is closed.   */  public final boolean isClosed()  {    return _source != null;  }  /**   * Close the stream, first flushing the write buffer.   */  public final void close() throws IOException  {    StreamImpl s = _source;        try {      int len = _writeLength;      if (len > 0) {	_writeLength = 0;        if (s != null)          s.write(_writeBuffer, 0, len, true);      }    } finally {      if (_disableClose) {	return;      }      _source = null;      if (_writeEncoding != null)        _writeEncoding = null;      if (! reuseBuffer) {        if (_tempWrite != null) {          TempBuffer.free(_tempWrite);          _tempWrite = null;        }        _tempWrite = null;        _writeBuffer = null;      }      if (s != null && ! _isDisableCloseSource)	s.closeWrite();    }  }  /**   * Frees the buffer   */  public final void free()  {    _source = null;        if (_tempWrite != null) {      TempBuffer.free(_tempWrite);      _tempWrite = null;    }        _tempWrite = null;    _writeBuffer = null;  }      /**   * Returns a named attribute.  For example, an HTTP stream   * may use this to return header values.   */  public Object getAttribute(String name)    throws IOException  {    return _source.getAttribute(name);  }  /**   * Sets a named attribute.  For example, an HTTP stream   * may use this to set header values.   */  public void setAttribute(String name, Object value)    throws IOException  {    _source.setAttribute(name, value);  }  /**   * Removes a named attribute.   */  public void removeAttribute(String name)    throws IOException  {    _source.removeAttribute(name);  }  /**   * Lists all named attributes.   */  public Iterator getAttributeNames()    throws IOException  {    return _source.getAttributeNames();  }  /**   * Returns the Path which opened this stream.   */  public Path getPath()  {    if (_source != null)      return _source.getPath();    else      return null;  }  /**   * Returns the user path which opened this stream.   *   * <p>Parsing routines typically use this for error reporting.   */  public String getUserPath()  {    return _source.getPath().getUserPath();  }  /**   * Sets a path name associated with the stream.   */  public void setPath(Path path)  {    _source.setPath(path);  }    /**   * For testing, sets the system newlines.   */  public static void setSystemNewline(String newline)  {    _sysNewline = newline;    _sysNewlineBytes = _sysNewline.getBytes();  }  public boolean lock(boolean shared, boolean block)  {    if (! (_source instanceof LockableStream))      return true;    LockableStream ls = (LockableStream) _source;    return ls.lock(shared, block);  }  public boolean unlock()  {    if (! (_source instanceof LockableStream))      return true;    LockableStream ls = (LockableStream) _source;    return ls.unlock();  }  /**   * Returns the write position.   */  public long getPosition()  {    return _position + _writeLength;  }  /**   * Clears the position for statistics cases like a socket stream.   */  public void clearPosition()  {    _position = - _writeLength;  }  /**   * Sets the current write position.   */  public boolean setPosition(long pos)    throws IOException  {    if (pos < 0) {      // Return error on seek to negative stream position      return false;    } else {      // Seek backwards/forwards in the stream      seekStart(pos);      if (_source != null)        return true;      else        return false;    }  }  private class StreamWriter extends Writer    implements EnclosedWriteStream, FlushBuffer {    public final void write(char ch)      throws IOException    {      WriteStream.this.print(ch);    }    public final void write(char []buffer, int offset, int length)      throws IOException    {      WriteStream.this.print(buffer, offset, length);    }    public final void write(char []buffer)      throws IOException    {      WriteStream.this.print(buffer, 0, buffer.length);    }    public final void write(String string)      throws IOException    {      WriteStream.this.print(string);    }    public final void write(String string, int off, int len)      throws IOException    {      WriteStream.this.print(string, off, len);    }    public final void flush()      throws IOException    {      WriteStream.this.flush();    }    public final void flushBuffer()      throws IOException    {      WriteStream.this.flushBuffer();    }      public final void close()      throws IOException    {      // XXX: if flush, then servlets are sloooow      // WriteStream.this.flush();    }    public WriteStream getWriteStream()    {      return WriteStream.this;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?