Coverage Report - org.ini4j.IniFile
 
Classes in this File Line Coverage Branch Coverage Complexity
IniFile
100%
31/31
90%
9/10
0
IniFile$Mode
100%
4/4
N/A
0
 
 1  
 /*
 2  
  * Copyright 2005,2009 Ivan SZKIBA
 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.ini4j;
 17  
 
 18  
 import java.io.File;
 19  
 import java.io.FileWriter;
 20  
 
 21  
 import java.util.prefs.BackingStoreException;
 22  
 
 23  
 public class IniFile extends IniPreferences
 24  
 {
 25  4
     public static enum Mode
 26  
     {
 27  1
         RO,
 28  1
         WO,
 29  1
         RW
 30  
     }
 31  
 
 32  
     private final File _file;
 33  
     private final Mode _mode;
 34  
 
 35  
     public IniFile(File file) throws BackingStoreException
 36  
     {
 37  3
         this(file, Mode.RO);
 38  3
     }
 39  
 
 40  
     public IniFile(File file, Mode mode) throws BackingStoreException
 41  
     {
 42  8
         super(new Ini());
 43  8
         _file = file;
 44  8
         _mode = mode;
 45  8
         if ((_mode == Mode.RO) || ((_mode != Mode.WO) && _file.exists()))
 46  
         {
 47  6
             sync();
 48  
         }
 49  7
     }
 50  
 
 51  
     public File getFile()
 52  
     {
 53  1
         return _file;
 54  
     }
 55  
 
 56  
     public Mode getMode()
 57  
     {
 58  1
         return _mode;
 59  
     }
 60  
 
 61  
     @Override public void flush() throws BackingStoreException
 62  
     {
 63  3
         if (_mode == Mode.RO)
 64  
         {
 65  1
             throw new BackingStoreException("read only instance");
 66  
         }
 67  
 
 68  
         try
 69  
         {
 70  2
             synchronized (lock)
 71  
             {
 72  2
                 FileWriter writer = new FileWriter(_file);
 73  
 
 74  
                 try
 75  
                 {
 76  1
                     getIni().store(writer);
 77  
                 }
 78  
                 finally
 79  
                 {
 80  1
                     writer.close();
 81  1
                 }
 82  1
             }
 83  
         }
 84  1
         catch (Exception x)
 85  
         {
 86  1
             throw new BackingStoreException(x);
 87  1
         }
 88  1
     }
 89  
 
 90  
     @Override public void sync() throws BackingStoreException
 91  
     {
 92  8
         if (_mode == Mode.WO)
 93  
         {
 94  1
             throw new BackingStoreException("write only instance");
 95  
         }
 96  
 
 97  
         try
 98  
         {
 99  7
             synchronized (lock)
 100  
             {
 101  7
                 getIni().load(_file.toURI().toURL());
 102  6
             }
 103  
         }
 104  1
         catch (Exception x)
 105  
         {
 106  1
             throw new BackingStoreException(x);
 107  6
         }
 108  6
     }
 109  
 }