Coverage Report - palmed.buffer.Buffer

Classes in this Package Line Coverage Branch Coverage Complexity
Buffer
0% 
0% 
2,091

 1  
 /**
 2  
  * Redistribution  and use  in source  and binary  forms, with  or without
 3  
  * modification, are permitted provided  that the following conditions are
 4  
  * met :
 5  
  *
 6  
  * . Redistributions  of  source  code  must  retain  the  above copyright
 7  
  *   notice, this list of conditions and the following disclaimer.
 8  
  *
 9  
  * . Redistributions in  binary form  must reproduce  the above  copyright
 10  
  *   notice, this list of conditions  and the following disclaimer in  the
 11  
  *   documentation and/or other materials provided with the distribution.
 12  
  *
 13  
  * . The name of the author may not be used to endorse or promote products
 14  
  *   derived from this software without specific prior written permission.
 15  
  *
 16  
  * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
 17  
  * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
 18  
  * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
 19  
  * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
 20  
  * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
 21  
  * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
 22  
  * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
 23  
  * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
 24  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 25  
  * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
 26  
  * POSSIBILITY OF SUCH DAMAGE.
 27  
  *
 28  
  * $Id: Buffer.java 409 2006-01-02 08:50:37Z mat007 $
 29  
  */
 30  
 
 31  
 package palmed.buffer;
 32  
 
 33  
 import java.io.ByteArrayOutputStream;
 34  
 import java.io.DataInputStream;
 35  
 import java.io.DataOutputStream;
 36  
 import java.io.IOException;
 37  
 import java.io.InputStream;
 38  
 import java.io.OutputStream;
 39  
 import palmed.edit.ITextBox;
 40  
 import palmed.file.File;
 41  
 import palmed.file.IFile;
 42  
 
 43  
 /**
 44  
  * This class implements an association between a text box content and a file.
 45  
  *
 46  
  * @author Mathieu Champlon
 47  
  * @version $Revision: 409 $ $Date: 2006-01-02 17:50:37 +0900 (lun., 02 janv. 2006) $
 48  
  */
 49  
 public final class Buffer implements IBuffer
 50  
 {
 51  
     /**
 52  
      * The text box.
 53  
      */
 54  
     private final ITextBox textBox_;
 55  
     /**
 56  
      * The file.
 57  
      */
 58  
     private IFile file_;
 59  
 
 60  
     /**
 61  
      * Create an empty buffer.
 62  
      *
 63  
      * @param textBox the text box.
 64  
      */
 65  
     public Buffer( final ITextBox textBox )
 66  0
     {
 67  0
         if( textBox == null )
 68  0
             throw new IllegalArgumentException( "parameter 'textBox' is null" );
 69  0
         textBox_ = textBox;
 70  0
         textBox_.clear();
 71  0
     }
 72  
 
 73  
     /**
 74  
      * {@inheritDoc}
 75  
      */
 76  
     public void open( final IFile file )
 77  
     {
 78  
         try
 79  
         {
 80  0
             final InputStream stream = file.openInputStream();
 81  0
             textBox_.read( stream );
 82  0
             stream.close();
 83  0
             file_ = file;
 84  
         }
 85  0
         catch( IOException e )
 86  
         {
 87  0
             throw new BufferException( e );
 88  0
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
      * {@inheritDoc}
 93  
      */
 94  
     public boolean save()
 95  
     {
 96  0
         if( !hasBeenModified() )
 97  0
             return true;
 98  0
         if( file_ != null )
 99  
         {
 100  0
             save( file_ );
 101  0
             return true;
 102  
         }
 103  0
         return false;
 104  
     }
 105  
 
 106  
     /**
 107  
      * {@inheritDoc}
 108  
      */
 109  
     public void save( final IFile file )
 110  
     {
 111  
         try
 112  
         {
 113  0
             final OutputStream stream = file.openOutputStream();
 114  0
             final ByteArrayOutputStream bufferedStream = new ByteArrayOutputStream();
 115  0
             textBox_.write( bufferedStream );
 116  0
             stream.write( bufferedStream.toByteArray() );
 117  0
             stream.close();
 118  0
             file_ = file;
 119  
         }
 120  0
         catch( IOException e )
 121  
         {
 122  0
             throw new BufferException( e );
 123  0
         }
 124  0
     }
 125  
 
 126  
     /**
 127  
      * {@inheritDoc}
 128  
      */
 129  
     public boolean hasBeenModified()
 130  
     {
 131  0
         return textBox_.hasBeenModified();
 132  
     }
 133  
 
 134  
     /**
 135  
      * {@inheritDoc}
 136  
      */
 137  
     public boolean hasFile( final IFile file )
 138  
     {
 139  0
         return file.equals( file_ );
 140  
     }
 141  
 
 142  
     /**
 143  
      * {@inheritDoc}
 144  
      */
 145  
     public void marshall( final OutputStream stream ) throws IOException
 146  
     {
 147  0
         writeFile( stream );
 148  0
         textBox_.marshall( stream );
 149  0
     }
 150  
 
 151  
     private void writeFile( final OutputStream stream ) throws IOException
 152  
     {
 153  0
         final DataOutputStream output = new DataOutputStream( stream );
 154  0
         if( file_ == null )
 155  0
             output.writeUTF( "" );
 156  
         else
 157  0
             output.writeUTF( file_.toString() );
 158  0
     }
 159  
 
 160  
     /**
 161  
      * {@inheritDoc}
 162  
      */
 163  
     public void unmarshall( final InputStream stream ) throws IOException
 164  
     {
 165  0
         readFile( stream );
 166  0
         textBox_.unmarshall( stream );
 167  0
     }
 168  
 
 169  
     private void readFile( final InputStream stream ) throws IOException
 170  
     {
 171  0
         final DataInputStream input = new DataInputStream( stream );
 172  0
         final String path = input.readUTF();
 173  0
         if( !path.equals( "" ) )
 174  0
             file_ = new File( path ); // FIXME remove dependency
 175  0
     }
 176  
 
 177  
     /**
 178  
      * {@inheritDoc}
 179  
      */
 180  
     public void delete()
 181  
     {
 182  0
         textBox_.delete();
 183  0
     }
 184  
 }