use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class InvReplTest method testReplacement.
@Test
public void testReplacement() throws IllegalAccessException, InstantiationException, NoSuchMethodException, IOException {
InvokeProxetta proxetta = initProxetta();
String className = One.class.getCanonicalName();
byte[] klazz = proxetta.builder(One.class).create();
//FileUtil.writeBytes("/Users/igor/OneClone.class", klazz);
FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
// PrintStream out = System.out;
System.setOut(new PrintStream(fbaos));
One one = (One) ClassLoaderUtil.defineClass((new StringBuilder()).append(className).append(JoddProxetta.invokeProxyClassNameSuffix).toString(), klazz).newInstance();
// clone ctor calls super ctor,
assertEquals("one ctor!one ctor!", fbaos.toString());
fbaos.reset();
one.example1();
assertEquals("REPLACED VIRTUAL! jodd.proxetta.inv.Two * one!173>overriden sub", fbaos.toString());
fbaos.reset();
one.example2();
assertEquals("REPLACED STATIC! one * jodd/proxetta/inv/Two * example2 * void example2() * jodd.proxetta.inv.One * jodd.proxetta.inv.One$$Clonetou!15013static: 4", fbaos.toString());
fbaos.reset();
one.example3();
assertEquals("state = REPLACED ctor!", fbaos.toString());
fbaos.reset();
assertEquals("jodd.proxetta.inv.One$$Clonetou", one.getClass().getName());
assertTrue(one instanceof Serializable);
Annotation[] anns = one.getClass().getAnnotations();
assertEquals(3, anns.length);
Method ms = one.getClass().getMethod("example1");
anns = ms.getAnnotations();
assertEquals(1, anns.length);
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class MultipartRequestInputStream method readBoundary.
/**
* Reads boundary from the input stream.
*/
public byte[] readBoundary() throws IOException {
FastByteArrayOutputStream boundaryOutput = new FastByteArrayOutputStream();
byte b;
// skip optional whitespaces
while ((b = readByte()) <= ' ') {
}
boundaryOutput.write(b);
// now read boundary chars
while ((b = readByte()) != '\r') {
boundaryOutput.write(b);
}
if (boundaryOutput.size() == 0) {
throw new IOException("Problems with parsing request: invalid boundary");
}
skipBytes(1);
boundary = new byte[boundaryOutput.size() + 2];
System.arraycopy(boundaryOutput.toByteArray(), 0, boundary, 2, boundary.length - 2);
boundary[0] = '\r';
boundary[1] = '\n';
return boundary;
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class MultipartStreamParser method parseRequestStream.
/**
* Extracts uploaded files and parameters from the request data.
*/
public void parseRequestStream(InputStream inputStream, String encoding) throws IOException {
setParsed();
MultipartRequestInputStream input = new MultipartRequestInputStream(inputStream);
input.readBoundary();
while (true) {
FileUploadHeader header = input.readDataHeader(encoding);
if (header == null) {
break;
}
if (header.isFile) {
String fileName = header.fileName;
if (fileName.length() > 0) {
if (header.contentType.indexOf("application/x-macbinary") > 0) {
input.skipBytes(128);
}
}
FileUpload newFile = fileUploadFactory.create(input);
newFile.processStream();
if (fileName.length() == 0) {
// file was specified, but no name was provided, therefore it was not uploaded
if (newFile.getSize() == 0) {
newFile.size = -1;
}
}
putFile(header.formFieldName, newFile);
} else {
// no file, therefore it is regular form parameter.
FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
input.copyAll(fbos);
String value = encoding != null ? new String(fbos.toByteArray(), encoding) : new String(fbos.toByteArray());
putParameter(header.formFieldName, value);
}
input.skipBytes(1);
input.mark(1);
// read byte, but may be end of stream
int nextByte = input.read();
if (nextByte == -1 || nextByte == '-') {
input.reset();
break;
}
input.reset();
}
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class ObjectUtil method cloneViaSerialization.
/**
* Create object copy using serialization mechanism.
*/
public static <T extends Serializable> T cloneViaSerialization(T obj) throws IOException, ClassNotFoundException {
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
ObjectOutputStream out = null;
ObjectInputStream in = null;
Object objCopy = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(obj);
out.flush();
byte[] bytes = bos.toByteArray();
in = new ObjectInputStream(new ByteArrayInputStream(bytes));
objCopy = in.readObject();
} finally {
StreamUtil.close(out);
StreamUtil.close(in);
}
return (T) objCopy;
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class ReceivedEmail method processPart.
/**
* Process single part of received message. All parts are simple added to the message, i.e. hierarchy is not saved.
*/
protected void processPart(ReceivedEmail email, Part part) throws IOException, MessagingException {
Object content = part.getContent();
if (content instanceof String) {
String stringContent = (String) content;
String disposition = part.getDisposition();
if (disposition != null && disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
String contentType = part.getContentType();
String mimeType = EmailUtil.extractMimeType(contentType);
String encoding = EmailUtil.extractEncoding(contentType);
String fileName = part.getFileName();
String contentId = (part instanceof MimePart) ? ((MimePart) part).getContentID() : null;
if (encoding == null) {
encoding = StringPool.US_ASCII;
}
email.addAttachment(fileName, mimeType, contentId, stringContent.getBytes(encoding));
} else {
String contentType = part.getContentType();
String encoding = EmailUtil.extractEncoding(contentType);
String mimeType = EmailUtil.extractMimeType(contentType);
if (encoding == null) {
encoding = StringPool.US_ASCII;
}
email.addMessage(stringContent, mimeType, encoding);
}
} else if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
int count = mp.getCount();
for (int i = 0; i < count; i++) {
Part innerPart = mp.getBodyPart(i);
processPart(email, innerPart);
}
} else if (content instanceof InputStream) {
String fileName = EmailUtil.resolveFileName(part);
String contentId = (part instanceof MimePart) ? ((MimePart) part).getContentID() : null;
String mimeType = EmailUtil.extractMimeType(part.getContentType());
InputStream is = (InputStream) content;
FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
StreamUtil.copy(is, fbaos);
email.addAttachment(fileName, mimeType, contentId, fbaos.toByteArray());
} else if (content instanceof MimeMessage) {
MimeMessage mimeMessage = (MimeMessage) content;
addAttachmentMessage(new ReceivedEmail(mimeMessage));
} else {
String fileName = part.getFileName();
String contentId = (part instanceof MimePart) ? ((MimePart) part).getContentID() : null;
String mimeType = EmailUtil.extractMimeType(part.getContentType());
InputStream is = part.getInputStream();
FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
StreamUtil.copy(is, fbaos);
StreamUtil.close(is);
email.addAttachment(fileName, mimeType, contentId, fbaos.toByteArray());
}
}
Aggregations