001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.internal.delta;
013:
014: import java.nio.ByteBuffer;
015:
016: import org.tmatesoft.svn.core.io.diff.SVNDiffInstruction;
017:
018: /**
019: * @version 1.1.1
020: * @author TMate Software Ltd.
021: */
022: public abstract class SVNDeltaAlgorithm {
023:
024: private ByteBuffer myNewData;
025: private ByteBuffer myData;
026: private int myNewDataLength;
027: private int myInstructionsLength;
028: private SVNDiffInstruction myTemplateInstruction;
029:
030: public SVNDeltaAlgorithm() {
031: myNewData = ByteBuffer.allocate(1024);
032: myData = ByteBuffer.allocate(2048);
033: myTemplateInstruction = new SVNDiffInstruction(0, 0, 0);
034: }
035:
036: public void reset() {
037: myNewData.clear();
038: myData.clear();
039: myInstructionsLength = 0;
040: myNewDataLength = 0;
041: }
042:
043: public abstract void computeDelta(byte[] a, int aLength, byte[] b,
044: int bLength);
045:
046: public ByteBuffer getData() {
047: if (myNewData.position() > 0) {
048: myData = ensureBufferSize(myData, myNewData.position());
049: myData.put(myNewData.array(), 0, myNewData.position());
050: myNewData.clear();
051: }
052: myData.flip();
053: return myData;
054: }
055:
056: public int getInstructionsLength() {
057: return myInstructionsLength;
058: }
059:
060: public int getNewDataLength() {
061: return myNewDataLength;
062: }
063:
064: protected void copyFromSource(int position, int length) {
065: myTemplateInstruction.type = SVNDiffInstruction.COPY_FROM_SOURCE;
066: myTemplateInstruction.offset = position;
067: myTemplateInstruction.length = length;
068: myData = ensureBufferSize(myData, 10);
069: myTemplateInstruction.writeTo(myData);
070: myInstructionsLength = myData.position();
071: }
072:
073: protected void copyFromTarget(int position, int length) {
074: myTemplateInstruction.type = SVNDiffInstruction.COPY_FROM_TARGET;
075: myTemplateInstruction.offset = position;
076: myTemplateInstruction.length = length;
077: myData = ensureBufferSize(myData, 10);
078: myTemplateInstruction.writeTo(myData);
079: myInstructionsLength = myData.position();
080: }
081:
082: protected void copyFromNewData(byte[] data, int offset, int length) {
083: myTemplateInstruction.type = SVNDiffInstruction.COPY_FROM_NEW_DATA;
084: myTemplateInstruction.offset = 0;
085: myTemplateInstruction.length = length;
086: myData = ensureBufferSize(myData, 10);
087: myTemplateInstruction.writeTo(myData);
088: myInstructionsLength = myData.position();
089: myNewData = ensureBufferSize(myNewData, length);
090: myNewData.put(data, offset, length);
091: myNewDataLength += length;
092: }
093:
094: private static ByteBuffer ensureBufferSize(ByteBuffer buffer,
095: int size) {
096: if (buffer.remaining() < size) {
097: ByteBuffer newBuffer = ByteBuffer.allocate((buffer
098: .position() + size) * 3 / 2);
099: newBuffer.put(buffer.array(), 0, buffer.position());
100: buffer = newBuffer;
101: }
102: return buffer;
103: }
104: }
|