📄 multipartstream.java
字号:
* 读取当前包中的<code>header-part</code>
*
* @return 返回当前包中的<code>header-part</code>。
*/
public String readHeaders()
throws MalformedStreamException {
int i = 0;
byte b[] = new byte[1];
// to support multi-byte characters
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int sizeMax = HEADER_PART_SIZE_MAX;
int size = 0;
while (i < 4) {
try {
b[0] = readByte();
} catch (IOException e) {
throw new MalformedStreamException("Stream ended unexpectedly");
}
size++;
if (b[0] == HEADER_SEPARATOR[i]) {
i++;
} else {
i = 0;
}
if (size <= sizeMax) {
baos.write(b[0]);
}
}
String headers = null;
if (headerEncoding != null) {
try {
headers = baos.toString(headerEncoding);
} catch (UnsupportedEncodingException e) {
headers = baos.toString();
}
} else {
headers = baos.toString();
}
return headers;
}
/**
* <p>从当前<code>encapsulation</code>中读取<code>body-data</code>,
* 并写入输出缓冲 。
*
* @param output 数据写入的缓冲
*
* @return 被写入的数据数量
*/
public int readBodyData(OutputStream output)
throws MalformedStreamException,
IOException {
boolean done = false;
int pad;
int pos;
int bytesRead;
int total = 0;
while (!done) {
pos = findSeparator();
if (pos != -1) {
output.write(buffer, head, pos - head);
total += pos - head;
head = pos;
done = true;
} else {
if (tail - head > keepRegion) {
pad = keepRegion;
} else {
pad = tail - head;
}
output.write(buffer, head, tail - head - pad);
total += tail - head - pad;
System.arraycopy(buffer, tail - pad, buffer, 0, pad);
// 用新数据重新填充缓冲
head = 0;
bytesRead = input.read(buffer, pad, bufSize - pad);
// [pprrrrrrr]
if (bytesRead != -1) {
tail = pad + bytesRead;
} else {
output.write(buffer, 0, pad);
output.flush();
total += pad;
throw new MalformedStreamException(
"Stream ended unexpectedly");
}
}
}
output.flush();
return total;
}
/**
* <p>从当前<code>encapsulation</code>中读取<code>body-data</code>,
* 然后将其抛弃。
*
* <p>调用这个方法可以跳过不需要的数据。
*
* @return 被抛弃的数据数量
*/
public int discardBodyData()
throws MalformedStreamException,
IOException {
boolean done = false;
int pad;
int pos;
int bytesRead;
int total = 0;
while (!done) {
pos = findSeparator();
if (pos != -1) {
total += pos - head;
head = pos;
done = true;
} else {
// 决定有多少的数据需要保存在缓冲中
// buffer.
if (tail - head > keepRegion) {
pad = keepRegion;
} else {
pad = tail - head;
}
total += tail - head - pad;
// 把数据移动到缓冲的开始
System.arraycopy(buffer, tail - pad, buffer, 0, pad);
// 以新数据填充缓冲
head = 0;
bytesRead = input.read(buffer, pad, bufSize - pad);
// [pprrrrrrr]
if (bytesRead != -1) {
tail = pad + bytesRead;
} else {
total += pad;
throw new MalformedStreamException(
"Stream ended unexpectedly");
}
}
}
return total;
}
/**
* 查找第一个包的开始
* <p>
* @return 如果找到包返回<code>true</code>,否则返回false。
*/
public boolean skipPreamble()
throws IOException {
System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2);
boundaryLength = boundary.length - 2;
try {
// 放弃所有的一直到定界符的数据
discardBodyData();
// 读取边界,如果函数执行成功true
return readBoundary();
} catch (MalformedStreamException e) {
return false;
} finally {
// 恢复定界符
System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2);
boundaryLength = boundary.length;
boundary[0] = 0x0D;
boundary[1] = 0x0A;
}
}
/**
* 比较两个字节数组串是否相等。
*
* @param a 第一个要比较的字节数组。
* @param b 第二个要比较的字节数组。
* @param count 要比较的字节数。
*
* @return <code>true</code> 如果两个字节数组相等,
* 否则返回<code>false</code>。
*/
public static boolean arrayequals(byte[] a,
byte[] b,
int count) {
for (int i = 0; i < count; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
/**
* 在缓冲中查找一个字节值
*
* @param value 要查找的字节值
* @param pos 开始查找的位置
*
* @return 如果找到了则返回该字节的位置,否则返回-1
*/
protected int findByte(byte value,
int pos) {
for (int i = pos; i < tail; i++) {
if (buffer[i] == value) {
return i;
}
}
return -1;
}
/**
* 在缓冲中查找边界
*
* @return 如果找到边界,则返回边界的位置,否则返回-1
*/
protected int findSeparator() {
int first;
int match = 0;
int maxpos = tail - boundaryLength;
for (first = head;
(first <= maxpos) && (match != boundaryLength);
first++) {
first = findByte(boundary[0], first);
if (first == -1 || (first > maxpos)) {
return -1;
}
for (match = 1; match < boundaryLength; match++) {
if (buffer[first + match] != boundary[match]) {
break;
}
}
}
if (match == boundaryLength) {
return first - 1;
}
return -1;
}
/**
* 返回表示该对象的字符串。
*
* @return 以字符串表示的对象。
*/
public String toString() {
StringBuffer sbTemp = new StringBuffer();
sbTemp.append("boundary='");
sbTemp.append(String.valueOf(boundary));
sbTemp.append("'\nbufSize=");
sbTemp.append(bufSize);
return sbTemp.toString();
}
/**
* 当输入流中的格式不能被识别时,触发该异常。
*/
public class MalformedStreamException
extends IOException {
public MalformedStreamException() {
super();
}
public MalformedStreamException(String message) {
super(message);
}
}
/**
* 当尝试设置一个无效的边界标志时,将产生该异常。
*/
public class IllegalBoundaryException
extends IOException {
/**
* 构造函数
*/
public IllegalBoundaryException() {
super();
}
public IllegalBoundaryException(String message) {
super(message);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -