dew/src/main/java/org/apidesign/bck2brwsr/dew/MemoryFileObject.java
changeset 1324 263482b074e9
child 1326 8ae6a6c42b5f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dew/src/main/java/org/apidesign/bck2brwsr/dew/MemoryFileObject.java	Wed Oct 02 21:00:24 2013 +0200
     1.3 @@ -0,0 +1,125 @@
     1.4 +/*
     1.5 + * To change this license header, choose License Headers in Project Properties.
     1.6 + * To change this template file, choose Tools | Templates
     1.7 + * and open the template in the editor.
     1.8 + */
     1.9 +
    1.10 +package org.apidesign.bck2brwsr.dew;
    1.11 +
    1.12 +import java.io.ByteArrayInputStream;
    1.13 +import java.io.ByteArrayOutputStream;
    1.14 +import java.io.IOException;
    1.15 +import java.io.InputStream;
    1.16 +import java.io.InputStreamReader;
    1.17 +import java.io.OutputStream;
    1.18 +import java.io.OutputStreamWriter;
    1.19 +import java.io.Reader;
    1.20 +import java.io.Writer;
    1.21 +
    1.22 +/**
    1.23 + *
    1.24 + * @author Tomas Zeuzla
    1.25 + */
    1.26 +class MemoryFileObject extends BaseFileObject {
    1.27 +
    1.28 +    private byte[] content;
    1.29 +    private long lastModified;
    1.30 +
    1.31 +    MemoryFileObject (
    1.32 +            String resourceName,
    1.33 +            Kind kind,
    1.34 +            byte[] content) {
    1.35 +        super(resourceName, kind);
    1.36 +        this.content = content;
    1.37 +        this.lastModified = this.content == null ?
    1.38 +            -1 :
    1.39 +            System.currentTimeMillis();
    1.40 +    }
    1.41 +
    1.42 +    MemoryFileObject (
    1.43 +            String resourceName,
    1.44 +            byte[] content) {
    1.45 +        this(resourceName, getKind(resourceName) ,content);
    1.46 +    }
    1.47 +
    1.48 +
    1.49 +    @Override
    1.50 +    public InputStream openInputStream() throws IOException {
    1.51 +        if (content == null) {
    1.52 +            throw new IOException();
    1.53 +        } else {
    1.54 +            return new ByteArrayInputStream(content);
    1.55 +        }
    1.56 +    }
    1.57 +
    1.58 +    @Override
    1.59 +    public OutputStream openOutputStream() throws IOException {
    1.60 +        return new CloseStream();
    1.61 +    }
    1.62 +
    1.63 +    @Override
    1.64 +    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
    1.65 +        return new InputStreamReader(openInputStream());
    1.66 +    }
    1.67 +
    1.68 +    @Override
    1.69 +    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    1.70 +        if (content == null) {
    1.71 +            throw new IOException();
    1.72 +        } else {
    1.73 +            return new String(content);
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    @Override
    1.78 +    public Writer openWriter() throws IOException {
    1.79 +        return new OutputStreamWriter(openOutputStream());
    1.80 +    }
    1.81 +
    1.82 +    @Override
    1.83 +    public long getLastModified() {
    1.84 +        return lastModified;
    1.85 +    }
    1.86 +
    1.87 +    @Override
    1.88 +    public boolean delete() {
    1.89 +        return false;
    1.90 +    }
    1.91 +
    1.92 +    byte[] getContent() {
    1.93 +        return content;
    1.94 +    }
    1.95 +
    1.96 +    private class CloseStream extends OutputStream {
    1.97 +
    1.98 +        private final ByteArrayOutputStream delegate;
    1.99 +
   1.100 +        CloseStream() {
   1.101 +            delegate = new ByteArrayOutputStream();
   1.102 +        }
   1.103 +
   1.104 +        @Override
   1.105 +        public void write(int b) throws IOException {
   1.106 +            delegate.write(b);
   1.107 +        }
   1.108 +
   1.109 +        @Override
   1.110 +        public void write(byte[] b) throws IOException {
   1.111 +            delegate.write(b);
   1.112 +        }
   1.113 +
   1.114 +        @Override
   1.115 +        public void write(byte[] b, int off, int len) throws IOException {
   1.116 +            delegate.write(b, off, len);
   1.117 +        }
   1.118 +
   1.119 +        @Override
   1.120 +        public void close() throws IOException {
   1.121 +            delegate.close();
   1.122 +            content = delegate.toByteArray();
   1.123 +            lastModified = System.currentTimeMillis();
   1.124 +        }                                    
   1.125 +
   1.126 +    }
   1.127 +
   1.128 +}