uk.org.ogsadai.activity.files
Class FileWritingActivity

java.lang.Object
  |
  +--uk.org.ogsadai.activity.Activity
        |
        +--uk.org.ogsadai.activity.files.AbstractFileActivity
              |
              +--uk.org.ogsadai.activity.files.FileWritingActivity

public class FileWritingActivity
extends AbstractFileActivity

This activity allows writing to a file. The file may be a text file or a binary file. The available actions include deleting and replacing portions of the file, and appending and inserting text into the file. For information on this activity see the OGSA-DAI user doc: OGSA-DAI/doc/interaction/activities/files/fileWritingActivity.html

Author:
The OGSA-DAI Project Team

Field Summary
static int APPEND
          Indicate that we wish to perform a append operation.
private static java.lang.String COPYRIGHT_NOTICE
          Copyright statement
static int DELETE
          Indicate that we wish to perform a delete operation.
static int INSERT
          Indicate that we wish to perform a insert operation.
private static DAILogger LOG
          Logger object for logging in this class
private  int mAction
          The action which will be performed on the file.
private  boolean mAlreadyReturnedBlank
          Indicates whether we've already returned a blank.
private  java.io.File mFile
          The file to write to.
private  long mFileLength
          The length of the file to write to, before any writing takes place.
private  java.lang.String mFilename
          The filename of the file to write to.
private  BlockReader mInput
          The output from another activity, from which to read text to write into the file.
private  java.lang.String mLength
          The number of bytes length over which the writing will occur.
private  InfiniteSet mMatch
          An expression of the instances of matches of the regular expression to deal with.
private  boolean mMultiline
          An indication of whether we are performing the action on a per-file or per-line basis.
private  java.lang.String mOffset
          The number of bytes offset from which the writing will occur.
private  boolean mOptimiseForTime
          An indication of whether we wish to aim for efficient use of time or efficent use of memory when performing edits on large files.
private  BlockWriter mOutput
          The input to another activity, to which to write this activity's output.
private  java.io.RandomAccessFile mRaFile
          The file to write to.
private  java.lang.String mRegexp
          The regular expression to use to locate the part of the file to write to.
private  java.lang.String mString
          The string containing the text to write into the file.
private  int mType
          The style we are using to indicate where the writing will occur.
static int NONE
          Indicate that no action has been set.
static int OFFSET_AND_LENGTH
          Indicate that we are identifying the text to modify using offset and/or length.
static boolean PER_FILE
          Indicate that we wish to perform the action on a per-file basis.
static boolean PER_LINE
          Indicate that we wish to perform the action on a per-line basis.
static int REGEXP
          Indicate that we are identifying the text to modify using a regular expression.
static int REPLACE
          Indicate that we wish to perform a replace operation.
static java.lang.String SEQ_SEPARATOR
          The character used to separate items in a sequence in the match expression.
static int STEP_SIZE_UPPER_LIMIT
          The upper limit on the step size which will be employed when reading and writing portions of files.
 
Fields inherited from class uk.org.ogsadai.activity.files.AbstractFileActivity
mCredentials, mFileAccessProvider, mTopDir
 
Fields inherited from class uk.org.ogsadai.activity.Activity
mContext, mExternalInputs, mExternalOutputs, mInternalInputs, mInternalOutputs
 
Constructor Summary
FileWritingActivity(org.w3c.dom.Element element)
           
 
Method Summary
private  void append(java.io.RandomAccessFile raFile, java.lang.String string)
          Append a string to the end of a file, thereby extending its length.
static int charAt(java.io.RandomAccessFile raFile, long pos)
          Returns the byte at a particular offset within a file.
private  void delete(java.io.RandomAccessFile raFile, long offset, long length)
          Delete a number of contiguous bytes from a file, starting at a particular offset.
private  void extractFromOrTextAttribute(org.w3c.dom.Element node)
          Reads the from or text attributes of the given XML element, and sets instance variables accordingly.
