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
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * 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: http://www.fsf.org.
  */
 package org.exoplatform.services.jcr.ext.replication.storage;
 
 
 import java.io.File;
Created by The eXo Platform SAS.
Date: 19.12.2008

Author(s):
Alex Reshetnyak
Version:
$Id: ChangesFile.java 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.

Parameters:
file changes file
crc checksum
id the id to changes file
resHolder ResourcesHolde, the resources holder.
Throws:
java.security.NoSuchAlgorithmException 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
   {
      finishWrite();
      InputStream in = PrivilegedFileHelper.fileInputStream();
      .add(in);
      return in;
   }

   
Write data to file.

Parameters:
data byte buffer
position to write
Throws:
java.io.IOException will be generated the exception IOException
   public void writeData(byte[] datalong positionthrows IOException
   {
      checkFileAccessor();
      synchronized ()
      {
         // write to file
         .seek(position);
         .write(data);
         // update digest
         .update(data);
      }
   }

   
Say internal writer that file write stopped.

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

   
Check is file accessor created. Create if not.

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

   
Delete file and its file-system storage.

Returns:
boolean, true if delete successful.
Throws:
java.io.IOException on error
See also:
java.io.File.delete()
   public boolean delete() throws IOException
   {
      finishWrite();
      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