001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs;
031:
032: import java.io.FileOutputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035:
036: import java.nio.channels.FileChannel;
037: import java.nio.channels.FileLock;
038: import java.nio.channels.OverlappingFileLockException;
039:
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: /**
044: * Stream encapsulating FileOutputStream
045: */
046: public class FileWriteStream extends VfsStream implements
047: LockableStream {
048: private FileOutputStream _os;
049:
050: private FileLock _fileLock;
051: private FileChannel _fileChannel;
052:
053: private static final Logger log = Logger
054: .getLogger(FileWriteStream.class.getName());
055:
056: /**
057: * Create a new FileWriteStream based on the java.io.* stream.
058: *
059: * @param fos the underlying file output stream.
060: */
061: public FileWriteStream(FileOutputStream fos) {
062: super (null, fos);
063: _os = fos;
064: }
065:
066: /**
067: * Create a new FileWriteStream based on the java.io.* stream.
068: *
069: * @param fos the underlying file output stream.
070: * @param path the associated Path.
071: */
072: public FileWriteStream(FileOutputStream fos, Path path) {
073: super (null, fos, path);
074: _os = fos;
075: }
076:
077: /**
078: * Closes the underlying stream.
079: */
080: public void close() throws IOException {
081: unlock();
082:
083: _fileChannel = null;
084:
085: super .close();
086: }
087:
088: public boolean lock(boolean shared, boolean block) {
089: unlock();
090:
091: if (shared) {
092: // Invalid request for a shared "read" lock on a write only stream.
093:
094: return false;
095: }
096:
097: try {
098: if (_fileChannel == null) {
099: _fileChannel = _os.getChannel();
100: }
101:
102: if (block)
103: _fileLock = _fileChannel.lock(0, Long.MAX_VALUE, false);
104: else
105: _fileLock = _fileChannel.tryLock(0, Long.MAX_VALUE,
106: false);
107:
108: return _fileLock != null;
109: } catch (OverlappingFileLockException e) {
110: log.log(Level.FINE, e.toString(), e);
111: return false;
112: } catch (IOException e) {
113: log.log(Level.FINE, e.toString(), e);
114: return false;
115: }
116: }
117:
118: public boolean unlock() {
119: try {
120: FileLock lock = _fileLock;
121: _fileLock = null;
122:
123: if (lock != null) {
124: lock.release();
125:
126: return true;
127: }
128:
129: return false;
130: } catch (IOException e) {
131: log.log(Level.FINE, e.toString(), e);
132: return false;
133: }
134: }
135:
136: }
|