📄 wcsmimemultipartstreamparser.java
字号:
/**
*
*/
package com.esri.solutions.jitk.datasources.ogc.wcs.parsing.version110;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.esri.solutions.jitk.datasources.exceptions.ResponseParsingException;
import com.esri.solutions.jitk.datasources.ogc.wcs.IOAgent;
/**
* @author vlad2928
*
*/
public class WCSMimeMultipartStreamParser {
private byte[] buf;
private List<WCSMimePartDescriptor> parts;
public WCSMimeMultipartStreamParser() {
parts = new ArrayList<WCSMimePartDescriptor>();
buf = null;
}
public void parse(InputStream is) throws ResponseParsingException {
try {
if(is != null) {
buf = IOAgent.readIntoByteArray(is);
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf)));
String s = null;
WCSMimePartDescriptor part = null;
boolean isHeader = false;
int bodyLength=0;
for(int i=0; (s = reader.readLine()) != null; i += s.length() + 1) {
if(s.startsWith("--")) {
isHeader = true;
part = new WCSMimePartDescriptor(s.replaceAll("-", ""));
} else if(isHeader) {
if(s.equals("")) {
isHeader = false;
part.setBodyOffset(i+1);
bodyLength = 0;
for(int j=part.getBodyOffset(); j < buf.length - 3; j++) {
if(buf[j] == 10) {
if(buf[j+1] == 45 && buf[j+2] == 45) {
if(this.parts.size() > 0) {
byte[] separator = new byte[part.getName().length() + 3];
java.lang.System.arraycopy(buf, j+3, separator, 0, part.getName().length());
separator[separator.length - 3] = 45;
separator[separator.length - 2] = 45;
separator[separator.length - 1] = 10;
if(new String(separator).equals(part.getName() + "--\n")) break;
} else {
byte[] separator = new byte[part.getName().length() + 1];
java.lang.System.arraycopy(buf, j+3, separator, 0, part.getName().length());
separator[separator.length - 1] = 10;
if(new String(separator).equals(part.getName() + "\n")) break;
}
}
}
bodyLength++;
}
part.setBodyLength(bodyLength);
this.parts.add(part);
i += bodyLength;
reader.skip(bodyLength);
} else {
String[] field = s.split(":");
if(field.length == 2) {
String fieldName = field[0].trim();
String fieldValue = field[1].trim();
part.addField(fieldName, fieldValue);
}
}
}
}
} else {
throw new ResponseParsingException("WCSMimeMultipartStreamParser error.");
}
} catch(IOException e) {
throw new ResponseParsingException("WCSMimeMultipartStreamParser error. " + e.getMessage());
}
}
public List<WCSMimePartDescriptor> getParts() {
return this.parts;
}
public InputStream getPartInputStream(WCSMimePartDescriptor part) {
return new ByteArrayInputStream(buf, part.getBodyOffset(), part.getBodyLength());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -