org.eclipse.jdt.internal.core
Class Buffer

java.lang.Object
  extended by org.eclipse.jdt.internal.core.Buffer
All Implemented Interfaces:
IBuffer
Direct Known Subclasses:
NullBuffer

public class Buffer
extends java.lang.Object
implements IBuffer

See Also:
IBuffer

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.eclipse.jdt.core.IBuffer
IBuffer.ITextEditCapability
 
Method Summary
 void addBufferChangedListener(IBufferChangedListener listener)
          Adds the given listener for changes to this buffer.
 void append(char[] text)
          Append the text to the actual content, the gap is moved to the end of the text.
 void append(java.lang.String text)
          Append the text to the actual content, the gap is moved to the end of the text.
 void close()
          Closes the buffer.
 char getChar(int position)
          Returns the character at the given position in this buffer.
 char[] getCharacters()
          Returns the contents of this buffer as a character array, or null if the buffer has not been initialized.
 java.lang.String getContents()
          Returns the contents of this buffer as a String.
 int getLength()
          Returns number of characters stored in this buffer.
 IOpenable getOwner()
          Returns the Java openable element owning of this buffer.
 java.lang.String getText(int offset, int length)
          Returns the given range of text in this buffer.
 IResource getUnderlyingResource()
          Returns the underlying resource for which this buffer was opened, or null if this buffer was not opened on a resource.
 boolean hasUnsavedChanges()
          Returns whether this buffer has been modified since it was opened or since it was last saved.
 boolean isClosed()
          Returns whether this buffer has been closed.
 boolean isReadOnly()
          Returns whether this buffer is read-only.
 void removeBufferChangedListener(IBufferChangedListener listener)
          Removes the given listener from this buffer.
 void replace(int position, int length, char[] text)
          Replaces length characters starting from position with text.
 void replace(int position, int length, java.lang.String text)
          Replaces length characters starting from position with text.
 void save(org.eclipse.core.runtime.IProgressMonitor progress, boolean force)
          Saves the contents of this buffer to its underlying resource.
 void setContents(char[] newContents)
          Sets the contents of this buffer to the given character array.
 void setContents(java.lang.String newContents)
          Sets the contents of this buffer to the given String.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

addBufferChangedListener

public void addBufferChangedListener(IBufferChangedListener listener)
Description copied from interface: IBuffer
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.

Specified by:
addBufferChangedListener in interface IBuffer
Parameters:
listener - the listener of buffer changes
See Also:
IBuffer

append

public void append(char[] text)
Append the text to the actual content, the gap is moved to the end of the text.

Specified by:
append in interface IBuffer
Parameters:
text - the given character array to append to contents of the buffer

append

public void append(java.lang.String text)
Append the text to the actual content, the gap is moved to the end of the text.

Specified by:
append in interface IBuffer
Parameters:
text - the String to append to the contents of the buffer

close

public void close()
Description copied from interface: IBuffer
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.

Specified by:
close in interface IBuffer
See Also:
IBuffer

getChar

public char getChar(int position)
Description copied from interface: IBuffer
Returns the character at the given position in this buffer.

The returned value is undefined if the buffer is closed.

Specified by:
getChar in interface IBuffer
Parameters:
position - a zero-based source offset in this buffer
Returns:
the character at the given position in this buffer
See Also:
IBuffer

getCharacters

public char[] getCharacters()
Description copied from interface: IBuffer
Returns the contents of this buffer as a character array, or null if 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.

The returned value is undefined if the buffer is closed.

Specified by:
getCharacters in interface IBuffer
Returns:
the characters contained in this buffer
See Also:
IBuffer

getContents

public java.lang.String getContents()
Description copied from interface: IBuffer
Returns the contents of this buffer as a String. Like all strings, the result is an immutable value object., It can also answer null if the buffer has not been initialized.

The returned value is undefined if the buffer is closed.

Specified by:
getContents in interface IBuffer
Returns:
the contents of this buffer as a String
See Also:
IBuffer

getLength

public int getLength()
Description copied from interface: IBuffer
Returns number of characters stored in this buffer.

The returned value is undefined if the buffer is closed.

Specified by:
getLength in interface IBuffer
Returns:
the number of characters in this buffer
See Also:
IBuffer

