Interface IBuffer
IOpenable. If a buffer does not have an underlying resource,
saving the buffer has no effect. Buffers can be read-only.
This interface is similar to the JDT IBuffer interface.
- Restriction:
- This interface is not intended to be extended by clients.
-
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the given listener for changes to this buffer.voidappend(char[] text) Appends the given character array to the contents of the buffer.voidAppends the given string to the contents of the buffer.voidclose()Closes the buffer.chargetChar(int position) Returns the character at the given position in this buffer.char[]Returns the contents of this buffer as a character array, ornullif the buffer has not been initialized.Returns the contents of this buffer as aString.intReturns number of characters stored in this buffer.getOwner()Returns the resource element owning of this buffer.getText(int offset, int length) Returns the given range of text in this buffer.org.eclipse.core.resources.IResourceReturns the underlying resource for which this buffer was opened, ornullif this buffer was not opened on a resource.booleanReturns whether this buffer has been modified since it was opened or since it was last saved.booleanisClosed()Returns whether this buffer has been closed.booleanReturns whether this buffer is read-only.voidRemoves the given listener from this buffer.voidreplace(int position, int length, char[] text) Replaces the given range of characters in this buffer with the given text.voidReplaces the given range of characters in this buffer with the given text.voidsave(org.eclipse.core.runtime.IProgressMonitor progress, boolean force) Saves the contents of this buffer to its underlying resource.voidsetContents(char[] contents) Sets the contents of this buffer to the given character array.voidsetContents(String contents) Sets the contents of this buffer to the givenString.
-
Method Details
-
addBufferChangedListener
Adds the given listener for changes to this buffer. Has no effect if an identical listener is already registered or if the buffer is closed.- Parameters:
listener- the listener of buffer changes
-
append
void append(char[] text) Appends the given character array to the contents of the buffer. This buffer will now have unsaved changes. Any client can append to the contents of the buffer, not just the owner of the buffer. Reports a buffer changed event.Has no effect if this buffer is read-only.
A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
text- the given character array to append to contents of the buffer
-
append
Appends the given string to the contents of the buffer. This buffer will now have unsaved changes. Any client can append to the contents of the buffer, not just the owner of the buffer. Reports a buffer changed event.Has no effect if this buffer is read-only.
A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
text- theStringto append to the contents of the buffer
-
close
void close()Closes the buffer. Any unsaved changes are lost. Reports a buffer changed event with a 0 offset and a 0 length. When this event is fired, the buffer should already be closed.Further operations on the buffer are not allowed, except for close. If an attempt is made to close an already closed buffer, the second attempt has no effect.
-
getChar
char getChar(int position) Returns the character at the given position in this buffer.A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
position- a zero-based source offset in this buffer- Returns:
- the character at the given position in this buffer
-
getCharacters
char[] getCharacters()Returns the contents of this buffer as a character array, ornullif the buffer has not been initialized.Callers should make no assumption about whether the returned character array is or is not the genuine article or a copy. In other words, if the client wishes to change this array, they should make a copy. Likewise, if the client wishes to hang on to the array in its current state, they should make a copy.
A
RuntimeExceptionmight be thrown if the buffer is closed.- Returns:
- the characters contained in this buffer
-
getContents
String getContents()Returns the contents of this buffer as aString. Like all strings, the result is an immutable value object., It can also answernullif the buffer has not been initialized.A
RuntimeExceptionmight be thrown if the buffer is closed.- Returns:
- the contents of this buffer as a
String
-
getLength
int getLength()Returns number of characters stored in this buffer.A
RuntimeExceptionmight be thrown if the buffer is closed.- Returns:
- the number of characters in this buffer
-
getOwner
IOpenable getOwner()Returns the resource element owning of this buffer.- Returns:
- the resource element owning this buffer
-
getText
Returns the given range of text in this buffer.A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
offset- the zero-based starting offsetlength- the number of characters to retrieve- Returns:
- the given range of text in this buffer
-
getUnderlyingResource
org.eclipse.core.resources.IResource getUnderlyingResource()Returns the underlying resource for which this buffer was opened, ornullif this buffer was not opened on a resource.- Returns:
- the underlying resource for this buffer, or
nullif none.
-
hasUnsavedChanges
boolean hasUnsavedChanges()Returns whether this buffer has been modified since it was opened or since it was last saved. If a buffer does not have an underlying resource, this method always returnstrue.- Returns:
- a
booleanindicating presence of unsaved changes (in the absence of any underlying resource, it will always returntrue).
-
isClosed
boolean isClosed()Returns whether this buffer has been closed.- Returns:
- a
booleanindicating whether this buffer is closed.
-
isReadOnly
boolean isReadOnly()Returns whether this buffer is read-only.- Returns:
- a
booleanindicating whether this buffer is read-only
-
removeBufferChangedListener
Removes the given listener from this buffer. Has no affect if an identical listener is not registered or if the buffer is closed.- Parameters:
listener- the listener
-
replace
void replace(int position, int length, char[] text) Replaces the given range of characters in this buffer with the given text.positionandposition + lengthmust be in the range [0, getLength()].lengthmust not be negative.A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
position- the zero-based starting position of the affected text range in this bufferlength- the length of the affected text range in this buffertext- the replacing text as a character array
-
replace
Replaces the given range of characters in this buffer with the given text.positionandposition + lengthmust be in the range [0, getLength()].lengthmust not be negative.A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
position- the zero-based starting position of the affected text range in this bufferlength- the length of the affected text range in this buffertext- the replacing text as aString
-
save
Saves the contents of this buffer to its underlying resource. If successful, this buffer will have no unsaved changes. The buffer is left open. Saving a buffer with no unsaved changes has no effect - the underlying resource is not changed. If the buffer does not have an underlying resource or is read-only, this has no effect.The
forceparameter controls how this method deals with cases where the workbench is not completely in sync with the local file system. Iffalseis specified, this method will only attempt to overwrite a corresponding file in the local file system provided it is in sync with the workbench. This option ensures there is no unintended data loss; it is the recommended setting. However, iftrueis specified, an attempt will be made to write a corresponding file in the local file system, overwriting any existing one if need be. In either case, if this method succeeds, the resource will be marked as being local (even if it wasn't before).A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
progress- the progress monitor to notifyforce- abooleanflag indicating how to deal with resource inconsistencies.- Throws:
CModelException- if an error occurs writing the buffer to the underlying resource- See Also:
-
setContents
void setContents(char[] contents) Sets the contents of this buffer to the given character array. This buffer will now have unsaved changes. Any client can set the contents of the buffer, not just the owner of the buffer. Reports a buffer changed event.Equivalent to
replace(0, getLength(), contents).Has no effect if this buffer is read-only.
A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
contents- the new contents of this buffer as a character array
-
setContents
Sets the contents of this buffer to the givenString. This buffer will now have unsaved changes. Any client can set the contents of the buffer, not just the owner of the buffer. Reports a buffer changed event.Equivalent to
replace(0, getLength(), contents).Has no effect if this buffer is read-only.
A
RuntimeExceptionmight be thrown if the buffer is closed.- Parameters:
contents- the new contents of this buffer as aString
-