private  void extractOptimiseAttribute(org.w3c.dom.Element node)
          Sets the mOptimiseForTime instance variable depending on the contents of the optimiseSpeed attribute of the provided XML element.
static java.lang.String getFileContents(java.io.RandomAccessFile raFile)
          Obtains the entire content of a file and returns it as a single string.
private static java.lang.String getNextTenBytes(java.io.RandomAccessFile raFile)
          Return the 35 bytes from the provided file located after the current file pointer.
private  int getStepSize(long length)
          Returns the step size, for reading a chunk of data of total size length.
private  java.lang.String getStringInput()
          Returns the string used as the activity's input.
 void initialise()
          Gets the data resource accessor for the files data resource according to whether the users credentials permit this access.
private  void insert(java.io.RandomAccessFile raFile, long offset, java.lang.String string)
          Insert a string into a particular place in a file, causing the contents of the file after that place to be shifted away from the start of the file by an amount equal to the length of the string.
private  java.lang.String obtainInput()
          Obtains all the data available from the activity providing its output to this activity, and returns it as a single string.
static void overwriteString(java.io.RandomAccessFile raFile, long offset, java.lang.String replacementString)
          Overwrite a sequence of bytes in a file with a replacement string.
private  int parseInt(java.lang.String intString)
          Helper method to parse strings into integers.
private  long parseLength(java.lang.String string, long fileLength, long offset)
          Parses a string containing a representation of the length of a sequence of bytes within a file.
private  void parseLocate(org.w3c.dom.Element node)
          Parse a locate element, extracting its contents and setting the instance variables of this object appropriately.
private  InfiniteSet parseMatch(java.lang.String list)
          Parses an expression of a collection of integers.
private  long parseOffset(java.lang.String string, long fileLength)
          Parses a string containing a representation of an offset within a file or a line.
private  void performAction(java.io.RandomAccessFile raFile, int type, long offset, long length, boolean perLine, java.lang.String string)
          Perform an action on a file.
private  void performPerFileOffsetAndLength(java.io.RandomAccessFile raFile, long fileLength)
          Perform a replacement of text within a file, on a per-file basis, locating the text based on a range of bytes uniquely defined by an offset and a length.
private  void performPerFileRegexp(java.io.RandomAccessFile raFile, long fileLength)
          Perform a replacement of text within a file, on a per-file basis, locating the text based on matches of a regular expression.
private  void performPerLineOffsetAndLength(java.io.RandomAccessFile raFile, long fileLength)
          Perform a replacement of text within a file, on a per-line basis, locating the text based on a range of bytes uniquely defined by an offset and a length.
private  void performPerLineRegexp(java.io.RandomAccessFile raFile, long fileLength)
          Perform a replacement of text within a file, on a per-line basis, locating the text based on matches of a regular expression.
private  java.lang.String performSubstitutions(java.lang.String string, AbstractMatcher matcher)
          Return a string equal to the provided string, but with group variables substituted with their actual value, in the context of a particular regular expression match.
 void processBlock()
          Performs an iteration of the processing of an activity.
 java.lang.String readLine(java.io.RandomAccessFile raFile)
          Read a line from the file
private  void replace(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String string)
          Replace a sequence of contiguous bytes in a file with a string.
private  void replaceString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString)
          Replace a sequence of contiguous bytes in a file with a string.
private  java.lang.String replaceStringInString(java.lang.String hostString, int offset, int length, java.lang.String replacement)
          Replaces a substring within a string with another string.
static void replaceWithLongerString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString, int step)
          Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly longer than the sequence of bytes.
static void replaceWithShorterString(java.io.RandomAccessFile raFile, long offset, long length, java.lang.String replacementString, int step)
          Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly shorter than the sequence of bytes.
 
Methods inherited from class uk.org.ogsadai.activity.Activity
cleanUp, connectsTo, createOutputPipe, getActivityConfiguration, getActivityName, getCause, getName, getObservableStatus, getProperties, getSession, getStatus, hasActivityConfiguration, hasProperties, process, processFirst, setActivityName, setCompleted, setError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
Copyright statement

See Also:
Constant Field Values

LOG

private static final DAILogger LOG
Logger object for logging in this class


SEQ_SEPARATOR

public static final java.lang.String SEQ_SEPARATOR
The character used to separate items in a sequence in the match expression.

See Also:
Constant Field Values

STEP_SIZE_UPPER_LIMIT

public static final int STEP_SIZE_UPPER_LIMIT
The upper limit on the step size which will be employed when reading and writing portions of files.

See Also:
Constant Field Values

NONE

public static final int NONE
Indicate that no action has been set.

See Also:
Constant Field Values

DELETE

public static final int DELETE
Indicate that we wish to perform a delete operation.

See Also:
Constant Field Values

REPLACE

public static final int REPLACE
Indicate that we wish to perform a replace operation.

See Also:
Constant Field Values

APPEND

public static final int APPEND
Indicate that we wish to perform a append operation.

See Also:
Constant Field Values

INSERT

public static final int INSERT
Indicate that we wish to perform a insert operation.

See Also:
Constant Field Values

OFFSET_AND_LENGTH

public static final int OFFSET_AND_LENGTH
Indicate that we are identifying the text to modify using offset and/or length.

See Also:
Constant Field Values

REGEXP

public static final int REGEXP
Indicate that we are identifying the text to modify using a regular expression.

See Also:
Constant Field Values

PER_FILE

public static final boolean PER_FILE
Indicate that we wish to perform the action on a per-file basis.

See Also:
Constant Field Values

PER_LINE

public static final boolean PER_LINE
Indicate that we wish to perform the action on a per-line basis.

See Also:
Constant Field Values

mType

private int mType
The style we are using to indicate where the writing will occur. One of {OFFSET_AND_LENGTH, REGEXP}.


mFilename

private java.lang.String mFilename
The filename of the file to write to.


mFile

private java.io.File mFile
The file to write to.


mRaFile

private java.io.RandomAccessFile mRaFile
The file to write to.


mFileLength

private long mFileLength
The length of the file to write to, before any writing takes place.


mOffset

private java.lang.String mOffset
The number of bytes offset from which the writing will occur.


mLength

private java.lang.String mLength
The number of bytes length over which the writing will occur.


mRegexp

private java.lang.String mRegexp
The regular expression to use to locate the part of the file to write to.


mMatch

private InfiniteSet mMatch
An expression of the instances of matches of the regular expression to deal with.


mMultiline

private boolean mMultiline
An indication of whether we are performing the action on a per-file or per-line basis.


mOptimiseForTime

private boolean mOptimiseForTime
An indication of whether we wish to aim for efficient use of time or efficent use of memory when performing edits on large files.


mAction

private int mAction
The action which will be performed on the file. One of {NONE, DELETE, REPLACE, APPEND, INSERT}.


mInput

private BlockReader mInput
The output from another activity, from which to read text to write into the file.


mOutput

private BlockWriter mOutput
The input to another activity, to which to write this activity's output.


mString

private java.lang.String mString
The string containing the text to write into the file.


mAlreadyReturnedBlank

private boolean mAlreadyReturnedBlank
Indicates whether we've already returned a blank.

Constructor Detail

FileWritingActivity

public FileWritingActivity(org.w3c.dom.Element element)
                    throws ActivitySpecificationException,
                           ActivityCreationException
Method Detail

parseMatch

private InfiniteSet parseMatch(java.lang.String list)
                        throws ActivitySpecificationException
Parses an expression of a collection of integers.

The expression must be a comma-separated list of values. The values may be any of:

Value String Represents
Single, positive integers x The integer x
Finite ranges of positive integers x-y The interval [x, y], where x < y
Infinite ranges of positive integers x- The interval [x, infinity)
Strictly increasing infinite sequences of positive integers x1:x2:x3:... The infinite sequence (x1, x2, x3, ...), where x1 < x2 and x2 < x3

Parameters:
list - The comma-separated list of values
Returns:
an object describing the contents of the list of values
Throws:
ActivitySpecificationException - If there was a syntax error in the list of values

parseInt

private int parseInt(java.lang.String intString)
              throws DAINumberFormatException
Helper method to parse strings into integers.

Parameters:
intString - String to parse
Returns:
integer.
Throws:
DAINumberFormatException - If the string cannot be parsed into an integer.

parseLocate

private void parseLocate(org.w3c.dom.Element node)
                  throws ActivitySpecificationException
Parse a locate element, extracting its contents and setting the instance variables of this object appropriately.

Parameters:
node - The element to parse
Throws:
ActivitySpecificationException - If there is a syntax error in the match expression

extractOptimiseAttribute

private void extractOptimiseAttribute(org.w3c.dom.Element node)
Sets the mOptimiseForTime instance variable depending on the contents of the optimiseSpeed attribute of the provided XML element. Iff the element does not contain this attribute, or contains the attribute with a boolean true value then the field is set to true.

Parameters:
node - The XML element

extractFromOrTextAttribute

private void extractFromOrTextAttribute(org.w3c.dom.Element node)
                                 throws ActivitySpecificationException
Reads the from or text attributes of the given XML element, and sets instance variables accordingly.

Parameters:
node - the XML element
Throws:
ActivitySpecificationException - If neither from nor text attributes were specified

parseOffset

private long parseOffset(java.lang.String string,
                         long fileLength)
                  throws DAINumberFormatException,
                         DAIOutOfBoundsException
Parses a string containing a representation of an offset within a file or a line. The string may contain a positive integer or one of start or end, representing the beginning and end of the file or the line, respectively.

Parameters:
string - The string to parse
fileLength - The length of the file or the line
Returns:
the offset
Throws:
DAINumberFormatException - If the string did not contain an integer.
DAIOutOfBoundsException - If the string contained an out of bounds integer.

parseLength

private long parseLength(java.lang.String string,
                         long fileLength,
                         long offset)
                  throws DAINumberFormatException,
                         DAIOutOfBoundsException
Parses a string containing a representation of the length of a sequence of bytes within a file. The string may contain a positive integer, which represents the length of the sequence, or the value toEnd, which represents the length from the given offset to the end of the file.

Parameters:
string - The string to parse
fileLength - The total length of the file
offset - The offset from which this length is considered to start
Returns:
the length
Throws:
DAINumberFormatException - If the string did not contain an integer.
DAIOutOfBoundsException - If the string contained an out of bounds integer.

initialise

public void initialise()
                throws ActivitySpecificationException,
                       ActivityExecutionException
Description copied from class: AbstractFileActivity
Gets the data resource accessor for the files data resource according to whether the users credentials permit this access.

Overrides:
initialise in class AbstractFileActivity
Throws:
ActivitySpecificationException - If the user is not permitted to access the resource.
ActivityExecutionException - If some internal problem occurs.
See Also:
Activity.initialise()

processBlock

public void processBlock()
Description copied from class: Activity
Performs an iteration of the processing of an activity. This may involve reading a block of input data and writing a block of output data, or may involve some other kind of processing. When an activity is processed by the OGSA-DAI engine, this method will be invoked repeatedly until the activity either completes or stops due to an error or termination call. An implementation of this method should invoke the setCompleted method to indicate that processing is complete, or the setError method if an error occurs that will prevent the processing from completing.

Specified by:
processBlock in class Activity

performPerLineRegexp

private void performPerLineRegexp(java.io.RandomAccessFile raFile,
                                  long fileLength)
                           throws NonIncreasingSequenceException,
                                  MalformedRegexpPatternException,
                                  MalformedReplacementStringException,
                                  UnsupportedRegexpPatternFormatException,
                                  java.io.IOException
Perform a replacement of text within a file, on a per-line basis, locating the text based on matches of a regular expression.

Parameters:
raFile - The file to replace text within
fileLength - The length of the file
Throws:
NonIncreasingSequenceException - If the match expression contained a sequence of integers which was not strictly increasing
MalformedRegexpPatternException - If the regular expression contains a syntax error.
MalformedReplacementStringException - If there was a syntax error in the string
UnsupportedRegexpPatternFormatException - If the regular expression is unsupported.
java.io.IOException - If there's a problem accessing the file

performPerFileRegexp

private void performPerFileRegexp(java.io.RandomAccessFile raFile,
                                  long fileLength)
                           throws NonIncreasingSequenceException,
                                  MalformedRegexpPatternException,
                                  MalformedReplacementStringException,
                                  UnsupportedRegexpPatternFormatException,
                                  java.io.IOException
Perform a replacement of text within a file, on a per-file basis, locating the text based on matches of a regular expression.

Parameters:
raFile - The file to replace text within
fileLength - The length of the file
Throws:
NonIncreasingSequenceException - If the match expression contained a sequence of integers which was not strictly increasing
MalformedRegexpPatternException - If the regular expression contains a syntax error.
MalformedReplacementStringException - If there was a syntax error in the string
UnsupportedRegexpPatternFormatException - If the regular expression is unsupported.
java.io.IOException - If there's a problem accessing the file

performPerLineOffsetAndLength

private void performPerLineOffsetAndLength(java.io.RandomAccessFile raFile,
                                           long fileLength)
                                    throws ActivitySpecificationException,
                                           java.io.IOException
Perform a replacement of text within a file, on a per-line basis, locating the text based on a range of bytes uniquely defined by an offset and a length.

Parameters:
raFile - The file to replace text within
fileLength - The length of the file
Throws:
ActivitySpecificationException - If the offset or length are invalid for the requested operation.
java.io.IOException - If there's a problem accessing the file

performPerFileOffsetAndLength

private void performPerFileOffsetAndLength(java.io.RandomAccessFile raFile,
                                           long fileLength)
                                    throws ActivitySpecificationException,
                                           java.io.IOException
Perform a replacement of text within a file, on a per-file basis, locating the text based on a range of bytes uniquely defined by an offset and a length.

Parameters:
raFile - The file to replace text within
fileLength - The length of the file
Throws:
ActivitySpecificationException - If the offset or length are invalid for the requested operation.
java.io.IOException - If there's a problem accessing the file

performSubstitutions

private java.lang.String performSubstitutions(java.lang.String string,
                                              AbstractMatcher matcher)
                                       throws MalformedReplacementStringException
Return a string equal to the provided string, but with group variables substituted with their actual value, in the context of a particular regular expression match.

Groups are defined by bracketing in a regular expression, and are numbered from left-to-right. Group 0 denotes the entire regular expression. Groups may be referenced by group variables in a string, of the form $0, $1, etc. These may also be written ${0}, ${1}, etc., in order to disambiguate the end of the group variable and subsequent digits in the string.

Consider the following example:

Parameters:
string - Text which may contain references to groups by referring to $n
matcher - The matcher object which has already found a match
Returns:
the provided string but with group references replaced by the strings matched
Throws:
MalformedReplacementStringException - If there was a syntax error in the string

replaceStringInString

private java.lang.String replaceStringInString(java.lang.String hostString,
                                               int offset,
                                               int length,
                                               java.lang.String replacement)
Replaces a substring within a string with another string. For example, replacing the substring from offset 1 with length 2 of the string hello with the string yes will result in the string hyeslo.

Parameters:
hostString - The string within which to replace the substring
offset - The offset of the substring to replace, within hostString
length - The length of the substring to replace
replacement - The string with which to replace the substring
Returns:
the original string with the substring replaced.

getStepSize

