public static void SetLength_NegativeValue() { using (MemoryStream underlying = new MemoryStream()) using (BufferedStream stream = new BufferedStream(underlying)) { Assert.Throws<ArgumentOutOfRangeException>(() => stream.SetLength(-1)); stream.SetLength(1); Assert.Equal(1, underlying.Length); Assert.Equal(1, stream.Length); } }
public void SetLengthWithClosedBaseStream () { string fn = Path.Combine (TempFolder, "temp"); try { FileStream fs = new FileStream (fn, FileMode.Create); BufferedStream bs = new BufferedStream (fs); fs.Close (); bs.SetLength (1000); } finally { File.Delete (fn); } }
/* Map File Format The first 65536 bytes of the map file is the graphical portion of the map. The next 65536 bytes appears to be the map that is used for path finding. The next 4 bytes is the number of markers on the map. The markers than follow here. If there are no markers than nothing is beyond the marker count bytes. Marker Format The first byte appears to be the x position The second byte appears to be the map tile it is in on the x axis Two blank bytes The next byte appears to the y position The next byte appears to be the map tile it is in on the y axis Two blank bytes The next 4 bytes are the image id of the image The next 2 bytes are the size of the string that fallows The string of text for the marker */ /// <summary> /// Merges tibia maps together. The input files are only read from. /// </summary> /// <param name="serverType">The type of server (Real or OT)</param> /// <param name="outputDirectory">The directory where the merged maps will go.</param> /// <param name="inputDirectories">A list of directories that contain the tibia maps to be merged together.</param> /// <returns> /// Returns true if successful. If it returns false the files in the output /// directory may be corrupted and incorrect. /// </returns> public static bool Merge(Constants.ServerType serverType, string outputDirectory, params string[] inputDirectories) { if (inputDirectories.Length < 1) return false; string mask = null; if (serverType == Pokemon.Constants.ServerType.Real) mask = "1??1????.map"; else mask = "0??0????.map"; string[] files = Directory.GetFiles(inputDirectories[0], mask); try { foreach (string file in files) { File.Copy(file, outputDirectory + "/" + Path.GetFileName(file), true); } } catch { return false; } for (int i = 1; i < inputDirectories.Length; ++i) { files = Directory.GetFiles(inputDirectories[i]); foreach (string file in files) { if (!File.Exists(outputDirectory + "/" + Path.GetFileName(file))) { try { File.Copy(file, outputDirectory + "/" + Path.GetFileName(file)); } catch { return false; } } else { FileStream sourcefile = null; FileStream inputfile = null; BufferedStream sourcebuffered = null; BufferedStream inputbuffered = null; try { //Setup the streams and buffers sourcefile = new FileStream(outputDirectory + "/" + Path.GetFileName(file), FileMode.Open); inputfile = new FileStream(file, FileMode.Open); sourcebuffered = new BufferedStream(sourcefile); inputbuffered = new BufferedStream(inputfile); //Read and write the graphical data byte[] source = new byte[65536]; sourcebuffered.Read(source, 0, 65536); byte[] input = new byte[65536]; inputbuffered.Read(input, 0, 65536); Compare(source, input, 0, 65536); sourcebuffered.Seek(0, SeekOrigin.Begin); sourcebuffered.Write(source, 0, 65536); //Read and write the pathfinding data sourcebuffered.Seek(65536, SeekOrigin.Begin); inputbuffered.Seek(65536, SeekOrigin.Begin); sourcebuffered.Read(source, 0, 65536); inputbuffered.Read(input, 0, 65536); Compare(source, input, 0xfa, 65536); sourcebuffered.Seek(65536, SeekOrigin.Begin); sourcebuffered.Write(source, 0, 65536); //Read and write the marker data sourcebuffered.Seek(131072, SeekOrigin.Begin); byte[] sourcemarkercountbytes = new byte[4]; sourcebuffered.Read(sourcemarkercountbytes, 0, 4); int sourcemarkercount = BitConverter.ToInt32(sourcemarkercountbytes, 0); inputbuffered.Seek(131072, SeekOrigin.Begin); byte[] inputmarkercountbytes = new byte[4]; inputbuffered.Read(inputmarkercountbytes, 0, 4); int inputmarkercount = BitConverter.ToInt32(inputmarkercountbytes, 0); List<Marker> sourcemarkers = new List<Marker>(); for (int r = 0; r < sourcemarkercount; ++r) { byte[] data = new byte[12]; sourcebuffered.Read(data, 0, 12); byte[] stringlength = new byte[2]; sourcebuffered.Read(stringlength, 0, 2); int len = BitConverter.ToUInt16(stringlength, 0); byte[] str = new byte[len]; sourcebuffered.Read(str, 0, len); sourcebuffered.Seek(len, SeekOrigin.Current); Marker marker = new Marker(data, stringlength, str); sourcemarkers.Add(marker); } for (int r = 0; r < inputmarkercount; ++r) { byte[] data = new byte[12]; inputbuffered.Read(data, 0, 12); byte[] stringlength = new byte[2]; inputbuffered.Read(stringlength, 0, 2); int len = BitConverter.ToUInt16(stringlength, 0); byte[] str = new byte[len]; inputbuffered.Read(str, 0, len); inputbuffered.Seek(len, SeekOrigin.Current); Marker marker = new Marker(data, stringlength, str); //Make sure we arn't copying a marker that already exists if (!sourcemarkers.Contains(marker)) { sourcemarkercount++; byte[] write = marker.GetBytes(); sourcebuffered.SetLength(sourcebuffered.Length + write.Length); sourcebuffered.Seek(-write.Length, SeekOrigin.End); sourcebuffered.Write(write, 0, write.Length); } } sourcebuffered.Seek(131072, SeekOrigin.Begin); sourcemarkercountbytes = BitConverter.GetBytes(sourcemarkercount); sourcebuffered.Write(sourcemarkercountbytes, 0, 4); } catch { return false; } finally { if (sourcebuffered != null) sourcebuffered.Close(); if (inputbuffered != null) inputbuffered.Close(); if (sourcefile != null) sourcefile.Close(); if (inputfile != null) inputfile.Close(); } } } } return true; }
public void SetLength_Disposed () { BufferedStream stream = new BufferedStream (new MemoryStream ()); stream.Close (); stream.SetLength (16); }
public void PositionAfterSetLength () { BufferedStream stream = new BufferedStream (new MemoryStream ()); stream.SetLength (32); stream.Position = 32; stream.SetLength (16); Assert.AreEqual (16, stream.Position, "Position==16"); }
public void SetLengthException3 () { BufferedStream stream = new BufferedStream (mem); mem = null; // Strangely, this does not throw an exception on .NET 1.1 stream.SetLength (1); }
public void SetLengthException2 () { BufferedStream stream = new BufferedStream (mem); mem.Close (); stream.SetLength (1); }
public void SetLengthException () { BufferedStream stream = new BufferedStream (mem); stream.SetLength (-1); }
public void SetLength () { BufferedStream stream = new BufferedStream (mem); stream.Write (new byte [] {0,1,2,3,4,5}, 0, 6); Assert.AreEqual (6, stream.Length, "test#01"); stream.SetLength (60); Assert.AreEqual (60, stream.Length, "test#02"); stream.SetLength (2); Assert.AreEqual (2, stream.Length, "test#03"); }
public void SetLengthWithClosedBaseStream () { string path = TempFolder + Path.DirectorySeparatorChar + "temp"; StdioFileStream fs = new StdioFileStream (path, FileMode.Create); BufferedStream bs = new BufferedStream (fs); fs.Close (); bs.SetLength (1000); }
public void SetLengthWithClosedBaseStream () { StdioFileStream fs = new StdioFileStream ("temp", FileMode.Create); BufferedStream bs = new BufferedStream (fs); fs.Close (); bs.SetLength (1000); }
public void SetLength () { BufferedStream stream = new BufferedStream (mem); stream.Write (new byte [] {0,1,2,3,4,5}, 0, 6); AssertEquals ("test#01", 6, stream.Length); stream.SetLength (60); AssertEquals ("test#02", 60, stream.Length); stream.SetLength (2); AssertEquals ("test#03", 2, stream.Length); }