KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > filestore > FileContentWriter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content.filestore;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.RandomAccessFile JavaDoc;
24 import java.nio.channels.Channels JavaDoc;
25 import java.nio.channels.WritableByteChannel JavaDoc;
26
27 import org.alfresco.repo.content.AbstractContentWriter;
28 import org.alfresco.service.cmr.repository.ContentIOException;
29 import org.alfresco.service.cmr.repository.ContentReader;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * Provides direct access to a local file.
35  * <p>
36  * This class does not provide remote access to the file.
37  *
38  * @author Derek Hulley
39  */

40 public class FileContentWriter extends AbstractContentWriter
41 {
42     private static final Log logger = LogFactory.getLog(FileContentWriter.class);
43     
44     private File JavaDoc file;
45     private boolean allowRandomAccess;
46     
47     /**
48      * Constructor that builds a URL based on the absolute path of the file.
49      *
50      * @param file the file for writing. This will most likely be directly
51      * related to the content URL.
52      */

53     public FileContentWriter(File JavaDoc file)
54     {
55         this(
56                 file,
57                 FileContentStore.STORE_PROTOCOL + file.getAbsolutePath(),
58                 null);
59     }
60     
61     /**
62      * Constructor that builds a URL based on the absolute path of the file.
63      *
64      * @param file the file for writing. This will most likely be directly
65      * related to the content URL.
66      * @param existingContentReader a reader of a previous version of this content
67      */

68     public FileContentWriter(File JavaDoc file, ContentReader existingContentReader)
69     {
70         this(
71                 file,
72                 FileContentStore.STORE_PROTOCOL + file.getAbsolutePath(),
73                 existingContentReader);
74     }
75     
76     /**
77      * Constructor that explicitely sets the URL that the reader represents.
78      *
79      * @param file the file for writing. This will most likely be directly
80      * related to the content URL.
81      * @param url the relative url that the reader represents
82      * @param existingContentReader a reader of a previous version of this content
83      */

84     public FileContentWriter(File JavaDoc file, String JavaDoc url, ContentReader existingContentReader)
85     {
86         super(url, existingContentReader);
87         
88         this.file = file;
89         allowRandomAccess = true;
90     }
91     
92     /* package */ void setAllowRandomAccess(boolean allow)
93     {
94         this.allowRandomAccess = allow;
95     }
96
97     /**
98      * @return Returns the file that this writer accesses
99      */

100     public File JavaDoc getFile()
101     {
102         return file;
103     }
104
105     /**
106      * @return Returns the size of the underlying file or
107      */

108     public long getSize()
109     {
110         if (file == null)
111             return 0L;
112         else if (!file.exists())
113             return 0L;
114         else
115             return file.length();
116     }
117
118     /**
119      * The URL of the write is known from the start and this method contract states
120      * that no consideration needs to be taken w.r.t. the stream state.
121      */

122     @Override JavaDoc
123     protected ContentReader createReader() throws ContentIOException
124     {
125         FileContentReader reader = new FileContentReader(this.file, getContentUrl());
126         reader.setAllowRandomAccess(this.allowRandomAccess);
127         return reader;
128     }
129     
130     @Override JavaDoc
131     protected WritableByteChannel JavaDoc getDirectWritableChannel() throws ContentIOException
132     {
133         try
134         {
135             // we may not write to an existing file - EVER!!
136
if (file.exists() && file.length() > 0)
137             {
138                 throw new IOException JavaDoc("File exists - overwriting not allowed");
139             }
140             // create the channel
141
WritableByteChannel JavaDoc channel = null;
142             if (allowRandomAccess)
143             {
144                 RandomAccessFile JavaDoc randomAccessFile = new RandomAccessFile JavaDoc(file, "rw"); // will create it
145
channel = randomAccessFile.getChannel();
146             }
147             else
148             {
149                 OutputStream JavaDoc os = new FileOutputStream JavaDoc(file);
150                 channel = Channels.newChannel(os);
151             }
152             // done
153
if (logger.isDebugEnabled())
154             {
155                 logger.debug("Opened write channel to file: \n" +
156                         " file: " + file + "\n" +
157                         " random-access: " + allowRandomAccess);
158             }
159             return channel;
160         }
161         catch (Throwable JavaDoc e)
162         {
163             throw new ContentIOException("Failed to open file channel: " + this, e);
164         }
165     }
166
167     /**
168      * @return Returns true always
169      */

170     public boolean canWrite()
171     {
172         return true; // this is a writer
173
}
174 }
175
Popular Tags