private int getStepSize(long length)
Returns the step size, for reading a chunk of data of total size length. This will depend on whether the user has chosen to optimise for minimal speed or for minimal memory usage.

Parameters:
length - The total length of the data chunk
Returns:
the step size

performAction

private void performAction(java.io.RandomAccessFile raFile,
                           int type,
                           long offset,
                           long length,
                           boolean perLine,
                           java.lang.String string)
                    throws java.io.IOException
Perform an action on a file. The action may be one of: deleting bytes, replacing bytes, inserting bytes, or appending bytes.

Parameters:
raFile - The file within which to perform the action
type - The action to perform. One of DELETE, REPLACE, INSERT, APPEND
offset - The offset at which delete, replace and insert will operate
length - The length of the selection of bytes used by delete and replace
perLine - true indicates that this action is being executed on a per-line basis, throughout the file, false indicates that this action is being executed once for the entire file.
string - The string used by replace, insert and append
Throws:
java.io.IOException - If there's a problem accessing the file

append

private void append(java.io.RandomAccessFile raFile,
                    java.lang.String string)
             throws java.io.IOException
Append a string to the end of a file, thereby extending its length.

Semantically equivalent to calling method replace with arguments raFile, raFile.length(), 0, string.

Parameters:
raFile - The file to append to
string - The string to append
Throws:
java.io.IOException - If there's a problem accessing the file

insert

private void insert(java.io.RandomAccessFile raFile,
                    long offset,
                    java.lang.String string)
             throws java.io.IOException
Insert a string into a particular place in a file, causing the contents of the file after that place to be shifted away from the start of the file by an amount equal to the length of the string.

Semantically equivalent to calling method replace with arguments raFile, offset, 0, string.

Parameters:
raFile - The file to insert the string into
offset - The offset at which to insert the string
string - TThe string to insert
Throws:
java.io.IOException - If there's a problem accessing the file

delete

private void delete(java.io.RandomAccessFile raFile,
                    long offset,
                    long length)
             throws java.io.IOException
Delete a number of contiguous bytes from a file, starting at a particular offset. The bytes which appear at an offset greater than (offset + length) will be shifted towards the start of the file by an amount equal to length.

Semantically equivalent to calling method replace with arguments raFile, offset, length, "".

Parameters:
raFile - The file to delete the bytes from
offset - The offset at which to start deleting bytes
length - The number of bytes to delete
Throws:
java.io.IOException - If there's a problem accessing the file

replace

private void replace(java.io.RandomAccessFile raFile,
                     long offset,
                     long length,
                     java.lang.String string)
              throws java.io.IOException
Replace a sequence of contiguous bytes in a file with a string. The string may be longer, shorter, or of equal length to the bytes which are replaced. The rest of the file after the affected bytes is shifted forward or backward accordingly.

Semantically equivalent to calling method replaceString with arguments raFile, offset, length, string.

Parameters:
raFile - The file to replace bytes within
offset - The offset of the start of the sequence of bytes to replace
length - The number of bytes to replace
string - The string to replace the bytes with
Throws:
java.io.IOException - If there's a problem accessing the file

getStringInput

private java.lang.String getStringInput()
Returns the string used as the activity's input. This will come from either the output of another activity, or from a string provided in the perform document.

Returns:
the input string

obtainInput

private java.lang.String obtainInput()
Obtains all the data available from the activity providing its output to this activity, and returns it as a single string. If we have already obtained this (e.g. we are doing a per-line insertion of text), then return what was previously read in and cached. Warning! This method may cause an OutOfMemoryError since it reads all of the output from the previous activity into memory. Instead, this should be implemented in a streaming fashion.

Returns:
the previous activity's output

replaceString

private void replaceString(java.io.RandomAccessFile raFile,
                           long offset,
                           long length,
                           java.lang.String replacementString)
                    throws java.io.IOException
Replace a sequence of contiguous bytes in a file with a string. The string may be longer, shorter, or of equal length to the bytes which are replaced. The rest of the file after the affected bytes is shifted forward or backward accordingly.

