org.eclipse.compare.BufferedContent Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2000, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.compare;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.eclipse.compare.internal.ContentChangeNotifier;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.core.runtime.CoreException;
/**
* 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 ContentChangeNotifier fChangeNotifier;
/**
* Creates a buffered stream content accessor.
*/
protected BufferedContent() {
// empty implementation
}
/* (non-Javadoc)
* see IStreamContentAccessor.getContents
*/
@Override
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
*/
@Override
public void addContentChangeListener(IContentChangeListener listener) {
if (fChangeNotifier == null)
fChangeNotifier= new ContentChangeNotifier(this);
fChangeNotifier.addContentChangeListener(listener);
}
/* (non-Javadoc)
* see IContentChangeNotifier.removeChangeListener
*/
@Override
public void removeContentChangeListener(IContentChangeListener listener) {
if (fChangeNotifier != null) {
fChangeNotifier.removeContentChangeListener(listener);
if (fChangeNotifier.isEmpty())
fChangeNotifier= null;
}
}
/**
* Notifies all registered IContentChangeListener
s of a content change.
*/
protected void fireContentChanged() {
if (fChangeNotifier == null || fChangeNotifier.isEmpty()) {
return;
}
fChangeNotifier.fireContentChanged();
}
}