Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/KeyHole/src/org/litesoft/aokeyhole/persist/lines/FileLinesPersister.java

Diff revisions: vs.
  @@ -1,15 +1,13 @@
1 1 // This Source Code is in the Public Domain per: http://unlicense.org
2 2 package org.litesoft.aokeyhole.persist.lines;
3 3
4 - import java.io.*;
5 - import java.text.*;
6 -
7 4 import org.litesoft.commonfoundation.typeutils.*;
8 5
6 + import java.io.*;
7 + import java.text.*;
9 8 import java.util.*;
10 9
11 - public class FileLinesPersister implements LinesPersister
12 - {
10 + public class FileLinesPersister implements LinesPersister {
13 11 public static final String SUFFIX_OLD = ".previous";
14 12 public static final String SUFFIX_NEW = ".tmpNew";
15 13
  @@ -19,71 +17,57 @@
19 17 private static final String TIMESTAMP_PREVIOUS = "TimestampPrevious";
20 18
21 19 public FileLinesPersister( String pFileName )
22 - throws IOException
23 - {
20 + throws IOException {
24 21 mFile = new File( pFileName );
25 22
26 - if ( !mFile.exists() || mFile.isDirectory() )
27 - {
23 + if ( !mFile.exists() || mFile.isDirectory() ) {
28 24 throw new FileNotFoundException( pFileName + " Not a File" );
29 25 }
30 - if ( !mFile.canRead() )
31 - {
26 + if ( !mFile.canRead() ) {
32 27 throw new IOException( pFileName + " Not Readable" );
33 28 }
34 29 mName = cleanName( mFile.getName() );
35 30 mReadOnly = !mFile.canWrite();
36 - if ( mName.length() == 0 )
37 - {
31 + if ( mName.length() == 0 ) {
38 32 throw new IOException( pFileName + " does NOT appear to have a File 'Name'" );
39 33 }
40 34 }
41 35
42 - private String cleanName( String pName )
43 - {
36 + private String cleanName( String pName ) {
44 37 pName += '.';
45 38 return pName.substring( 0, pName.indexOf( '.' ) );
46 39 }
47 40
48 41 @Override
49 - public String getName()
50 - {
42 + public String getName() {
51 43 return mName;
52 44 }
53 45
54 46 @Override
55 - public boolean isReadOnly()
56 - {
47 + public boolean isReadOnly() {
57 48 return mReadOnly;
58 49 }
59 50
60 51 @Override
61 52 public String[] loadLines()
62 - throws IOException
63 - {
53 + throws IOException {
64 54 BufferedReader reader = new BufferedReader( new FileReader( mFile ) );
65 55
66 56 List<String> lines = new ArrayList<String>();
67 57
68 58 boolean ok = false;
69 - try
70 - {
71 - for ( String line; null != (line = reader.readLine()); )
72 - {
59 + try {
60 + for ( String line; null != (line = reader.readLine()); ) {
73 61 lines.add( line );
74 62 }
75 63 ok = true;
76 64 }
77 - finally
78 - {
79 - if ( !ok )
80 - {
81 - try
82 - {
65 + finally {
66 + if ( !ok ) {
67 + try {
83 68 reader.close();
84 69 }
85 - catch ( IOException ignore )
86 - {
70 + catch ( IOException ignore ) {
87 71 // Have to eat this because Java provides no mechanism to release the file resources that does NOT throw an exception
88 72 }
89 73 }
  @@ -95,8 +79,7 @@
95 79
96 80 @Override
97 81 public void saveLines( String[] pLines )
98 - throws IOException
99 - {
82 + throws IOException {
100 83 String fPath = mFile.getPath();
101 84 File newFile = new File( fPath + SUFFIX_NEW );
102 85 File oldFile = new File( fPath + timestamp() + SUFFIX_OLD );
  @@ -105,27 +88,21 @@
105 88 roll( newFile, mFile, oldFile );
106 89 }
107 90
108 - private String timestamp()
109 - {
91 + private String timestamp() {
110 92 return System.getProperty( TIMESTAMP_PREVIOUS ) == null ? //
111 93 "" : //
112 94 "." + new SimpleDateFormat( "yyyyMMddHHmmss" ).format( Timestamps.now() );
113 95 }
114 96
115 97 private void writeLines( File pFile, String[] pLines )
116 - throws IOException
117 - {
98 + throws IOException {
118 99 BufferedWriter writer = new BufferedWriter( new FileWriter( pFile ) );
119 100
120 101 boolean ok = false;
121 - try
122 - {
123 - if ( pLines != null )
124 - {
125 - for ( String line : pLines )
126 - {
127 - if ( line != null )
128 - {
102 + try {
103 + if ( pLines != null ) {
104 + for ( String line : pLines ) {
105 + if ( line != null ) {
129 106 writer.write( line );
130 107 writer.newLine();
131 108 }
  @@ -133,16 +110,12 @@
133 110 }
134 111 ok = true;
135 112 }
136 - finally
137 - {
138 - if ( !ok )
139 - {
140 - try
141 - {
113 + finally {
114 + if ( !ok ) {
115 + try {
142 116 writer.close();
143 117 }
144 - catch ( IOException ignore )
145 - {
118 + catch ( IOException ignore ) {
146 119 // Have to eat this because Java provides no mechanism to release the file resources that does NOT throw an exception
147 120 }
148 121 }
  @@ -151,12 +124,9 @@
151 124 }
152 125
153 126 private void roll( File pNewFile, File pCurFile, File pOldFile )
154 - throws IOException
155 - {
156 - if ( pCurFile.exists() )
157 - {
158 - if ( pOldFile.exists() )
159 - {
127 + throws IOException {
128 + if ( pCurFile.exists() ) {
129 + if ( pOldFile.exists() ) {
160 130 remove( pOldFile );
161 131 }
162 132 rename( pCurFile, pOldFile );
  @@ -165,24 +135,18 @@
165 135 }
166 136
167 137 private void rename( File pFileFrom, File pFileTo )
168 - throws IOException
169 - {
170 - if ( pFileFrom.exists() )
171 - {
172 - if ( !pFileFrom.renameTo( pFileTo ) )
173 - {
138 + throws IOException {
139 + if ( pFileFrom.exists() ) {
140 + if ( !pFileFrom.renameTo( pFileTo ) ) {
174 141 throw new IOException( "Unable to rename (" + pFileFrom.getPath() + ") to: " + pFileTo.getPath() );
175 142 }
176 143 }
177 144 }
178 145
179 146 private void remove( File pFileToDel )
180 - throws IOException
181 - {
182 - if ( pFileToDel.exists() )
183 - {
184 - if ( !pFileToDel.delete() )
185 - {
147 + throws IOException {
148 + if ( pFileToDel.exists() ) {
149 + if ( !pFileToDel.delete() ) {
186 150 throw new IOException( "Unable to remove/delete: " + pFileToDel.getPath() );
187 151 }
188 152 }