Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/DataAccessProvider.java @ 534:243202f1bda5
Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Mon, 04 Feb 2013 18:00:55 +0100 |
| parents | 2743641f2f12 |
| children | dd4f6311af52 |
comparison
equal
deleted
inserted
replaced
| 533:e6f72c9829a6 | 534:243202f1bda5 |
|---|---|
| 19 import static org.tmatesoft.hg.util.LogFacility.Severity.Error; | 19 import static org.tmatesoft.hg.util.LogFacility.Severity.Error; |
| 20 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; | 20 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; |
| 21 | 21 |
| 22 import java.io.File; | 22 import java.io.File; |
| 23 import java.io.FileInputStream; | 23 import java.io.FileInputStream; |
| 24 import java.io.FileNotFoundException; | |
| 25 import java.io.FileOutputStream; | |
| 24 import java.io.IOException; | 26 import java.io.IOException; |
| 27 import java.io.OutputStream; | |
| 25 import java.nio.ByteBuffer; | 28 import java.nio.ByteBuffer; |
| 26 import java.nio.MappedByteBuffer; | 29 import java.nio.MappedByteBuffer; |
| 27 import java.nio.channels.FileChannel; | 30 import java.nio.channels.FileChannel; |
| 28 | 31 |
| 29 import org.tmatesoft.hg.core.SessionContext; | 32 import org.tmatesoft.hg.core.SessionContext; |
| 70 // ensure contract of CFG_PROPERTY_MAPIO_LIMIT, for mapioBoundary == 0 use MAX_VALUE so that no file is memmap-ed | 73 // ensure contract of CFG_PROPERTY_MAPIO_LIMIT, for mapioBoundary == 0 use MAX_VALUE so that no file is memmap-ed |
| 71 private static int mapioBoundaryValue(int mapioBoundary) { | 74 private static int mapioBoundaryValue(int mapioBoundary) { |
| 72 return mapioBoundary == 0 ? Integer.MAX_VALUE : mapioBoundary; | 75 return mapioBoundary == 0 ? Integer.MAX_VALUE : mapioBoundary; |
| 73 } | 76 } |
| 74 | 77 |
| 75 public DataAccess create(File f) { | 78 public DataAccess createReader(File f) { |
| 76 if (!f.exists()) { | 79 if (!f.exists()) { |
| 77 return new DataAccess(); | 80 return new DataAccess(); |
| 78 } | 81 } |
| 79 try { | 82 try { |
| 80 FileChannel fc = new FileInputStream(f).getChannel(); | 83 FileChannel fc = new FileInputStream(f).getChannel(); |
| 93 } catch (IOException ex) { | 96 } catch (IOException ex) { |
| 94 // unlikely to happen, we've made sure file exists. | 97 // unlikely to happen, we've made sure file exists. |
| 95 context.getLog().dump(getClass(), Error, ex, null); | 98 context.getLog().dump(getClass(), Error, ex, null); |
| 96 } | 99 } |
| 97 return new DataAccess(); // non-null, empty. | 100 return new DataAccess(); // non-null, empty. |
| 101 } | |
| 102 | |
| 103 public DataSerializer createWriter(File f, boolean createNewIfDoesntExist) { | |
| 104 if (!f.exists() && !createNewIfDoesntExist) { | |
| 105 return new DataSerializer(); | |
| 106 } | |
| 107 try { | |
| 108 return new StreamDataSerializer(context.getLog(), new FileOutputStream(f)); | |
| 109 } catch (final FileNotFoundException ex) { | |
| 110 context.getLog().dump(getClass(), Error, ex, null); | |
| 111 return new DataSerializer() { | |
| 112 public void write(byte[] data, int offset, int length) throws IOException { | |
| 113 throw ex; | |
| 114 } | |
| 115 }; | |
| 116 } | |
| 98 } | 117 } |
| 99 | 118 |
| 100 private static class MemoryMapFileAccess extends DataAccess { | 119 private static class MemoryMapFileAccess extends DataAccess { |
| 101 private FileChannel fileChannel; | 120 private FileChannel fileChannel; |
| 102 private long position = 0; // always points to buffer's absolute position in the file | 121 private long position = 0; // always points to buffer's absolute position in the file |
| 372 } | 391 } |
| 373 fileChannel = null; | 392 fileChannel = null; |
| 374 } | 393 } |
| 375 } | 394 } |
| 376 } | 395 } |
| 396 | |
| 397 public/*XXX, private, once HgCloneCommand stops using it */ static class StreamDataSerializer extends DataSerializer { | |
| 398 private final OutputStream out; | |
| 399 private final LogFacility log; | |
| 400 private byte[] buffer; | |
| 401 | |
| 402 public StreamDataSerializer(LogFacility logFacility, OutputStream os) { | |
| 403 assert os != null; | |
| 404 out = os; | |
| 405 log = logFacility; | |
| 406 } | |
| 407 | |
| 408 @Override | |
| 409 public void write(byte[] data, int offset, int length) throws IOException { | |
| 410 out.write(data, offset, length); | |
| 411 } | |
| 412 | |
| 413 @Override | |
| 414 public void writeInt(int... values) throws IOException { | |
| 415 ensureBufferSize(4*values.length); // sizeof(int) | |
| 416 int idx = 0; | |
| 417 for (int v : values) { | |
| 418 DataSerializer.bigEndian(v, buffer, idx); | |
| 419 idx += 4; | |
| 420 } | |
| 421 out.write(buffer, 0, idx); | |
| 422 } | |
| 423 | |
| 424 @Override | |
| 425 public void writeByte(byte... values) throws IOException { | |
| 426 if (values.length == 1) { | |
| 427 out.write(values[0]); | |
| 428 } else { | |
| 429 out.write(values, 0, values.length); | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 private void ensureBufferSize(int bytesNeeded) { | |
| 434 if (buffer == null || buffer.length < bytesNeeded) { | |
| 435 buffer = new byte[bytesNeeded]; | |
| 436 } | |
| 437 } | |
| 438 | |
| 439 @Override | |
| 440 public void done() { | |
| 441 try { | |
| 442 out.flush(); | |
| 443 out.close(); | |
| 444 } catch (IOException ex) { | |
| 445 log.dump(getClass(), Error, ex, "Failure to close stream"); | |
| 446 } | |
| 447 } | |
| 448 } | |
| 377 } | 449 } |
