dew/src/main/java/org/apidesign/bck2brwsr/dew/MemoryFileObject.java
author tzezula
Wed, 02 Oct 2013 21:00:24 +0200
changeset 1324 263482b074e9
child 1326 8ae6a6c42b5f
permissions -rw-r--r--
ClassLoaderFileManager
     1 /*
     2  * To change this license header, choose License Headers in Project Properties.
     3  * To change this template file, choose Tools | Templates
     4  * and open the template in the editor.
     5  */
     6 
     7 package org.apidesign.bck2brwsr.dew;
     8 
     9 import java.io.ByteArrayInputStream;
    10 import java.io.ByteArrayOutputStream;
    11 import java.io.IOException;
    12 import java.io.InputStream;
    13 import java.io.InputStreamReader;
    14 import java.io.OutputStream;
    15 import java.io.OutputStreamWriter;
    16 import java.io.Reader;
    17 import java.io.Writer;
    18 
    19 /**
    20  *
    21  * @author Tomas Zeuzla
    22  */
    23 class MemoryFileObject extends BaseFileObject {
    24 
    25     private byte[] content;
    26     private long lastModified;
    27 
    28     MemoryFileObject (
    29             String resourceName,
    30             Kind kind,
    31             byte[] content) {
    32         super(resourceName, kind);
    33         this.content = content;
    34         this.lastModified = this.content == null ?
    35             -1 :
    36             System.currentTimeMillis();
    37     }
    38 
    39     MemoryFileObject (
    40             String resourceName,
    41             byte[] content) {
    42         this(resourceName, getKind(resourceName) ,content);
    43     }
    44 
    45 
    46     @Override
    47     public InputStream openInputStream() throws IOException {
    48         if (content == null) {
    49             throw new IOException();
    50         } else {
    51             return new ByteArrayInputStream(content);
    52         }
    53     }
    54 
    55     @Override
    56     public OutputStream openOutputStream() throws IOException {
    57         return new CloseStream();
    58     }
    59 
    60     @Override
    61     public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
    62         return new InputStreamReader(openInputStream());
    63     }
    64 
    65     @Override
    66     public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    67         if (content == null) {
    68             throw new IOException();
    69         } else {
    70             return new String(content);
    71         }
    72     }
    73 
    74     @Override
    75     public Writer openWriter() throws IOException {
    76         return new OutputStreamWriter(openOutputStream());
    77     }
    78 
    79     @Override
    80     public long getLastModified() {
    81         return lastModified;
    82     }
    83 
    84     @Override
    85     public boolean delete() {
    86         return false;
    87     }
    88 
    89     byte[] getContent() {
    90         return content;
    91     }
    92 
    93     private class CloseStream extends OutputStream {
    94 
    95         private final ByteArrayOutputStream delegate;
    96 
    97         CloseStream() {
    98             delegate = new ByteArrayOutputStream();
    99         }
   100 
   101         @Override
   102         public void write(int b) throws IOException {
   103             delegate.write(b);
   104         }
   105 
   106         @Override
   107         public void write(byte[] b) throws IOException {
   108             delegate.write(b);
   109         }
   110 
   111         @Override
   112         public void write(byte[] b, int off, int len) throws IOException {
   113             delegate.write(b, off, len);
   114         }
   115 
   116         @Override
   117         public void close() throws IOException {
   118             delegate.close();
   119             content = delegate.toByteArray();
   120             lastModified = System.currentTimeMillis();
   121         }                                    
   122 
   123     }
   124 
   125 }