001: package vicazh.hyperpool.stream.net.http;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: public class ExplorerStream extends ServerStream {
007: public ExplorerStream() {
008: }
009:
010: ExplorerStream(Session session, OutputStream outputstream) {
011: super (session, outputstream);
012: }
013:
014: private OutputStream stream;
015:
016: private long skip;
017:
018: public void header() throws IOException {
019: super .header();
020: File f = ((FileStream) session.getClient()).localFile;
021: if (session.getClient().getMethod() == null
022: || !session.getClient().getMethod().equalsIgnoreCase(
023: "get") || (f.exists() && !f.isFile()))
024: return;
025: if (f.exists())
026: ((FileService) connection.element).remove(f);
027: try {
028: if (getCode() == ServerStream.OK) {
029: File d = f;
030: while (!(d = d.getParentFile()).exists()
031: && !new File(((FileService) connection.element)
032: .dir2path()).equals(d))
033: ;
034: if (d.isFile()) {
035: ((FileService) connection.element).remove(d);
036: d.delete();
037: }
038: f.getParentFile().mkdirs();
039: stream = new BufferedOutputStream(new FileOutputStream(
040: f));
041: } else {
042: String s = (String) getFields().get("content-range");
043: if (s != null && s.startsWith("bytes")) {
044: StringTokenizer st = new StringTokenizer(s);
045: st.nextToken();
046: long l = Long.parseLong(st.nextToken("-").trim());
047: if (l <= f.length()) {
048: stream = new BufferedOutputStream(
049: new FileOutputStream(f, true));
050: skip = f.length() - l;
051: return;
052: }
053: }
054: f.setLastModified(new Date().getTime());
055: ((ExplorerService) connection.element).add(f);
056: }
057: } catch (Exception e) {
058: }
059: }
060:
061: public void content(int i) throws IOException {
062: if (stream != null)
063: if (skip > 0)
064: skip--;
065: else
066: stream.write(i);
067: super .content(i);
068: }
069:
070: public void flush() throws IOException {
071: if (stream != null)
072: stream.flush();
073: super .flush();
074: }
075:
076: public void end() {
077: if (stream != null) {
078: try {
079: stream.flush();
080: } catch (Exception e) {
081: }
082: try {
083: stream.close();
084: } catch (Exception e) {
085: }
086: File lf = ((FileStream) session.getClient()).localFile;
087: if (((FileService) connection.element).checkPut(lf)) {
088: File f = ((ExplorerService) connection.element)
089: .getBase(lf);
090: if (getCode() == ServerStream.OK) {
091: File d = f;
092: while (!(d = d.getParentFile()).exists())
093: ;
094: if (d.isFile())
095: d.delete();
096: Object key = getFields().get("transfer-tncoding");
097: if (key != null
098: && getFields().get(key).equalsIgnoreCase(
099: "chunked"))
100: getFields().remove(key);
101: try {
102: ((ExplorerService) connection.element).writer
103: .putObject(session, f);
104: } catch (Exception e) {
105: }
106: } else if (f.exists())
107: f.setLastModified(new Date().getTime());
108: ((ExplorerService) connection.element).add(lf);
109: ((FileService) connection.element).pack();
110: } else
111: ((FileService) connection.element).delpath(lf);
112: }
113: super.end();
114: }
115: }
|