PageRenderTime 84ms CodeModel.GetById 75ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bufferio/BufferAutosaveRequest.java

#
Java | 122 lines | 64 code | 11 blank | 47 comment | 5 complexity | 1afb8cafa95b61156b20d7aa17a28524 MD5 | raw file
  1/*
  2 * BufferAutosaveRequest.java - I/O request
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2005 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.bufferio;
 24
 25//{{{ Imports
 26import java.io.*;
 27import org.gjt.sp.jedit.io.*;
 28import org.gjt.sp.jedit.*;
 29import org.gjt.sp.util.*;
 30//}}}
 31
 32/**
 33 * A buffer autosave request.
 34 * @author Slava Pestov
 35 * @version $Id: BufferAutosaveRequest.java 19408 2011-02-28 14:52:25Z kpouer $
 36 */
 37public class BufferAutosaveRequest extends BufferIORequest
 38{
 39	//{{{ BufferAutosaveRequest constructor
 40	/**
 41	 * Creates a new buffer I/O request.
 42	 * @param view The view
 43	 * @param buffer The buffer
 44	 * @param session The VFS session
 45	 * @param vfs The VFS
 46	 * @param path The path
 47	 */
 48	public BufferAutosaveRequest(View view, Buffer buffer,
 49		Object session, VFS vfs, String path)
 50	{
 51		super(view,buffer,session,vfs,path);
 52	} //}}}
 53
 54	//{{{ run() method
 55	public void run()
 56	{
 57		OutputStream out = null;
 58
 59		try
 60		{
 61			String[] args = { vfs.getFileName(path) };
 62			setStatus(jEdit.getProperty("vfs.status.autosave",args));
 63
 64			// the entire save operation can be aborted...
 65			setAbortable(true);
 66
 67			try
 68			{
 69				//buffer.readLock();
 70
 71				if(!buffer.isDirty())
 72				{
 73					// buffer has been saved while we
 74					// were waiting.
 75					return;
 76				}
 77
 78				out = vfs._createOutputStream(session,path,view);
 79				if(out == null)
 80					return;
 81
 82				write(buffer,out);
 83			}
 84			catch (FileNotFoundException e)
 85			{
 86				Log.log(Log.WARNING,this,"Unable to save " + e.getMessage());
 87			}
 88			catch(Exception e)
 89			{
 90				Log.log(Log.ERROR,this,e);
 91				String[] pp = { e.toString() };
 92				VFSManager.error(view,path,"ioerror.write-error",pp);
 93
 94				// Incomplete autosave file should not exist.
 95				if(out != null)
 96				{
 97					try
 98					{
 99						out.close();
100						out = null;
101						vfs._delete(session,path,view);
102					}
103					catch(IOException ioe)
104					{
105						Log.log(Log.ERROR,this,ioe);
106					}
107				}
108			}
109			//finally
110			//{
111				//buffer.readUnlock();
112			//}
113		}
114		catch(WorkThread.Abort a)
115		{
116		}
117		finally
118		{
119			IOUtilities.closeQuietly(out);
120		}
121	} //}}}
122}