/** * <summary>Replace a key with another tag.</summary> * <param name="key">The key</param> * <param name="replacement">The data to replace the key</param> * <returns>If the replacement was successful.</returns> */ public bool ReplaceData(string key, ITag replacement) { if (!file.Exists) { return(false); } using (MemoryStream decompressed = new MemoryStream()) { using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) using (Stream compStream = GetDecompressStream(fs)) compStream.CopyTo(decompressed); KeyScout counter = InternalUtils.ScoutObjectData(decompressed.ToArray(), key, new KeyScout()); if (counter.GetEnd() == null) { return(false); } MemoryStream memStream = new MemoryStream(); BigBinaryWriter bbw = new BigBinaryWriter(memStream); replacement.WriteData(bbw); bbw.Close(); byte[] replaceReturn = InternalUtils.ReplaceSubObjectData(decompressed.ToArray(), counter, memStream.ToArray()); using (FileStream newFile = new FileStream(file.FullName, FileMode.Create, FileAccess.Write, FileShare.Write)) { newFile.SetLength(0); using (Stream stream = GetCompressStream(newFile)) stream.Write(replaceReturn, 0, replaceReturn.Length); } } return(true); }
/** * <inheritdoc/> */ public void WriteData(BigBinaryWriter dos) { dos.Write(GetID()); MemoryStream memStream = new MemoryStream(); BigBinaryWriter writer = new BigBinaryWriter(memStream); writer.Write((short) Encoding.UTF8.GetByteCount(name)); writer.Write(Encoding.UTF8.GetBytes(name)); writer.Write(value); dos.Write((int)writer.BaseStream.Length); writer.Close(); dos.Write(memStream.ToArray()); }
/** * <inheritdoc/> */ public void WriteData(BigBinaryWriter dos) { dos.Write(GetID()); MemoryStream memStream = new MemoryStream(); BigBinaryWriter writer = new BigBinaryWriter(memStream); writer.Write((short)Encoding.UTF8.GetByteCount(name)); writer.Write(Encoding.UTF8.GetBytes(name)); foreach (KeyValuePair <string, T> entry in value) { entry.Value.SetName(entry.Key); entry.Value.WriteData(writer); } dos.Write((int)writer.BaseStream.Length); writer.Close(); dos.Write(memStream.ToArray()); }
/** * <summary>Replace a key with another tag.</summary> * <param name="key">The key</param> * <param name="replacement">The data to replace the key</param> * <returns>If the replacement was successful.</returns> */ public bool ReplaceData(string key, ITag replacement) { KeyScout counter = InternalUtils.ScoutObjectData(memStream.ToArray(), key, new KeyScout()); if (counter.GetEnd() == null) { return(false); } MemoryStream tempStream = new MemoryStream(); BigBinaryWriter bbw = new BigBinaryWriter(tempStream); replacement.WriteData(bbw); bbw.Close(); byte[] replaceReturn = InternalUtils.ReplaceSubObjectData(memStream.ToArray(), counter, tempStream.ToArray()); memStream.SetLength(0); memStream.Position = 0; memStream.Write(replaceReturn, 0, replaceReturn.Length); return(true); }
/** * <inheritdoc/> */ public void WriteData(BigBinaryWriter dos) { dos.Write(GetID()); MemoryStream memStream = new MemoryStream(); BigBinaryWriter writer = new BigBinaryWriter(memStream); writer.Write((short)Encoding.UTF8.GetByteCount(name)); writer.Write(Encoding.UTF8.GetBytes(name)); string compressorName = ODSUtil.GetCompressorName(compressor); if (compressorName == null) { throw new ODSException("Unable to find compressor: " + compressor); } // Write the data for the compressor into the temp writer. writer.Write((short)Encoding.UTF8.GetByteCount(compressorName)); writer.Write(Encoding.UTF8.GetBytes(compressorName)); // Temporary Compression Stream MemoryStream memStreamTemp = new MemoryStream(); Stream compressedStream = compressor.GetCompressStream(memStreamTemp); BigBinaryWriter writerTemp = new BigBinaryWriter(compressedStream); foreach (ITag tag in value) { tag.WriteData(writerTemp); } writerTemp.Close(); writer.Write(memStreamTemp.ToArray()); dos.Write((int)writer.BaseStream.Length); writer.Close(); dos.Write(memStream.ToArray()); }
/** * <summary> * This method can append, delete, and set tags. * <para>A note on keys when appending: <code>ObjectOne.ObjectTwo.tagName</code> When appending data <c>tagName</c> will not be the actual tag name. * The tag name written to the file is the name of the specified tag in the value parameter. Any parent objects that do not exist will be created. For example: * <code>ObjectOne.ObjectTwo.NewObject.tagName</code> If in the example above <c>NewObject</c> does not exist, than the object will be created with the value tag inside * of it. Please see the wiki for a more detailed explanation on this.</para> * <para>When value is null, the specified key is deleted. <c>The key MUST exist or an {@link ODSException} will be thrown.</c></para> * </summary> * * <param name="key"> * The key of the tag to append, delete, or set. * <para>When appending the key does not need to exist. ObjectTags that don't exist will be created automatically.</para> * <para>When the key is set to "" (An empty string) than it is assumed you want to append to the parent file.</para> * <para>Valid Tags:</para> * <code> * <para>ObjectOne.tagToDelete</para> * <para>ObjectOne.NewObject.tagToAppend</para> * <para>ObjectOne.tagToSet</para> * </code> * </param> * * <param name="value">The tag to append or replace the key with. <para>If this parameter is null than the key will be deleted.</para></param> * */ public void Set(string key, ITag value) { if (value == null) { bool output = Delete(key); if (!output) { throw new ODSException("The key " + key + " does not exist!"); } return; } if (key == "") { Save(new List <ITag>() { value }); return; } byte[] uncompressed = memStream.ToArray(); KeyScout counter = InternalUtils.ScoutObjectData(uncompressed, key, new KeyScout()); if (counter.GetEnd() == null) { if (counter.GetChildren().Count < 1) { Append(value); return; } string existingKey = ""; foreach (KeyScoutChild child in counter.GetChildren()) { if (existingKey.Length != 0) { existingKey += "."; } existingKey += child.GetName(); } string newKey = key.Replace(existingKey + ".", ""); ITag currentData; if (newKey.Split('.').Length > 1) { ObjectTag output = null; ObjectTag curTag = null; List <string> keys = new List <string>(newKey.Split('.')); int i = 0; foreach (string s in keys) { if (i == 0) { output = new ObjectTag(s); curTag = output; } else if (i == keys.Count - 1) { curTag.AddTag(value); } else { ObjectTag tag = new ObjectTag(s); curTag.AddTag(tag); curTag = tag; } i++; } currentData = output; } else { currentData = value; } // Actually replace the data and write it to the file. MemoryStream tempStream = new MemoryStream(); BigBinaryWriter bbw = new BigBinaryWriter(tempStream); currentData.WriteData(bbw); bbw.Close(); byte[] outputArray = InternalUtils.SetSubObjectData(uncompressed, counter, tempStream.ToArray()); memStream.SetLength(0); memStream.Position = 0; memStream.Write(outputArray, 0, outputArray.Length); } else { MemoryStream tempStream = new MemoryStream(); BigBinaryWriter bbw = new BigBinaryWriter(tempStream); value.WriteData(bbw); bbw.Close(); byte[] replaceReturn = InternalUtils.ReplaceSubObjectData(uncompressed, counter, tempStream.ToArray()); memStream.SetLength(0); memStream.Position = 0; memStream.Write(replaceReturn, 0, replaceReturn.Length); } }