Subversion Repository Public Repository

litesoft

Diff Revisions 72 vs 73 for /trunk/Java/core/Server/src/org/litesoft/util/FileUtils.java

Diff revisions: vs.
  @@ -108,7 +108,7 @@
108 108 long fSize = pFile.length();
109 109 if ( fSize > pMaxAllowedSize )
110 110 {
111 - throw new FileSystemException( "File Too large (" + fSize + "), but max set to: " + pMaxAllowedSize );
111 + throw new FileSystemException( "File (" + pFile + ") Too large (" + fSize + "), but max set to: " + pMaxAllowedSize );
112 112 }
113 113 int fileSize = (int) fSize;
114 114 byte[] b = new byte[fileSize];
  @@ -116,17 +116,22 @@
116 116 boolean closed = false;
117 117 try
118 118 {
119 - int i, offset = 0;
120 - while ( 0 <= (i = is.read( b, offset, fileSize - offset )) )
119 + int offset = 0;
120 + while ( offset < fileSize )
121 121 {
122 + int i = is.read( b, offset, fileSize - offset );
123 + if ( i < 1 )
124 + {
125 + throw new FileSystemException( "Unable to read the entire file (" + pFile + "). Expected " + fileSize + ", but only got: " + offset );
126 + }
122 127 offset += i;
123 128 }
124 - closed = true;
125 - is.close();
126 - if ( offset != fileSize )
129 + if ( -1 != is.read() )
127 130 {
128 - throw new FileSystemException( "Unable to read the entire file. Expected " + fileSize + ", but only got: " + offset );
131 + throw new FileSystemException( "Read beyond file (" + pFile + ") size (" + fileSize + ")!" );
129 132 }
133 + closed = true;
134 + is.close();
130 135 }
131 136 finally
132 137 {
  @@ -151,6 +156,39 @@
151 156 addLines( file, true, pLines );
152 157 }
153 158
159 + public static void store( File pFile, byte[] pBytes )
160 + throws FileSystemException
161 + {
162 + Utils.assertNotNull( "File", pFile );
163 + File file = new File( pFile.getAbsolutePath() + ".new" );
164 + try
165 + {
166 + OutputStream os = new FileOutputStream( file );
167 + boolean closed = false;
168 + try
169 + {
170 + if ( (pBytes != null) && (pBytes.length != 0) )
171 + {
172 + os.write( pBytes );
173 + }
174 + closed = true;
175 + os.close();
176 + }
177 + finally
178 + {
179 + if ( !closed )
180 + {
181 + Utils.dispose( os );
182 + }
183 + }
184 + }
185 + catch ( IOException e )
186 + {
187 + throw new FileSystemException( e );
188 + }
189 + rollIn( file, pFile, new File( pFile.getAbsolutePath() + ".bak" ) );
190 + }
191 +
154 192 public static void storeTextFile( File pFile, String... pLines )
155 193 throws FileSystemException
156 194 {