/// <summary> /// CopyOfStream /// </summary> /// <returns></returns> public Stream CopyOfStream() { var tempStream = new TempFileStream(); stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(tempStream); tempStream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin); return(tempStream); }
/// <summary> /// Creates a ticket from a template in an XDocument. /// </summary> /// <param name="document"></param> /// <returns></returns> public static GeneratedTicketTemplateSelectionBuilder CreateFromTemplate(XDocument document) { var stream = new TempFileStream(); document.Save(stream); stream.Seek(0, SeekOrigin.Begin); return(CreateFromTemplate(stream)); }
/// <summary> /// Gets the stream associated with the part. /// </summary> public Stream CopyOfStream() { var tempStream = new TempFileStream(); Document.SaveHttpReady(tempStream); tempStream.Seek(0, SeekOrigin.Begin); return(tempStream); }
/// <summary> /// Generate a tree for this template. /// </summary> /// <param name="vars">Name/value pairs.</param> /// <param name="makeIdsUnique">True to make ids in the file globally unique. If false, IDs will be taken from the template.</param> /// <param name="jobId">The new JobID value.</param> /// <returns>The tree containing the generated JDF.</returns> public XDocument Generate(Dictionary <string, object> vars, string jobId = null, bool makeIdsUnique = true) { ParameterCheck.ParameterRequired(vars, "vars"); XDocument tree = null; var buffStream = new TempFileStream(); StreamWriter writer = new StreamWriter(buffStream); try { items.Generate(writer, vars); writer.Flush(); buffStream.Seek(0, SeekOrigin.Begin); if (makeIdsUnique) { tree = GenerateNewIds(buffStream, jobId); } else { tree = XDocument.Load(buffStream); } } finally { writer.Close(); buffStream.Close(); } if (tree.XmlType() == XmlType.Jdf) { //the instance document is not a template so set Template to false in the root tree.Root.SetAttributeValue("Template", "false"); } RemoveNullValueAttributes(tree); return(tree); }
/// <summary> /// Validate the ticket and optionally add schema info (defaults and PVSI) /// </summary> /// <param name="addSchemaInfo">True adds default elements, default attributes and schema info to the ticket. False leaves this alone. Default is true.</param> /// <param name="workAroundMSBug">True works around an issue in the .NET framework that causes validation to work improperly on schema types that inherit /// from an abstract base class if the document is created via node authoring code instead of by Parse or Load methods. Default is true.</param> /// <returns></returns> public bool Validate(bool addSchemaInfo = true, bool workAroundMSBug = true) { //todo: this method should probably return ResultOf. ResultOf needs to be taken from connent and move to common to accomplish this properly. messages.Clear(); ticket.Document.Validate(SchemaSet.Instance.Schemas, (o, e) => messages.Add(new ValidationMessage(o, e.Severity, e.Message)), addSchemaInfo); if (workAroundMSBug) { using (var tempFileSream = new TempFileStream()) { messages.Clear(); ticket.Document.Save(tempFileSream); tempFileSream.Seek(0, SeekOrigin.Begin); var newDocument = XDocument.Load(tempFileSream); newDocument.Validate(SchemaSet.Instance.Schemas, (o, e) => messages.Add(new ValidationMessage(o, e.Severity, e.Message)), false); } } IsValid = messages.Where(m => m.ValidationMessageType == ValidationMessageType.Error).Count() == 0; Messages = new ReadOnlyCollection <ValidationMessage>(messages); HasValidatedAtLeastOnce = true; return(IsValid.Value); }
public void Read(Stream str) { this.Entries.Clear(); BigEndianBinaryReader bigEndianBinaryReader = new BigEndianBinaryReader(str); this.header.MagicNumber = bigEndianBinaryReader.ReadUInt32(); this.header.VersionNumber = bigEndianBinaryReader.ReadUInt32(); this.header.CompressionMethod = bigEndianBinaryReader.ReadUInt32(); this.header.TotalTOCSize = bigEndianBinaryReader.ReadUInt32(); this.header.TOCEntrySize = bigEndianBinaryReader.ReadUInt32(); this.header.numFiles = bigEndianBinaryReader.ReadUInt32(); this.header.blockSize = bigEndianBinaryReader.ReadUInt32(); this.header.archiveFlags = bigEndianBinaryReader.ReadUInt32(); var tocStream = str; BigEndianBinaryReader bigEndianBinaryReaderTOC = bigEndianBinaryReader; if (this.header.archiveFlags == 4) { var decStream = new TempFileStream(); using (var outputStream = new MemoryStream()) { RijndaelEncryptor.DecryptPSARC(str, outputStream, this.header.TotalTOCSize); int bytesRead; byte[] buffer = new byte[30000]; int decMax = (int)this.header.TotalTOCSize - 32; int decSize = 0; outputStream.Seek(0, SeekOrigin.Begin); while ((bytesRead = outputStream.Read(buffer, 0, buffer.Length)) > 0) { decSize += bytesRead; if (decSize > decMax) bytesRead = decMax - (decSize - bytesRead); decStream.Write(buffer, 0, bytesRead); } } decStream.Seek(0, SeekOrigin.Begin); str.Seek(this.header.TotalTOCSize, SeekOrigin.Begin); tocStream = decStream; bigEndianBinaryReaderTOC = new BigEndianBinaryReader(tocStream); } if (this.header.MagicNumber == 1347633490) { if (this.header.CompressionMethod == 2053925218) { byte b = 1; uint num = 256; do { num *= 256; b += 1; } while (num < this.header.blockSize); int num2 = 0; while (num2 < this.header.numFiles) { this.Entries.Add(new Entry { id = num2, MD5 = bigEndianBinaryReaderTOC.ReadBytes(16), zIndex = bigEndianBinaryReaderTOC.ReadUInt32(), Length = bigEndianBinaryReaderTOC.ReadUInt40(), Offset = bigEndianBinaryReaderTOC.ReadUInt40() }); num2++; } long decMax = (this.header.archiveFlags == 4) ? 32 : 0; uint num3 = (this.header.TotalTOCSize - (uint)(tocStream.Position + decMax)) / (uint)b; uint[] array = new uint[num3]; num2 = 0; while (num2 < num3) { switch (b) { case 2: array[num2] = (uint)bigEndianBinaryReaderTOC.ReadUInt16(); break; case 3: array[num2] = bigEndianBinaryReaderTOC.ReadUInt24(); break; case 4: array[num2] = bigEndianBinaryReaderTOC.ReadUInt32(); break; } num2++; } this.inflateEntries(bigEndianBinaryReader, array.ToArray<uint>(), this.header.blockSize); this.ReadNames(); } } //str.Flush(); }
public void Read(Stream str) { this.Entries.Clear(); BigEndianBinaryReader bigEndianBinaryReader = new BigEndianBinaryReader(str); this.header.MagicNumber = bigEndianBinaryReader.ReadUInt32(); this.header.VersionNumber = bigEndianBinaryReader.ReadUInt32(); this.header.CompressionMethod = bigEndianBinaryReader.ReadUInt32(); this.header.TotalTOCSize = bigEndianBinaryReader.ReadUInt32(); this.header.TOCEntrySize = bigEndianBinaryReader.ReadUInt32(); this.header.numFiles = bigEndianBinaryReader.ReadUInt32(); this.header.blockSize = bigEndianBinaryReader.ReadUInt32(); this.header.archiveFlags = bigEndianBinaryReader.ReadUInt32(); var tocStream = str; BigEndianBinaryReader bigEndianBinaryReaderTOC = bigEndianBinaryReader; if (this.header.archiveFlags == 4) { var decStream = new TempFileStream(); using (var outputStream = new MemoryStream()) { RijndaelEncryptor.DecryptPSARC(str, outputStream, this.header.TotalTOCSize); int bytesRead; byte[] buffer = new byte[30000]; int decMax = (int)this.header.TotalTOCSize - 32; int decSize = 0; outputStream.Seek(0, SeekOrigin.Begin); while ((bytesRead = outputStream.Read(buffer, 0, buffer.Length)) > 0) { decSize += bytesRead; if (decSize > decMax) { bytesRead = decMax - (decSize - bytesRead); } decStream.Write(buffer, 0, bytesRead); } } decStream.Seek(0, SeekOrigin.Begin); str.Seek(this.header.TotalTOCSize, SeekOrigin.Begin); tocStream = decStream; bigEndianBinaryReaderTOC = new BigEndianBinaryReader(tocStream); } if (this.header.MagicNumber == 1347633490) { if (this.header.CompressionMethod == 2053925218) { byte b = 1; uint num = 256; do { num *= 256; b += 1; }while (num < this.header.blockSize); int num2 = 0; while (num2 < this.header.numFiles) { this.Entries.Add(new Entry { id = num2, MD5 = bigEndianBinaryReaderTOC.ReadBytes(16), zIndex = bigEndianBinaryReaderTOC.ReadUInt32(), Length = bigEndianBinaryReaderTOC.ReadUInt40(), Offset = bigEndianBinaryReaderTOC.ReadUInt40() }); num2++; } long decMax = (this.header.archiveFlags == 4) ? 32 : 0; uint num3 = (this.header.TotalTOCSize - (uint)(tocStream.Position + decMax)) / (uint)b; uint[] array = new uint[num3]; num2 = 0; while (num2 < num3) { switch (b) { case 2: array[num2] = (uint)bigEndianBinaryReaderTOC.ReadUInt16(); break; case 3: array[num2] = bigEndianBinaryReaderTOC.ReadUInt24(); break; case 4: array[num2] = bigEndianBinaryReaderTOC.ReadUInt32(); break; } num2++; } this.inflateEntries(bigEndianBinaryReader, array.ToArray <uint>(), this.header.blockSize); this.ReadNames(); } } str.Flush(); }
/// <summary> /// Returns the data unmodified. (New <see cref="TempFileStream"/> for semantic equivalence) /// </summary> /// <param name='data'> /// The data to be (de)compressed. /// </param> private static System.IO.Stream NullHandler(System.IO.Stream data) { System.IO.Stream tfs = new TempFileStream(); data.CopyTo(tfs); tfs.Seek(0, System.IO.SeekOrigin.Begin); return tfs; }
/// <summary> /// Returns the data decompressed using Gzip. /// </summary> /// <param name='data'> /// The data to be decompressed. /// </param> private static System.IO.Stream DecompressGz(System.IO.Stream data) { System.IO.Compression.GZipStream gz = new System.IO.Compression.GZipStream(data, System.IO.Compression.CompressionMode.Decompress); TempFileStream tfs = new TempFileStream(); gz.CopyTo(tfs); tfs.Seek(0, System.IO.SeekOrigin.Begin); return tfs; }