Using buffered boundary stream in multipart parser. But currently not working
This commit is contained in:
parent
53dec4603c
commit
077963ae28
11 changed files with 198 additions and 152 deletions
|
|
@ -216,9 +216,9 @@ public class BufferedBoundaryInputStream extends FilterInputStream{
|
|||
}
|
||||
|
||||
/**
|
||||
* @return if current position in the buffer is a boundary
|
||||
* @return if there is more data to read
|
||||
*/
|
||||
public boolean isBoundary(){
|
||||
public boolean hasNext(){
|
||||
return bound_pos == buf_pos;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -89,11 +89,23 @@ public class IOUtil {
|
|||
}
|
||||
|
||||
/**
|
||||
* Copies all data from the input stream to the output stream.
|
||||
* The input stream will not be closed after method has returned.
|
||||
* Copies all data from one InputStream to another OutputStream.
|
||||
* The streams will not be closed after method has returned.
|
||||
*/
|
||||
public static void copyStream(InputStream in, OutputStream out) throws IOException {
|
||||
byte[] buff = new byte[256];
|
||||
byte[] buff = new byte[8192]; // This is the default BufferedInputStream buffer size
|
||||
int len;
|
||||
while((len = in.read(buff)) > 0){
|
||||
out.write(buff, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all data from one Reader to another Writer.
|
||||
* The streams will not be closed after method has returned.
|
||||
*/
|
||||
public static void copyStream(Reader in, Writer out) throws IOException {
|
||||
char[] buff = new char[8192]; // This is the default BufferedReader buffer size
|
||||
int len;
|
||||
while((len = in.read(buff)) > 0){
|
||||
out.write(buff, 0, len);
|
||||
|
|
|
|||
20
src/zutil/io/NullOutputStream.java
Executable file
20
src/zutil/io/NullOutputStream.java
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
package zutil.io;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* An OutputStream that does nothing but discard data. Similar to /dev/null
|
||||
*
|
||||
* Created by Ziver on 2016-07-08.
|
||||
*/
|
||||
public class NullOutputStream extends OutputStream {
|
||||
|
||||
@Override
|
||||
public void write(int b) { }
|
||||
|
||||
@Override
|
||||
public void write(byte b[]) { }
|
||||
|
||||
@Override
|
||||
public void write(byte b[], int off, int len) { }
|
||||
}
|
||||
41
src/zutil/io/NullWriter.java
Executable file
41
src/zutil/io/NullWriter.java
Executable file
|
|
@ -0,0 +1,41 @@
|
|||
package zutil.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* An Writer that does nothing but discard data. Similar to /dev/null
|
||||
*
|
||||
* Created by Ziver on 2016-07-08.
|
||||
*/
|
||||
public class NullWriter extends Writer{
|
||||
|
||||
@Override
|
||||
public void write(int c) { }
|
||||
@Override
|
||||
public void write(char cbuf[]) { }
|
||||
@Override
|
||||
public void write(char cbuf[], int off, int len) { }
|
||||
@Override
|
||||
public void write(String str) throws IOException { }
|
||||
@Override
|
||||
public void write(String str, int off, int len) { }
|
||||
|
||||
@Override
|
||||
public Writer append(CharSequence csq) {
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Writer append(CharSequence csq, int start, int end) {
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Writer append(char c) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() { }
|
||||
@Override
|
||||
public void close() { };
|
||||
}
|
||||
|
|
@ -24,8 +24,11 @@
|
|||
|
||||
package zutil.net.http.multipart;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import zutil.io.IOUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -34,14 +37,14 @@ import java.io.IOException;
|
|||
* @author Ziver
|
||||
*/
|
||||
public class MultipartFileField implements MultipartField{
|
||||
/** This is the temporary directory for the received files */
|
||||
private File tempDir;
|
||||
protected String filename;
|
||||
protected File file;
|
||||
|
||||
private String fieldname;
|
||||
private String filename;
|
||||
private InputStream in;
|
||||
|
||||
protected MultipartFileField() throws IOException {
|
||||
this.file = createTempFile();
|
||||
|
||||
protected MultipartFileField(Map<String,String> header, BufferedReader in) throws IOException {
|
||||
this.fieldname = header.get("fieldname");
|
||||
this.filename = header.get("filename");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -52,51 +55,28 @@ public class MultipartFileField implements MultipartField{
|
|||
}
|
||||
|
||||
/**
|
||||
* @return the specific file name for this field.
|
||||
* @return the field name
|
||||
*/
|
||||
public String getName(){
|
||||
return filename;
|
||||
return fieldname;
|
||||
}
|
||||
|
||||
public String getFilename(){
|
||||
return filename;
|
||||
}
|
||||
|
||||
public InputStream getInputStream(){
|
||||
return in;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the File class that points to the received file
|
||||
*/
|
||||
public File getFile(){
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the received file.
|
||||
* Reads in all data and save it into the specified file
|
||||
*
|
||||
* @param newFile is the new location to move the file to
|
||||
* @return if the move was successful
|
||||
* @param file is the new file where the data will be stored
|
||||
*/
|
||||
public boolean moveFile(File newFile){
|
||||
boolean success = file.renameTo(newFile);
|
||||
if(success)
|
||||
file = newFile;
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a temporary file in either the system
|
||||
* temporary folder or by the setTempDir() function
|
||||
*
|
||||
* @return the temporary file
|
||||
*/
|
||||
protected File createTempFile() throws IOException {
|
||||
if(tempDir != null)
|
||||
return File.createTempFile("upload", ".part", tempDir.getAbsoluteFile());
|
||||
else
|
||||
return File.createTempFile("upload", ".part");
|
||||
}
|
||||
|
||||
|
||||
public void setTempDir(File dir){
|
||||
if(!dir.isDirectory())
|
||||
throw new RuntimeException("\""+dir.getAbsolutePath()+"\" is not a directory!");
|
||||
if(!dir.canWrite())
|
||||
throw new RuntimeException("\""+dir.getAbsolutePath()+"\" is not writable!");
|
||||
tempDir = dir;
|
||||
public void saveToFile(File file) throws IOException {
|
||||
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
|
||||
IOUtil.copyStream(in, out);
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,17 +24,17 @@
|
|||
|
||||
package zutil.net.http.multipart;
|
||||
|
||||
import zutil.ProgressListener;
|
||||
import zutil.io.BufferedBoundaryInputStream;
|
||||
import zutil.io.IOUtil;
|
||||
import zutil.io.NullWriter;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.http.HttpHeader;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -57,23 +57,23 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
/** The length of the HTTP Body */
|
||||
private long contentLength;
|
||||
/** This is the input stream */
|
||||
private BufferedReader in;
|
||||
private InputStream in;
|
||||
|
||||
private MultiPartIterator iterator;
|
||||
|
||||
|
||||
public MultipartParser(BufferedReader in, String delimiter, long length){
|
||||
public MultipartParser(InputStream in, String delimiter, long length){
|
||||
this.in = in;
|
||||
this.delimiter = delimiter;
|
||||
this.contentLength = length;
|
||||
}
|
||||
public MultipartParser(BufferedReader in, HttpHeader header){
|
||||
public MultipartParser(InputStream in, HttpHeader header){
|
||||
this(in,
|
||||
parseDelimiter(header.getHeader("Content-type")),
|
||||
Long.parseLong( header.getHeader("Content-Length")));
|
||||
}
|
||||
public MultipartParser(HttpServletRequest req) throws IOException {
|
||||
this(req.getReader(),
|
||||
this(req.getInputStream(),
|
||||
parseDelimiter(req.getHeader("Content-type")),
|
||||
req.getContentLength());
|
||||
}
|
||||
|
|
@ -101,56 +101,39 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
|
||||
|
||||
protected class MultiPartIterator implements Iterator<MultipartField>{
|
||||
private String currentLine;
|
||||
private boolean endOfStream;
|
||||
private BufferedBoundaryInputStream boundaryIn;
|
||||
private BufferedReader buffIn;
|
||||
private HttpHeaderParser parser;
|
||||
|
||||
|
||||
protected MultiPartIterator(){
|
||||
this.endOfStream = false;
|
||||
this.parser = new HttpHeaderParser(in);
|
||||
this.boundaryIn = new BufferedBoundaryInputStream(in);
|
||||
this.buffIn = new BufferedReader(new InputStreamReader(boundaryIn));
|
||||
this.parser = new HttpHeaderParser(buffIn);
|
||||
this.parser.setReadStatusLine(false);
|
||||
|
||||
this.boundaryIn.setBoundary("--"+delimiter);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
try{
|
||||
findDelimiter();
|
||||
if (endOfStream)
|
||||
return false;
|
||||
return true;
|
||||
try {
|
||||
IOUtil.copyStream(buffIn, new NullWriter());
|
||||
return boundaryIn.hasNext();
|
||||
} catch (IOException e){
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
endOfStream = true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private void findDelimiter() throws IOException {
|
||||
if (endOfStream || matchesDelimiter(currentLine))
|
||||
return;
|
||||
while ((currentLine=in.readLine()) != null) {
|
||||
if (matchesDelimiter(currentLine))
|
||||
break;
|
||||
}
|
||||
}
|
||||
private boolean matchesDelimiter(String match){
|
||||
if (match == null)
|
||||
return false;
|
||||
else if (match.equals("--" + delimiter + "--")) {
|
||||
endOfStream = true;
|
||||
return true;
|
||||
}
|
||||
else if (match.equals("--" + delimiter))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MultipartField next() {
|
||||
if (!hasNext())
|
||||
return null;
|
||||
try {
|
||||
boundaryIn.next(); // Skip current boundary
|
||||
HttpHeader header = parser.read();
|
||||
String disposition = header.getHeader(HEADER_CONTENT_DISPOSITION);
|
||||
if (disposition != null){
|
||||
|
|
@ -158,16 +141,16 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
HttpHeaderParser.parseHeaderValue(map, disposition);
|
||||
if (map.containsKey("form-data")){
|
||||
if (map.containsKey("filename")){
|
||||
MultipartFileField field = new MultipartFileField();
|
||||
MultipartFileField field = new MultipartFileField(map, buffIn);
|
||||
return field;
|
||||
}
|
||||
else{
|
||||
MultipartStringField field = new MultipartStringField(map.get("name"));
|
||||
MultipartStringField field = new MultipartStringField(map, buffIn);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.warning("Only form-data is supported");
|
||||
logger.warning("Only multipart form-data is supported");
|
||||
return this.next(); // find next field
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
package zutil.net.http.multipart;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import zutil.io.IOUtil;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2016-07-06.
|
||||
|
|
@ -10,8 +15,9 @@ public class MultipartStringField implements MultipartField {
|
|||
private String name;
|
||||
private String value;
|
||||
|
||||
protected MultipartStringField(String name){
|
||||
this.name = name;
|
||||
protected MultipartStringField(Map<String,String> header, BufferedReader in) throws IOException {
|
||||
this.name = header.get("name");
|
||||
value = IOUtil.getContentAsString(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ import java.util.NoSuchElementException;
|
|||
/**
|
||||
* This class is a first in first out circular buffer with a fixed size.
|
||||
* If the size is exceed then the oldest item will be removed.
|
||||
*
|
||||
* Created by Ziver on 2015-09-22.
|
||||
*/
|
||||
public class CircularBuffer<T> implements Iterable<T>{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue