dew/src/main/java/org/apidesign/bck2brwsr/dew/MemoryFileObject.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 10:03:22 +0200
branchjavac
changeset 1327 217ca48c5b80
parent 1324 263482b074e9
permissions -rw-r--r--
Bringing in Tomas's no java.io.File file manager
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 
    19 package org.apidesign.bck2brwsr.dew;
    20 
    21 import java.io.ByteArrayInputStream;
    22 import java.io.ByteArrayOutputStream;
    23 import java.io.IOException;
    24 import java.io.InputStream;
    25 import java.io.InputStreamReader;
    26 import java.io.OutputStream;
    27 import java.io.OutputStreamWriter;
    28 import java.io.Reader;
    29 import java.io.Writer;
    30 
    31 /**
    32  *
    33  * @author Tomas Zeuzla
    34  */
    35 class MemoryFileObject extends BaseFileObject {
    36 
    37     private byte[] content;
    38     private long lastModified;
    39 
    40     MemoryFileObject (
    41             String resourceName,
    42             Kind kind,
    43             byte[] content) {
    44         super(resourceName, kind);
    45         this.content = content;
    46         this.lastModified = this.content == null ?
    47             -1 :
    48             System.currentTimeMillis();
    49     }
    50 
    51     MemoryFileObject (
    52             String resourceName,
    53             byte[] content) {
    54         this(resourceName, getKind(resourceName) ,content);
    55     }
    56 
    57 
    58     @Override
    59     public InputStream openInputStream() throws IOException {
    60         if (content == null) {
    61             throw new IOException();
    62         } else {
    63             return new ByteArrayInputStream(content);
    64         }
    65     }
    66 
    67     @Override
    68     public OutputStream openOutputStream() throws IOException {
    69         return new CloseStream();
    70     }
    71 
    72     @Override
    73     public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
    74         return new InputStreamReader(openInputStream());
    75     }
    76 
    77     @Override
    78     public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    79         if (content == null) {
    80             throw new IOException();
    81         } else {
    82             return new String(content);
    83         }
    84     }
    85 
    86     @Override
    87     public Writer openWriter() throws IOException {
    88         return new OutputStreamWriter(openOutputStream());
    89     }
    90 
    91     @Override
    92     public long getLastModified() {
    93         return lastModified;
    94     }
    95 
    96     @Override
    97     public boolean delete() {
    98         return false;
    99     }
   100 
   101     byte[] getContent() {
   102         return content;
   103     }
   104 
   105     private class CloseStream extends OutputStream {
   106 
   107         private final ByteArrayOutputStream delegate;
   108 
   109         CloseStream() {
   110             delegate = new ByteArrayOutputStream();
   111         }
   112 
   113         @Override
   114         public void write(int b) throws IOException {
   115             delegate.write(b);
   116         }
   117 
   118         @Override
   119         public void write(byte[] b) throws IOException {
   120             delegate.write(b);
   121         }
   122 
   123         @Override
   124         public void write(byte[] b, int off, int len) throws IOException {
   125             delegate.write(b, off, len);
   126         }
   127 
   128         @Override
   129         public void close() throws IOException {
   130             delegate.close();
   131             content = delegate.toByteArray();
   132             lastModified = System.currentTimeMillis();
   133         }                                    
   134 
   135     }
   136 
   137 }