/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.compare; import java.io.*; import org.eclipse.jface.util.ListenerList; import org.eclipse.core.runtime.CoreException; import org.eclipse.compare.internal.Utilities; /** * Abstract implementation for a buffered IStreamContentAccessor. *

* Subclasses must implement the createStream method * to connect the buffered content with a streamable source (e.g., a file). *

* As long as the contents of BufferedContent is only retrieved as an input stream * (by means of getContents) and the BufferedContent is not modified (with * setContent) no buffering takes place. * Buffering starts when either method getContent or setContent is called. * * @see IContentChangeNotifier * @see IStreamContentAccessor */ public abstract class BufferedContent implements IContentChangeNotifier, IStreamContentAccessor { byte[] fContent; private ListenerList fListenerList; /** * Creates a buffered stream content accessor. */ protected BufferedContent() { } /* (non-Javadoc) * see IStreamContentAccessor.getContents */ public InputStream getContents() throws CoreException { if (fContent != null) return new ByteArrayInputStream(fContent); return createStream(); } /** * Creates and returns a stream for reading the contents. *

* Subclasses must implement this method. *

* * @return the stream from which the content is read * @exception CoreException if the contents could not be accessed */ protected abstract InputStream createStream() throws CoreException; /** * Sets the contents. Registered content change listeners are notified. * * @param contents the new contents */ public void setContent(byte[] contents) { fContent= contents; fireContentChanged(); } /** * Returns the contents as an array of bytes. * * @return the contents as an array of bytes, or null if * the contents could not be accessed */ public byte[] getContent() { if (fContent == null) { try { InputStream is= createStream(); fContent= Utilities.readBytes(is); } catch(CoreException ex) { // NeedWork } } return fContent; } /** * Discards the buffered content. */ public void discardBuffer() { fContent= null; } /* (non-Javadoc) * see IContentChangeNotifier.addChangeListener */ public void addContentChangeListener(IContentChangeListener listener) { if (fListenerList == null) fListenerList= new ListenerList(); fListenerList.add(listener); } /* (non-Javadoc) * see IContentChangeNotifier.removeChangeListener */ public void removeContentChangeListener(IContentChangeListener listener) { if (fListenerList != null) { fListenerList.remove(listener); if (fListenerList.isEmpty()) fListenerList= null; } } /** * Notifies all registered IContentChangeListeners of a content change. */ protected void fireContentChanged() { if (fListenerList != null) { Object[] listeners= fListenerList.getListeners(); for (int i= 0; i < listeners.length; i++) ((IContentChangeListener)listeners[i]).contentChanged(this); } } }