KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > BufferedContent


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 import org.eclipse.compare.internal.ContentChangeNotifier;
17 import org.eclipse.compare.internal.Utilities;
18 import org.eclipse.core.runtime.CoreException;
19
20 /**
21  * Abstract implementation for a buffered <code>IStreamContentAccessor</code>.
22  * <p>
23  * Subclasses must implement the <code>createStream</code> method
24  * to connect the buffered content with a streamable source (e.g., a file).
25  * <p>
26  * As long as the contents of <code>BufferedContent</code> is only retrieved as an input stream
27  * (by means of <code>getContents</code>) and the <code>BufferedContent</code> is not modified (with
28  * <code>setContent</code>) no buffering takes place.
29  * Buffering starts when either method <code>getContent</code> or <code>setContent</code> is called.
30  *
31  * @see IContentChangeNotifier
32  * @see IStreamContentAccessor
33  */

34 public abstract class BufferedContent implements IContentChangeNotifier, IStreamContentAccessor {
35     
36     byte[] fContent;
37     private ContentChangeNotifier fChangeNotifier;
38     
39     /**
40      * Creates a buffered stream content accessor.
41      */

42     protected BufferedContent() {
43         // empty implementation
44
}
45         
46     /* (non-Javadoc)
47      * see IStreamContentAccessor.getContents
48      */

49     public InputStream JavaDoc getContents() throws CoreException {
50         if (fContent != null)
51             return new ByteArrayInputStream JavaDoc(fContent);
52         return createStream();
53     }
54
55     /**
56      * Creates and returns a stream for reading the contents.
57      * <p>
58      * Subclasses must implement this method.
59      * </p>
60      *
61      * @return the stream from which the content is read
62      * @exception CoreException if the contents could not be accessed
63      */

64     protected abstract InputStream JavaDoc createStream() throws CoreException;
65     
66     /**
67      * Sets the contents. Registered content change listeners are notified.
68      *
69      * @param contents the new contents
70      */

71     public void setContent(byte[] contents) {
72         fContent= contents;
73         fireContentChanged();
74     }
75     
76     /**
77      * Returns the contents as an array of bytes.
78      *
79      * @return the contents as an array of bytes, or <code>null</code> if
80      * the contents could not be accessed
81      */

82     public byte[] getContent() {
83         if (fContent == null) {
84             try {
85                 InputStream JavaDoc is= createStream();
86                 fContent= Utilities.readBytes(is);
87             } catch(CoreException ex) {
88                 // NeedWork
89
}
90         }
91         return fContent;
92     }
93
94     /**
95      * Discards the buffered content.
96      */

97     public void discardBuffer() {
98         fContent= null;
99     }
100     
101     /* (non-Javadoc)
102      * see IContentChangeNotifier.addChangeListener
103      */

104     public void addContentChangeListener(IContentChangeListener listener) {
105         if (fChangeNotifier == null)
106             fChangeNotifier= new ContentChangeNotifier(this);
107         fChangeNotifier.addContentChangeListener(listener);
108     }
109     
110     /* (non-Javadoc)
111      * see IContentChangeNotifier.removeChangeListener
112      */

113     public void removeContentChangeListener(IContentChangeListener listener) {
114         if (fChangeNotifier != null) {
115             fChangeNotifier.removeContentChangeListener(listener);
116             if (fChangeNotifier.isEmpty())
117                 fChangeNotifier= null;
118         }
119     }
120     
121     /**
122      * Notifies all registered <code>IContentChangeListener</code>s of a content change.
123      */

124     protected void fireContentChanged() {
125         if (fChangeNotifier == null || fChangeNotifier.isEmpty()) {
126             return;
127         }
128         fChangeNotifier.fireContentChanged();
129     }
130 }
131
132
Popular Tags