Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * ====================================================================
   * Copyright (c) 2004 TMate Software Ltd.  All rights reserved.
   *
   * This software is licensed as described in the file COPYING, which
   * you should have received as part of this distribution.  The terms
   * are also available at http://svnkit.com/license.html
   * If newer versions of this license are posted there, you may use a
   * newer version instead, at your option.
  * ====================================================================
  */
 package org.tmatesoft.svn.core.internal.io.fs;
 
 import java.io.File;
 

Author(s):
TMate Software Ltd.
Version:
1.1.0
 
 public class FSOutputStream extends OutputStream implements ISVNDeltaConsumer {
 
     public static final int WRITE_BUFFER_SIZE = 512000;
     public static final int SVN_DELTA_WINDOW_SIZE = 102400;
 
     private boolean isHeaderWritten;
     private CountingStream myTargetFile;
     private long myDeltaStart;
     private long myRepSize;
     private long myRepOffset;
     private InputStream mySourceStream;
     private FSRevisionNode myRevNode;
     private MessageDigest myDigest;
     private FSTransactionRoot myTxnRoot;
     private long mySourceOffset;
     private ByteBuffer myTextBuffer;
     private boolean myIsClosed;
     private boolean myIsCompress;
 
     private FSOutputStream(FSRevisionNode revNodeCountingStream fileInputStream sourcelong deltaStartlong repSizelong repOffsetFSTransactionRoot txnRootboolean compressthrows SVNException {
          = txnRoot;
          = file;
          = source;
          = deltaStart;
          = repSize;
          = repOffset;
          = false;
          = revNode;
          = 0;
          = false;
 
          = ByteBuffer.allocate();
 
         try {
              = MessageDigest.getInstance("MD5");
         } catch (NoSuchAlgorithmException nsae) {
             SVNErrorMessage err = SVNErrorMessage.create(."MD5 implementation not found: {0}"nsae.getLocalizedMessage());
             SVNErrorManager.error(errnsae);
         }
          = compress;
     }
 
     private void reset(FSRevisionNode revNodeCountingStream fileInputStream sourcelong deltaStartlong repSizelong repOffsetFSTransactionRoot txnRoot) {
          = txnRoot;
          = file;
          = source;
          = deltaStart;
          = repSize;
          = repOffset;
          = false;
          = revNode;
          = 0;
          = false;
         .reset();
         .clear();
     }
 
     public static OutputStream createStream(FSRevisionNode revNodeFSTransactionRoot txnRootOutputStream dstStreamboolean compressthrows SVNException {
         if (revNode.getType() != .) {
             SVNErrorMessage err = SVNErrorMessage.create(."Attempted to set textual contents of a *non*-file node");
            SVNErrorManager.error(err);
        }
        if (!revNode.getId().isTxn()) {
            SVNErrorMessage err = SVNErrorMessage.create(."Attempted to set textual contents of an immutable node");
            SVNErrorManager.error(err);
        }
        OutputStream targetOS = null;
        InputStream sourceStream = null;
        long offset = -1;
        long deltaStart = -1;
        try {
            File targetFile = txnRoot.getTransactionRevFile();
            offset = targetFile.length();
            targetOS = SVNFileUtil.openFileForWriting(targetFiletrue);
            CountingStream revWriter = new CountingStream(targetOSoffset);
            FSRepresentation baseRep = revNode.chooseDeltaBase(txnRoot.getOwner());
            sourceStream = FSInputStream.createDeltaStream(new SVNDeltaCombiner(), baseReptxnRoot.getOwner());
            String header;
            if (baseRep != null) {
                header = . + " " + baseRep.getRevision() + " " + baseRep.getOffset() + " " + baseRep.getSize() + "\n";
            } else {
                header = . + "\n";
            }
            revWriter.write(header.getBytes("UTF-8"));
            deltaStart = revWriter.getPosition();
            if ((dstStream != null) && (dstStream instanceof FSOutputStream)) {
                FSOutputStream fsOS = (FSOutputStreamdstStream;
                fsOS.reset(revNoderevWritersourceStreamdeltaStart, 0, offsettxnRoot);
                return dstStream;
            }
            return new FSOutputStream(revNoderevWritersourceStreamdeltaStart, 0, offsettxnRootcompress);
        } catch (IOException ioe) {
            SVNFileUtil.closeFile(targetOS);
            SVNFileUtil.closeFile(sourceStream);
            SVNErrorMessage err = SVNErrorMessage.create(.ioe.getLocalizedMessage());
            SVNErrorManager.error(errioe);
        } catch (SVNException svne) {
            SVNFileUtil.closeFile(targetOS);
            SVNFileUtil.closeFile(sourceStream);
            throw svne;
        }
        return null;
    }
    public void write(int bthrows IOException {
        write(new byte[] {
            (byte) (b & 0xFF)
        }, 0, 1);
    }
    public void write(byte[] bthrows IOException {
        write(b, 0, b.length);
    }
    public void write(byte[] bint offint lenthrows IOException {
        .update(bofflen);
         += len;
        int toWrite = 0;
        while (len > 0) {
            toWrite = Math.min(len.remaining());
            .put(bofftoWrite);
            if (.remaining() == 0) {
                try {
                    ByteArrayInputStream target = new ByteArrayInputStream(.array(), 0, .capacity());
                    .sendDelta(nulltargetthisfalse);
                } catch (SVNException svne) {
                    throw new IOException(svne.getMessage());
                }
                .clear();
            }
            off += toWrite;
            len -= toWrite;
        }
    }
    public void close() throws IOException {
        if () {
            return;
        }
         = true;
        try {
            ByteArrayInputStream target = new ByteArrayInputStream(.array(), 0, .position());
            .sendDelta(nulltargetthisfalse);
            FSRepresentation rep = new FSRepresentation();
            rep.setOffset();
            long offset = .getPosition();
            rep.setSize(offset - );
            rep.setExpandedSize();
            rep.setTxnId(.getId().getTxnID());
            rep.setRevision(.);
            rep.setHexDigest(SVNFileUtil.toHexDigest());
            .write("ENDREP\n".getBytes("UTF-8"));
            .setTextRepresentation(rep);
        } catch (SVNException svne) {
            throw new IOException(svne.getMessage());
        } finally {
            closeStreams();
        }
    }
    public void closeStreams() {
        SVNFileUtil.closeFile();
        SVNFileUtil.closeFile();
    }
    public FSRevisionNode getRevisionNode() {
        return ;
    }
    public OutputStream textDeltaChunk(String pathSVNDiffWindow diffWindowthrows SVNException {
         += diffWindow.getInstructionsLength();
        try {
            diffWindow.writeTo(, !);
             = true;
        } catch (IOException ioe) {
            SVNErrorMessage err = SVNErrorMessage.create(.ioe.getLocalizedMessage());
            SVNErrorManager.error(errioe);
        }
        return .;
    }
    public void textDeltaEnd(String paththrows SVNException {
    }
    public void applyTextDelta(String pathString baseChecksumthrows SVNException {
    }
New to GrepCode? Check out our FAQ X