comparison src/org/tmatesoft/hg/internal/DataAccessProvider.java @ 295:981f9f50bb6c

Issue 11: Error log facility. SessionContext to share common facilities
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 16 Sep 2011 05:35:32 +0200
parents b413b16d10a5
children 3cfa4d908fc9
comparison
equal deleted inserted replaced
294:32890bab7209 295:981f9f50bb6c
22 import java.nio.ByteBuffer; 22 import java.nio.ByteBuffer;
23 import java.nio.MappedByteBuffer; 23 import java.nio.MappedByteBuffer;
24 import java.nio.channels.FileChannel; 24 import java.nio.channels.FileChannel;
25 25
26 import org.tmatesoft.hg.core.HgBadStateException; 26 import org.tmatesoft.hg.core.HgBadStateException;
27 import org.tmatesoft.hg.core.SessionContext;
27 28
28 /** 29 /**
29 * 30 *
30 * @author Artem Tikhomirov 31 * @author Artem Tikhomirov
31 * @author TMate Software Ltd. 32 * @author TMate Software Ltd.
32 */ 33 */
33 public class DataAccessProvider { 34 public class DataAccessProvider {
34 35
35 private final int mapioMagicBoundary; 36 private final int mapioMagicBoundary;
36 private final int bufferSize; 37 private final int bufferSize;
37 38 private final SessionContext context;
38 public DataAccessProvider() { 39
39 this(100 * 1024, 8 * 1024); 40 public DataAccessProvider(SessionContext ctx) {
40 } 41 this(ctx, 100 * 1024, 8 * 1024);
41 42 }
42 public DataAccessProvider(int mapioBoundary, int regularBufferSize) { 43
44 public DataAccessProvider(SessionContext ctx, int mapioBoundary, int regularBufferSize) {
45 context = ctx;
43 mapioMagicBoundary = mapioBoundary; 46 mapioMagicBoundary = mapioBoundary;
44 bufferSize = regularBufferSize; 47 bufferSize = regularBufferSize;
45 } 48 }
46 49
47 public DataAccess create(File f) { 50 public DataAccess create(File f) {
65 // TESTS: bufferSize of 100 was used to check buffer underflow states when readBytes reads chunks bigger than bufSize 68 // TESTS: bufferSize of 100 was used to check buffer underflow states when readBytes reads chunks bigger than bufSize
66 return new FileAccess(fc, flen, bufferSize, useDirectBuffer); 69 return new FileAccess(fc, flen, bufferSize, useDirectBuffer);
67 } 70 }
68 } catch (IOException ex) { 71 } catch (IOException ex) {
69 // unlikely to happen, we've made sure file exists. 72 // unlikely to happen, we've made sure file exists.
70 ex.printStackTrace(); // FIXME log error 73 context.getLog().error(getClass(), ex, null);
71 } 74 }
72 return new DataAccess(); // non-null, empty. 75 return new DataAccess(); // non-null, empty.
73 } 76 }
74 77
75 // DOESN'T WORK YET 78 // DOESN'T WORK YET
175 buffer = null; 178 buffer = null;
176 if (fileChannel != null) { 179 if (fileChannel != null) {
177 try { 180 try {
178 fileChannel.close(); 181 fileChannel.close();
179 } catch (IOException ex) { 182 } catch (IOException ex) {
180 ex.printStackTrace(); // log debug 183 StreamLogFacility.newDefault().debug(getClass(), ex, null);
181 } 184 }
182 fileChannel = null; 185 fileChannel = null;
183 } 186 }
184 } 187 }
185 } 188 }
296 } 299 }
297 if (fileChannel != null) { 300 if (fileChannel != null) {
298 try { 301 try {
299 fileChannel.close(); 302 fileChannel.close();
300 } catch (IOException ex) { 303 } catch (IOException ex) {
301 ex.printStackTrace(); // log debug 304 StreamLogFacility.newDefault().debug(getClass(), ex, null);
302 } 305 }
303 fileChannel = null; 306 fileChannel = null;
304 } 307 }
305 } 308 }
306 } 309 }