/// <summary> /// Outputs the elements in memory into the specified output file. /// </summary> /// <param name="outputStream"></param> /// <returns></returns> public int Serialize(RandomAccessFile outputStream) { try { var elementCount = GetCount(); var startingLocation = (int)outputStream.getFilePointer(); outputStream.writeInt(elementCount); var location = (int)outputStream.getFilePointer(); for (int elementIndex = 0; elementIndex < elementCount; ++elementIndex) { var ordinal = (int)mElements[elementIndex].mType; outputStream.writeInt((int)mElements[elementIndex].mType); // Convert to ordinal location = (int)outputStream.getFilePointer(); outputStream.writeInt(mElements[elementIndex].mSize); location = (int)outputStream.getFilePointer(); outputStream.writeBytes(mElements[elementIndex].mText); location = (int)outputStream.getFilePointer(); } var bytesWritten = (int)outputStream.getFilePointer() - startingLocation; return(bytesWritten); } catch (Exception) { return(0); } }
public VariableLengthRecordFile(String folder, String filename) { mPathname = folder; mPathname += ("/"); mPathname += (filename); mPathname += (".dat"); mFileStream = new RandomAccessFile(mPathname); if (mFileStream.length() > 0) { mFileStream.seek(0); mBytesUsed = mFileStream.readInt(); } else { mBytesUsed = 0; mFileStream.writeInt(mBytesUsed); } }
/// <summary> /// Adds the record and returns the starting offset and number of bytes written. /// </summary> /// <param name="record"></param> /// <param name="dataFileOffset"></param> /// <param name="dataSizeBytes"></param> /// <returns></returns> public bool Append(VariableLengthRecord record, ref int dataFileOffset, ref int dataSizeBytes) { try { dataFileOffset = (int)mFileStream.length(); mFileStream.seek(dataFileOffset); // Seek to the end of the file dataSizeBytes = record.Serialize(mFileStream); // Convert the record to the stream mBytesUsed += dataSizeBytes; mFileStream.seek(0); mFileStream.writeInt(mBytesUsed); return(true); } catch (Exception) { return(false); } }