📄 httpoutputstream.java
字号:
package servlet.http;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Observer;
import javax.servlet.ServletOutputStream;
// Referenced classes of package servlet.http:
// HttpDate
public class HttpOutputStream extends ServletOutputStream
{
public HttpOutputStream(int i)
{
closeOnClose = false;
doContentLengthCheck = true;
buf = new byte[i];
bufferSize = i;
}
public HttpOutputStream()
{
this(4096);
}
public void init(OutputStream outputstream)
throws IOException
{
initNewBuffer(outputstream, true);
}
public void initNewBuffer(OutputStream outputstream, boolean flag)
throws IOException
{
out = outputstream;
if(flag)
buf = new byte[bufferSize];
}
public void init(OutputStream outputstream, boolean flag)
throws IOException
{
init(outputstream);
limit = -1;
closeOnClose = true;
}
public void next()
{
count = 0;
total = 0;
limit = -1;
length = -1;
obs = null;
committed = false;
countedLength = 0;
closeOnClose = false;
}
public void finish()
throws IOException
{
if(length == -1 && countedLength != 0)
length = countedLength;
flush();
}
public void resets()
{
out = null;
obs = null;
doContentLengthCheck = true;
closeOnClose = false;
}
public int getTotal()
{
return total;
}
public void setContentLength(int i)
{
if(i < 0)
{
throw new IllegalArgumentException("invalid content length");
}
else
{
length = i;
return;
}
}
public void unsetContentLength()
{
doContentLengthCheck = false;
}
public int getContentLength()
{
return length;
}
public void setObserver(Observer observer)
{
obs = observer;
limit = -1;
}
public void setIOException(IOException ioexception)
{
except = ioexception;
limit = -1;
}
public boolean isCommitted()
{
return committed;
}
protected void check(int i)
throws IOException
{
if(except != null)
throw except;
if(limit < 0)
{
if(total > 0)
throw new InternalError();
limit = 0x7fffffff;
if(except != null)
{
limit = -1;
throw except;
}
if(length != -1 && length < 0x7fffffff - total)
limit = total + length;
}
if(doContentLengthCheck && total + i > limit)
{
limit = -1;
throw except = new IOException("tried to write more than content length");
}
else
{
return;
}
}
public void write(int i)
throws IOException
{
if(total >= limit)
check(1);
if(count == buf.length)
flushBytes();
buf[count++] = (byte)i;
total++;
countedLength++;
}
public void write(byte abyte0[], int i, int j)
throws IOException
{
if(j < 0)
throw new IllegalArgumentException();
if(total + j > limit)
check(j);
if(j >= buf.length)
{
flushBytes();
total += j;
writeOut(abyte0, i, j);
return;
}
int k = buf.length - count;
if(j > k)
flushBytes();
System.arraycopy(abyte0, i, buf, count, j);
count += j;
total += j;
countedLength += j;
}
public void flush()
throws IOException
{
if(limit < 0)
check(0);
flushBytes();
}
protected void flushBytes()
throws IOException
{
if(!committed && obs != null)
obs.update(null, this);
committed = true;
if(count > 0)
{
writeOut(buf, 0, count);
out.flush();
count = 0;
}
}
public void print(String s)
throws IOException
{
int i = s.length();
if(total + i > limit)
check(i);
int j = 0;
while(i > 0)
{
int k = buf.length - count;
if(k == 0)
{
flushBytes();
k = buf.length - count;
}
if(k > i)
k = i;
s.getBytes(j, j + k, buf, count);
count += k;
total += k;
j += k;
i -= k;
countedLength += k;
}
}
public void print(HttpDate httpdate)
throws IOException
{
int i = HttpDate.DATELEN;
if(total + i > limit)
check(i);
int j = buf.length - count;
if(j < i)
{
flushBytes();
j = buf.length - count;
}
i = httpdate.getBytes(buf, count, j);
count += i;
total += i;
}
public void close()
throws IOException
{
finish();
if(closeOnClose)
{
try
{
out.close();
}
catch(Exception ex) {}
closeOnClose = false;
}
}
public void setHeader(byte abyte0[])
throws IOException
{
writeOut(abyte0, 0, abyte0.length);
}
public void setHeader(byte abyte0[], int i, int j)
throws IOException
{
writeOut(abyte0, i, j);
}
public OutputStream getRawOutputStream()
{
return out;
}
public void writeRaw(byte abyte0[], int i, int j)
throws IOException
{
if(!committed && obs != null)
obs.update(null, this);
committed = true;
out.write(abyte0, i, j);
}
protected void writeOut(byte abyte0[], int i, int j)
throws IOException
{
out.write(abyte0, i, j);
}
protected OutputStream out;
protected byte buf[];
protected int count;
protected int total;
protected int limit;
protected int length;
protected Observer obs;
protected IOException except;
protected boolean committed;
protected int countedLength;
private int bufferSize;
private boolean closeOnClose;
private boolean doContentLengthCheck;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -