1 11 package org.eclipse.compare; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.InputStream ; 15 16 import org.eclipse.compare.internal.ContentChangeNotifier; 17 import org.eclipse.compare.internal.Utilities; 18 import org.eclipse.core.runtime.CoreException; 19 20 34 public abstract class BufferedContent implements IContentChangeNotifier, IStreamContentAccessor { 35 36 byte[] fContent; 37 private ContentChangeNotifier fChangeNotifier; 38 39 42 protected BufferedContent() { 43 } 45 46 49 public InputStream getContents() throws CoreException { 50 if (fContent != null) 51 return new ByteArrayInputStream (fContent); 52 return createStream(); 53 } 54 55 64 protected abstract InputStream createStream() throws CoreException; 65 66 71 public void setContent(byte[] contents) { 72 fContent= contents; 73 fireContentChanged(); 74 } 75 76 82 public byte[] getContent() { 83 if (fContent == null) { 84 try { 85 InputStream is= createStream(); 86 fContent= Utilities.readBytes(is); 87 } catch(CoreException ex) { 88 } 90 } 91 return fContent; 92 } 93 94 97 public void discardBuffer() { 98 fContent= null; 99 } 100 101 104 public void addContentChangeListener(IContentChangeListener listener) { 105 if (fChangeNotifier == null) 106 fChangeNotifier= new ContentChangeNotifier(this); 107 fChangeNotifier.addContentChangeListener(listener); 108 } 109 110 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 124 protected void fireContentChanged() { 125 if (fChangeNotifier == null || fChangeNotifier.isEmpty()) { 126 return; 127 } 128 fChangeNotifier.fireContentChanged(); 129 } 130 } 131 132 | Popular Tags |