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.io.diff;
013:
014: import java.io.File;
015: import java.io.InputStream;
016: import java.io.OutputStream;
017: import java.security.MessageDigest;
018:
019: import org.tmatesoft.svn.core.SVNException;
020: import org.tmatesoft.svn.core.internal.wc.SVNFileUtil;
021:
022: /**
023: * The <b>SVNDiffWindowApplyBaton</b> class is used to provide the source
024: * and target streams during applying diff windows. Also an instance of
025: * <b>SVNDiffWindowApplyBaton</b> may be supplied with an MD5 digest object
026: * for on-the-fly updating it with the bytes of the target view. So that when
027: * a diff window's instructions are applied, the digest will be the checksum
028: * for the full expanded text written to the target stream during delta application.
029: *
030: * @version 1.1.1
031: * @author TMate Software Ltd.
032: */
033: public class SVNDiffWindowApplyBaton {
034:
035: InputStream mySourceStream;
036: OutputStream myTargetStream;
037:
038: long mySourceViewOffset;
039: int mySourceViewLength;
040: int myTargetViewSize;
041:
042: byte[] mySourceBuffer;
043: byte[] myTargetBuffer;
044: MessageDigest myDigest;
045:
046: /**
047: * Creates a diff window apply baton whith source and target streams
048: * represented by files.
049: *
050: * @param source a source file (from where the source views would
051: * be taken)
052: * @param target a target file where the full text is written
053: * @param digest an MD5 checksum for the full text that would be
054: * updated after each instruction applying
055: * @return a new <b>SVNDiffWindowApplyBaton</b> object
056: * @throws SVNException
057: */
058: public static SVNDiffWindowApplyBaton create(File source,
059: File target, MessageDigest digest) throws SVNException {
060: SVNDiffWindowApplyBaton baton = new SVNDiffWindowApplyBaton();
061: baton.mySourceStream = source.exists() ? SVNFileUtil
062: .openFileForReading(source) : SVNFileUtil.DUMMY_IN;
063: baton.myTargetStream = SVNFileUtil.openFileForWriting(target,
064: true);
065: baton.mySourceBuffer = new byte[0];
066: baton.mySourceViewLength = 0;
067: baton.mySourceViewOffset = 0;
068: baton.myDigest = digest;
069: return baton;
070: }
071:
072: /**
073: * Creates a diff window apply baton whith initial source and target streams.
074: *
075: * @param source a source input stream (from where the source
076: * views would be taken)
077: * @param target a target output stream where the full text is written
078: * @param digest an MD5 checksum for the full text that would be
079: * updated after each instruction applying
080: * @return a new <b>SVNDiffWindowApplyBaton</b> object
081: */
082: public static SVNDiffWindowApplyBaton create(InputStream source,
083: OutputStream target, MessageDigest digest) {
084: SVNDiffWindowApplyBaton baton = new SVNDiffWindowApplyBaton();
085: baton.mySourceStream = source;
086: baton.myTargetStream = target;
087: baton.mySourceBuffer = new byte[0];
088: baton.mySourceViewLength = 0;
089: baton.mySourceViewOffset = 0;
090: baton.myDigest = digest;
091: return baton;
092: }
093:
094: private SVNDiffWindowApplyBaton() {
095: }
096:
097: /**
098: * Closes the source and target streams, finalizes
099: * the checksum computation and returns it in a hex representation.
100: *
101: * @return an MD5 checksum in a hex representation.
102: */
103: public String close() {
104: SVNFileUtil.closeFile(mySourceStream);
105: mySourceStream = null;
106: SVNFileUtil.closeFile(myTargetStream);
107: myTargetStream = null;
108: if (myDigest != null) {
109: MessageDigest d = myDigest;
110: myDigest = null;
111: return SVNFileUtil.toHexDigest(d);
112: }
113: return null;
114: }
115:
116: }
|