Coverage Report - org.mockftpserver.fake.filesystem.FileEntry
 
Classes in this File Line Coverage Branch Coverage Complexity
FileEntry
95%
40/42
100%
12/12
1.692
 
 1  
 /*
 2  
  * Copyright 2008 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.mockftpserver.fake.filesystem;
 17  
 
 18  
 import java.io.ByteArrayInputStream;
 19  
 import java.io.ByteArrayOutputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.io.OutputStream;
 23  
 
 24  
 /**
 25  
  * File system entry representing a file
 26  
  *
 27  
  * @author Chris Mair
 28  
  * @version $Revision: 219 $ - $Date: 2009-02-07 21:58:49 -0500 (Sat, 07 Feb 2009) $
 29  
  */
 30  
 public class FileEntry extends AbstractFileSystemEntry {
 31  
 
 32  1
     private static final byte[] EMPTY = new byte[0];
 33  
 
 34  299
     private byte[] bytes = EMPTY;
 35  
     private ByteArrayOutputStream out;
 36  
 
 37  
     /**
 38  
      * Construct a new instance without setting its path
 39  
      */
 40  108
     public FileEntry() {
 41  108
     }
 42  
 
 43  
     /**
 44  
      * Construct a new instance with the specified value for its path
 45  
      *
 46  
      * @param path - the value for path
 47  
      */
 48  
     public FileEntry(String path) {
 49  104
         super(path);
 50  104
     }
 51  
 
 52  
     /**
 53  
      * Construct a new instance with the specified path and file contents
 54  
      *
 55  
      * @param path     - the value for path
 56  
      * @param contents - the contents of the file, as a String
 57  
      */
 58  
     public FileEntry(String path, String contents) {
 59  87
         super(path);
 60  87
         setContents(contents);
 61  87
     }
 62  
 
 63  
     /**
 64  
      * Return false to indicate that this entry represents a file
 65  
      *
 66  
      * @return false
 67  
      */
 68  
     public boolean isDirectory() {
 69  71
         return false;
 70  
     }
 71  
 
 72  
     /**
 73  
      * Return the size of this file
 74  
      *
 75  
      * @return the file size in bytes
 76  
      */
 77  
     public long getSize() {
 78  48
         return getCurrentBytes().length;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Set the contents of the file represented by this entry
 83  
      *
 84  
      * @param contents - the String whose bytes are used as the contents
 85  
      */
 86  
     public void setContents(String contents) {
 87  149
         byte[] newBytes = (contents != null) ? contents.getBytes() : EMPTY;
 88  149
         setContentsInternal(newBytes);
 89  149
     }
 90  
 
 91  
     /**
 92  
      * Set the contents of the file represented by this entry
 93  
      *
 94  
      * @param contents - the byte[] used as the contents
 95  
      */
 96  
     public void setContents(byte[] contents) {
 97  
         // Copy the bytes[] to guard against subsequent modification of the source array
 98  18
         byte[] newBytes = EMPTY;
 99  18
         if (contents != null) {
 100  17
             newBytes = new byte[contents.length];
 101  17
             System.arraycopy(contents, 0, newBytes, 0, contents.length);
 102  
         }
 103  18
         setContentsInternal(newBytes);
 104  18
     }
 105  
 
 106  
     /**
 107  
      * Create and return an InputStream for reading the contents of the file represented by this entry
 108  
      *
 109  
      * @return an InputStream
 110  
      */
 111  
     public InputStream createInputStream() {
 112  32
         return new ByteArrayInputStream(getCurrentBytes());
 113  
     }
 114  
 
 115  
     /**
 116  
      * Create and return an OutputStream for writing the contents of the file represented by this entry
 117  
      *
 118  
      * @param append - true if the OutputStream should append to any existing contents false if
 119  
      *               any existing contents should be overwritten
 120  
      * @return an OutputStream
 121  
      * @throws FileSystemException - if an error occurs creating or initializing the OutputStream
 122  
      */
 123  
     public OutputStream createOutputStream(boolean append) {
 124  
         // If appending and we already have an OutputStream, then continue to use it
 125  19
         if (append && out != null) {
 126  2
             return out;
 127  
         }
 128  
 
 129  17
         out = new ByteArrayOutputStream();
 130  17
         byte[] initialContents = (append) ? bytes : EMPTY;
 131  
         try {
 132  17
             out.write(initialContents);
 133  
         }
 134  0
         catch (IOException e) {
 135  0
             throw new FileSystemException(getPath(), null, e);
 136  17
         }
 137  17
         return out;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Return a new FileSystemEntry that is a clone of this object, except having the specified path
 142  
      *
 143  
      * @param path - the new path value for the cloned file system entry
 144  
      * @return a new FileSystemEntry that has all the same values as this object except for its path
 145  
      */
 146  
     public FileSystemEntry cloneWithNewPath(String path) {
 147  15
         FileEntry clone = new FileEntry(path);
 148  15
         clone.setLastModified(getLastModified());
 149  15
         clone.setOwner(getOwner());
 150  15
         clone.setGroup(getGroup());
 151  15
         clone.setPermissions(getPermissions());
 152  15
         clone.setContents(getCurrentBytes());
 153  15
         return clone;
 154  
     }
 155  
 
 156  
     //-------------------------------------------------------------------------
 157  
     // Internal Helper Methods
 158  
     //-------------------------------------------------------------------------
 159  
 
 160  
     /**
 161  
      * @return the current contents of this file entry as a byte[]
 162  
      */
 163  
     private byte[] getCurrentBytes() {
 164  95
         return (out != null) ? out.toByteArray() : bytes;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Set the contents of the file represented by this entry
 169  
      *
 170  
      * @param contents - the byte[] used as the contents
 171  
      */
 172  
     private void setContentsInternal(byte[] contents) {
 173  167
         this.bytes = contents;
 174  
 
 175  
         // Get rid of any OutputStream
 176  167
         this.out = null;
 177  167
     }
 178  
 
 179  
     /**
 180  
      * @see java.lang.Object#toString()
 181  
      */
 182  
     public String toString() {
 183  21
         return "File['" + getPath() + "' size=" + getSize() + " lastModified=" + getLastModified() + " owner="
 184  
                 + getOwner() + " group=" + getGroup() + " permissions=" + getPermissions() + "]";
 185  
     }
 186  
 
 187  
 }