Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.netbeans.lib.cvsclient.file;
  
  import  com.intellij.openapi.util.io.FileUtil;
  import  org.jetbrains.annotations.NonNls;
  import  org.netbeans.lib.cvsclient.util.BugLog;
  
  import java.io.*;
 import java.util.Date;

Author(s):
Thomas Singer
 
 public final class LocalFileWriter implements ILocalFileWriter {
 
   // Constants ==============================================================
 
   private static final int BUFFER_SIZE = 128 * 1024;
 
   // Fields =================================================================
 
   private final IReceiveTextFilePreprocessor receiveTextFilePreprocessor;
 
   private Date modifiedDate;
   private String nextFileMode;
   @NonNls private static final String RECEIVING_TMP_FILE_NAME = "receiving";
 
   // Setup ==================================================================
 
   public LocalFileWriter(IReceiveTextFilePreprocessor receiveTextFilePreprocessor) {
     BugLog.getInstance().assertNotNull(receiveTextFilePreprocessor);
 
     this. = receiveTextFilePreprocessor;
   }
 
   // Implemented ============================================================
 
   public void writeTextFile(FileObject fileObject,
                             int length,
                             InputStream inputStream,
                             boolean readOnly,
                             IReaderFactory readerFactory,
                             IFileReadOnlyHandler fileReadOnlyHandler,
                             IFileSystem fileSystem,
                             final Charset charSetthrows IOException {
     final File localFile = fileSystem.getFile(fileObject);
     localFile.getParentFile().mkdirs();
     if (localFile.exists()) {
       deleteFile(localFilefileReadOnlyHandler);
     }
     else {
       FileUtil.createIfDoesntExist(localFile);
     }
 
     .copyTextFileToLocation(inputStreamlengthlocalFilereaderFactorycharSet);
     setModifiedDateAndMode(localFilefileReadOnlyHandler);
     fileReadOnlyHandler.setFileReadOnly(localFilereadOnly);
   }
 
   public void writeBinaryFile(FileObject fileObject,
                               int length,
                               InputStream inputStream,
                               boolean readOnly,
                               IFileReadOnlyHandler fileReadOnlyHandler,
                               ICvsFileSystem cvsFileSystemthrows IOException {
     final File localFile = cvsFileSystem.getLocalFileSystem().getFile(fileObject);
     localFile.getParentFile().mkdirs();
 
     deleteFile(localFilefileReadOnlyHandler);
 
     writeFile(localFilelengthinputStream);
     setModifiedDateAndMode(localFilefileReadOnlyHandler);
 
     fileReadOnlyHandler.setFileReadOnly(localFilereadOnly);
   }
 
   public void removeLocalFile(FileObject fileObject, ICvsFileSystem cvsFileSystem, IFileReadOnlyHandler fileReadOnlyHandler)
     throws IOException {
     final File file = cvsFileSystem.getLocalFileSystem().getFile(fileObject);
     deleteFile(filefileReadOnlyHandler);
   }
 
   public void renameLocalFile(FileObject fileObject, ICvsFileSystem cvsFileSystemString newFileName) {
     final File originalFile = cvsFileSystem.getLocalFileSystem().getFile(fileObject);
     final File targetFile = new File(originalFile.getParentFile(), newFileName);
     try {
       FileUtil.rename(originalFiletargetFile);
     }
     catch (IOException e) {
       //ignore
     }
   }

  
Set the modified date of the next file to be written. The next call to writeFile will use this date.

Parameters:
modifiedDate the date the file should be marked as modified
  public void setNextFileDate(Date modifiedDate) {
    this. = modifiedDate;
  }
  public void setNextFileMode(String nextFileMode) {
    this. = nextFileMode;
  }
  // Utils ==================================================================
  private static void deleteFile(File file, IFileReadOnlyHandler fileReadOnlyHandlerthrows IOException {
    if (!file.exists()) {
      return;
    }
    if (!file.canWrite()) {
      fileReadOnlyHandler.setFileReadOnly(filefalse);
    }
    if (!file.delete()) {
      throw new IOException(SmartCvsSrcBundle.message("could.not.delete.file.error.message"file));
    }
  }
  private static void writeFile(File fileint lengthInputStream inputStreamthrows IOException {
    if (length == 0) {
      FileUtil.createIfDoesntExist(file);
      return;
    }
    final OutputStream fileOutputStream = new FileOutputStream(file);
    try {
      final byte[] chunk = new byte[];
      int size = length;
      while (size > 0) {
        final int bytesToRead = Math.min(sizechunk.length);
        final int bytesRead = inputStream.read(chunk, 0, bytesToRead);
        if (bytesRead < 0) {
          break;
        }
        size -= bytesRead;
        fileOutputStream.write(chunk, 0, bytesRead);
      }
    }
    finally {
      try {
        fileOutputStream.close();
      }
      catch (IOException ex) {
        // ignore
      }
    }
  }
  private void setModifiedDateAndMode(File localFile, IFileReadOnlyHandler readOnlyHandlerthrows IOException {
    if ( != null) {
      localFile.setLastModified(.getTime());
       = null;
    }
    if ( != null) {
      readOnlyHandler.setFileReadOnly(localFile, FileUtils.isReadOnlyMode());
       = null;
    }
  }
New to GrepCode? Check out our FAQ X