getOwner

public IOpenable getOwner()
Description copied from interface: IBuffer
Returns the Java openable element owning of this buffer.

Specified by:
getOwner in interface IBuffer
Returns:
the openable element owning this buffer
See Also:
IBuffer

getText

public java.lang.String getText(int offset,
                                int length)
Description copied from interface: IBuffer
Returns the given range of text in this buffer.

The returned value is undefined if the buffer is closed.

Specified by:
getText in interface IBuffer
Parameters:
offset - the zero-based starting offset
length - the number of characters to retrieve
Returns:
the given range of text in this buffer
See Also:
IBuffer

getUnderlyingResource

public IResource getUnderlyingResource()
Description copied from interface: IBuffer
Returns the underlying resource for which this buffer was opened, or null if this buffer was not opened on a resource.

Specified by:
getUnderlyingResource in interface IBuffer
Returns:
the underlying resource for this buffer, or null if none.
See Also:
IBuffer

hasUnsavedChanges

public boolean hasUnsavedChanges()
Description copied from interface: IBuffer
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 returns true.

NOTE: when a buffer does not have unsaved changes, the model may decide to close it to claim some memory back. If the associated element needs to be reopened later on, its buffer factory will be requested to create a new buffer.

Specified by:
hasUnsavedChanges in interface IBuffer
Returns:
a boolean indicating presence of unsaved changes (in the absence of any underlying resource, it will always return true).
See Also:
IBuffer

isClosed

public boolean isClosed()
Description copied from interface: IBuffer
Returns whether this buffer has been closed.

Specified by:
isClosed in interface IBuffer
Returns:
a boolean indicating whether this buffer is closed.
See Also:
IBuffer

isReadOnly

public boolean isReadOnly()
Description copied from interface: IBuffer
Returns whether this buffer is read-only.

Specified by:
isReadOnly in interface IBuffer
Returns:
a boolean indicating whether this buffer is read-only
See Also:
IBuffer

removeBufferChangedListener

public void removeBufferChangedListener(IBufferChangedListener listener)
Description copied from interface: IBuffer
Removes the given listener from this buffer. Has no affect if an identical listener is not registered or if the buffer is closed.

Specified by:
removeBufferChangedListener in interface IBuffer
Parameters:
listener - the listener
See Also:
IBuffer

replace

public void replace(int position,
                    int length,
                    char[] text)
Replaces length characters starting from position with text. After that operation, the gap is placed at the end of the inserted text.

Specified by:
replace in interface IBuffer
Parameters:
position - the zero-based starting position of the affected text range in this buffer
length - the length of the affected text range in this buffer
text - the replacing text as a character array

replace

public void replace(int position,
                    int length,
                    java.lang.String text)
Replaces length characters starting from position with text. After that operation, the gap is placed at the end of the inserted text.

Specified by:
replace in interface IBuffer
Parameters:
position - the zero-based starting position of the affected text range in this buffer
length - the length of the affected text range in this buffer
text - the replacing text as a String

save

public void save(org.eclipse.core.runtime.IProgressMonitor progress,
                 boolean force)
          throws JavaModelException
Description copied from interface: IBuffer
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 force parameter controls how this method deals with cases where the workbench is not completely in sync with the local file system. If false is 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, if true is 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).

Has no effect if this buffer is read-only or if the buffer is closed.

Specified by:
save in interface IBuffer
Parameters:
progress - the progress monitor to notify
force - a boolean flag indicating how to deal with resource inconsistencies.
Throws:
JavaModelException - if an error occurs writing the buffer to the underlying resource
See Also:
IBuffer

setContents

public void setContents(char[] newContents)
Description copied from interface: IBuffer
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 or if the buffer is closed.

Specified by:
setContents in interface IBuffer
Parameters:
newContents - the new contents of this buffer as a character array
See Also:
IBuffer

setContents

public void setContents(java.lang.String newContents)
Description copied from interface: IBuffer
Sets the contents of this buffer to the given String. 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 or if the buffer is closed.

Specified by:
setContents in interface IBuffer
Parameters:
newContents - the new contents of this buffer as a String
See Also:
IBuffer

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object