use of java.io.PushbackInputStream in project cxf by apache.
the class AttachmentDeserializer method initializeRootMessage.
protected void initializeRootMessage() throws IOException {
contentType = (String) message.get(Message.CONTENT_TYPE);
if (contentType == null) {
throw new IllegalStateException("Content-Type can not be empty!");
}
if (message.getContent(InputStream.class) == null) {
throw new IllegalStateException("An InputStream must be provided!");
}
if (AttachmentUtil.isTypeSupported(contentType.toLowerCase(), supportedTypes)) {
String boundaryString = findBoundaryFromContentType(contentType);
if (null == boundaryString) {
boundaryString = findBoundaryFromInputStream();
}
// If a boundary still wasn't found, throw an exception
if (null == boundaryString) {
throw new IOException("Couldn't determine the boundary from the message!");
}
boundary = boundaryString.getBytes("utf-8");
stream = new PushbackInputStream(message.getContent(InputStream.class), pbAmount);
if (!readTillFirstBoundary(stream, boundary)) {
throw new IOException("Couldn't find MIME boundary: " + boundaryString);
}
Map<String, List<String>> ih = loadPartHeaders(stream);
message.put(ATTACHMENT_PART_HEADERS, ih);
String val = AttachmentUtil.getHeader(ih, "Content-Type", "; ");
if (!StringUtils.isEmpty(val)) {
String cs = HttpHeaderHelper.findCharset(val);
if (!StringUtils.isEmpty(cs)) {
message.put(Message.ENCODING, HttpHeaderHelper.mapCharset(cs));
}
}
val = AttachmentUtil.getHeader(ih, "Content-Transfer-Encoding");
MimeBodyPartInputStream mmps = new MimeBodyPartInputStream(stream, boundary, pbAmount);
InputStream ins = AttachmentUtil.decode(mmps, val);
if (ins != mmps) {
ih.remove("Content-Transfer-Encoding");
}
body = new DelegatingInputStream(ins, this);
createCount++;
message.setContent(InputStream.class, body);
}
}
use of java.io.PushbackInputStream in project cxf by apache.
the class AttachmentDeserializer method findBoundaryFromInputStream.
private String findBoundaryFromInputStream() throws IOException {
InputStream is = message.getContent(InputStream.class);
// boundary should definitely be in the first 2K;
PushbackInputStream in = new PushbackInputStream(is, 4096);
byte[] buf = new byte[2048];
int i = in.read(buf);
int len = i;
while (i > 0 && len < buf.length) {
i = in.read(buf, len, buf.length - len);
if (i > 0) {
len += i;
}
}
String msg = IOUtils.newStringFromBytes(buf, 0, len);
in.unread(buf, 0, len);
// Reset the input stream since we'll need it again later
message.setContent(InputStream.class, in);
// Use regex to get the boundary and return null if it's not found
Matcher m = INPUT_STREAM_BOUNDARY_PATTERN.matcher(msg);
return m.find() ? "--" + m.group(1) : null;
}
use of java.io.PushbackInputStream in project cxf by apache.
the class AttachmentDeserializerTest method testSmallStream.
@Test
public void testSmallStream() throws Exception {
byte[] messageBytes = ("------=_Part_1\n\nJJJJ\n------=_Part_1\n\n" + "Content-Transfer-Encoding: binary\n\n=3D=3D=3D\n------=_Part_1\n").getBytes();
PushbackInputStream pushbackStream = new PushbackInputStream(new ByteArrayInputStream(messageBytes), 2048);
pushbackStream.read(new byte[4096], 0, 4015);
pushbackStream.unread(messageBytes);
pushbackStream.read(new byte[72]);
MimeBodyPartInputStream m = new MimeBodyPartInputStream(pushbackStream, "------=_Part_1".getBytes(), 2048);
assertEquals(10, m.read(new byte[1000]));
assertEquals(-1, m.read(new byte[1000]));
assertEquals(-1, m.read(new byte[1000]));
m.close();
}
use of java.io.PushbackInputStream in project cxf by apache.
the class IOUtilsTest method testNonEmptyWithPushBack.
@Test
public void testNonEmptyWithPushBack() throws Exception {
InputStream is = new PushbackInputStream(new ByteArrayInputStream("Hello".getBytes()));
assertFalse(IOUtils.isEmpty(is));
assertEquals("Hello", IOUtils.toString(is));
}
use of java.io.PushbackInputStream in project cxf by apache.
the class IOUtils method isEmpty.
public static boolean isEmpty(InputStream is) throws IOException {
if (is == null) {
return true;
}
// if available is 0 it does not mean it is empty
if (is.available() > 0) {
return false;
}
final byte[] bytes = new byte[1];
if (is.markSupported()) {
is.mark(1);
try {
return isEof(is.read(bytes));
} finally {
is.reset();
}
}
if (!(is instanceof PushbackInputStream)) {
return false;
}
// it may be an attachment stream
PushbackInputStream pbStream = (PushbackInputStream) is;
boolean isEmpty = isEof(pbStream.read(bytes));
if (!isEmpty) {
pbStream.unread(bytes);
}
return isEmpty;
}
Aggregations