Parameters:
raFile - The file to replace bytes within
offset - The offset of the start of the sequence of bytes to replace
length - The number of bytes to replace
replacementString - The string to replace the bytes with
Throws:
java.io.IOException - If there's a problem accessing the file

overwriteString

public static void overwriteString(java.io.RandomAccessFile raFile,
                                   long offset,
                                   java.lang.String replacementString)
                            throws java.io.IOException
Overwrite a sequence of bytes in a file with a replacement string. The bytes which are affected start at a given offset, and are equal in number to the length of the replacement string.

Parameters:
raFile - The file to overwrite bytes in
offset - The offset of the start of the bytes to overwrite
replacementString - The string to overwrite the bytes with
Throws:
java.io.IOException - If there's a problem accessing the file

replaceWithLongerString

public static void replaceWithLongerString(java.io.RandomAccessFile raFile,
                                           long offset,
                                           long length,
                                           java.lang.String replacementString,
                                           int step)
                                    throws java.io.IOException
Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly longer than the sequence of bytes.

This is achieved by performing the following three operations:

  1. Extend the length of the file by the excess in length.
  2. Starting from the end of the file and ending at the offset, shift the bytes toward the end of the file by a distance equal to the difference in length.
  3. Overwrite the replacement string at the specified offset.

Parameters:
raFile - The file within which to replace bytes
offset - The offset of the start of the bytes to replace
length - The number of bytes to replace
replacementString - The string to replace the bytes with, which must be longer than the number of bytes to replace
step - The number of bytes we shift at a time
Throws:
java.io.IOException - If there's a problem accessing the file

replaceWithShorterString

public static void replaceWithShorterString(java.io.RandomAccessFile raFile,
                                            long offset,
                                            long length,
                                            java.lang.String replacementString,
                                            int step)
                                     throws java.io.IOException
Replace a sequence of contiguous bytes in a file with a replacement string, which is strictly shorter than the sequence of bytes.

This is achieved by performing the following three operations:

  1. Overwrite the replacement string at the specified offset.
  2. Starting from the offset and ending at the end of the file, shift the bytes toward the start of the file by a distance equal to the difference in length.
  3. Truncate the length of the file by the difference in length.

Parameters:
raFile - The file within which to replace bytes
offset - The offset of the start of the bytes to replace
length - The number of bytes to replace
replacementString - The string to replace the bytes with, which must be shorter than the number of bytes to replace
step - The number of bytes we shift at a time
Throws:
java.io.IOException - If there's a problem accessing the file

getFileContents

public static java.lang.String getFileContents(java.io.RandomAccessFile raFile)
                                        throws java.io.IOException
Obtains the entire content of a file and returns it as a single string.

Parameters:
raFile - The file to get the content of
Returns:
the content of the file
Throws:
java.io.IOException - If there was a problem accessing the file

readLine

public java.lang.String readLine(java.io.RandomAccessFile raFile)
                          throws java.io.IOException
Read a line from the file

Parameters:
raFile - The file
Returns:
line
Throws:
java.io.IOException - If there was a problem accessing the file

charAt

public static int charAt(java.io.RandomAccessFile raFile,
                         long pos)
                  throws java.io.IOException
Returns the byte at a particular offset within a file. The file pointer is unaffected by calling this method.

Parameters:
raFile - The file
pos - The offset within the file
Returns:
the byte in the file at the offset
Throws:
java.io.IOException - If there was a problem accessing the file

getNextTenBytes

private static java.lang.String getNextTenBytes(java.io.RandomAccessFile raFile)
                                         throws java.io.IOException
Return the 35 bytes from the provided file located after the current file pointer. The file pointer is replaced in the same position it was in when this method was called.

Parameters:
raFile - The file to read the bytes from
Returns:
the next few bytes, as a String
Throws:
java.io.IOException - If there were problems accessing the file