Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2009 eXo Platform SAS.
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site:
Created by The eXo Platform SAS.
Date: 19.12.2008

Alex Reshetnyak
$Id: 27525 2009-01-28 00:01:58Z pnedonosko $
 public class RandomChangesFile implements ChangesFile

The logger.
    protected static final Log LOG = ExoLogger.getLogger("exo.jcr.component.ext.RandomChangesFile");

Checksum to file (set in constructor).
    private final byte[] crc;

Checksum calculated form content of file.
    private byte[] crcCalc = null;

Time stamp to ChangesLog.
    private final long id;

The file with data.
    private final File file;

The resources holder.
    private final ResourcesHolder resHolder;

The random access file.
    private RandomAccessFile fileAccessor;

MessageDigest will be used for calculating MD5 checksum.
    private MessageDigest digest;

Create ChangesFile with already formed file.

file changes file
crc checksum
id the id to changes file
resHolder ResourcesHolde, the resources holder.
Throws: will be generated the exception NoSuchAlgorithmException
    public RandomChangesFile(File filebyte[] crclong idResourcesHolder resHolderthrows NoSuchAlgorithmException
       this. = crc;
       this. = id;
      this. = file;
      this. = resHolder;
      this. = MessageDigest.getInstance("MD5");

   public byte[] getChecksum()
      return ;

   public InputStream getInputStream() throws IOException
      InputStream in = PrivilegedFileHelper.fileInputStream();
      return in;

Write data to file.

data byte buffer
position to write
Throws: will be generated the exception IOException
   public void writeData(byte[] datalong positionthrows IOException
      synchronized ()
         // write to file
         // update digest

Say internal writer that file write stopped.

Throws: error on file accessor close.
   public void finishWrite() throws IOException
      if ( != null)
         // close writer
          = null;
          = .digest();
          = null// set to null - to prevent write to file again

Check is file accessor created. Create if not.

Throws: error on file accessor creation.
   private void checkFileAccessor() throws IOException
      if ( == null)
          = PrivilegedFileHelper.randomAccessFile("rwd");
         // if (file.length() > 0) {
         // doTruncate = true;
         // }
         //"checkFileAccessor - seek on " + file.length());

Delete file and its file-system storage.

boolean, true if delete successful.
Throws: on error
See also:
   public boolean delete() throws IOException
      return PrivilegedFileHelper.delete();

   public long getId()
      return ;

   public String toString()
      return PrivilegedFileHelper.getAbsolutePath();

   public long getLength()
      return PrivilegedFileHelper.length();

   public void validate() throws InvalidChecksumException
      if ( == null || . == 0)
         throw new InvalidChecksumException("File checksum is null or empty.");
      if (!java.util.Arrays.equals())
         throw new InvalidChecksumException("File content isn't match checksum.");
New to GrepCode? Check out our FAQ X