public virtual void TestGetUInt8()
        {
            sbyte[]          buffer = new sbyte[] { unchecked ((int)(0x00)), unchecked ((int)(0x01)), unchecked ((sbyte)unchecked ((int)(0x7F))), unchecked ((sbyte)unchecked ((int)(0xFF))) };
            SequentialReader reader = CreateReader(buffer);

            Sharpen.Tests.AreEqual(0, reader.GetUInt8());
            Sharpen.Tests.AreEqual(1, reader.GetUInt8());
            Sharpen.Tests.AreEqual(127, reader.GetUInt8());
            Sharpen.Tests.AreEqual(255, reader.GetUInt8());
        }
 public virtual void TestGetUInt8_OutOfBounds()
 {
     try
     {
         SequentialReader reader = CreateReader(new sbyte[1]);
         reader.GetUInt8();
         reader.GetUInt8();
         NUnit.Framework.Assert.Fail("Exception expected");
     }
     catch (IOException ex)
     {
         Sharpen.Tests.AreEqual("End of data reached.", ex.Message);
     }
 }
		public virtual void Extract(SequentialReader reader, Com.Drew.Metadata.Metadata metadata)
		{
			GifHeaderDirectory directory = metadata.GetOrCreateDirectory<GifHeaderDirectory>();
			// FILE HEADER
			//
			// 3 - signature: "GIF"
			// 3 - version: either "87a" or "89a"
			//
			// LOGICAL SCREEN DESCRIPTOR
			//
			// 2 - pixel width
			// 2 - pixel height
			// 1 - screen and color map information flags (0 is LSB)
			//       0-2  Size of the global color table
			//       3    Color table sort flag (89a only)
			//       4-6  Color resolution
			//       7    Global color table flag
			// 1 - background color index
			// 1 - pixel aspect ratio
			reader.SetMotorolaByteOrder(false);
			try
			{
				string signature = reader.GetString(3);
				if (!signature.Equals("GIF"))
				{
					directory.AddError("Invalid GIF file signature");
					return;
				}
				string version = reader.GetString(3);
				if (!version.Equals(Gif87aVersionIdentifier) && !version.Equals(Gif89aVersionIdentifier))
				{
					directory.AddError("Unexpected GIF version");
					return;
				}
				directory.SetString(GifHeaderDirectory.TagGifFormatVersion, version);
				directory.SetInt(GifHeaderDirectory.TagImageWidth, reader.GetUInt16());
				directory.SetInt(GifHeaderDirectory.TagImageHeight, reader.GetUInt16());
				short flags = reader.GetUInt8();
				// First three bits = (BPP - 1)
				int colorTableSize = 1 << ((flags & 7) + 1);
				directory.SetInt(GifHeaderDirectory.TagColorTableSize, colorTableSize);
				if (version.Equals(Gif89aVersionIdentifier))
				{
					bool isColorTableSorted = (flags & 8) != 0;
					directory.SetBoolean(GifHeaderDirectory.TagIsColorTableSorted, isColorTableSorted);
				}
				int bitsPerPixel = ((flags & unchecked((int)(0x70))) >> 4) + 1;
				directory.SetInt(GifHeaderDirectory.TagBitsPerPixel, bitsPerPixel);
				bool hasGlobalColorTable = (flags & unchecked((int)(0xf))) != 0;
				directory.SetBoolean(GifHeaderDirectory.TagHasGlobalColorTable, hasGlobalColorTable);
				directory.SetInt(GifHeaderDirectory.TagTransparentColorIndex, reader.GetUInt8());
				int aspectRatioByte = reader.GetUInt8();
				if (aspectRatioByte != 0)
				{
					float pixelAspectRatio = (float)((aspectRatioByte + 15d) / 64d);
					directory.SetFloat(GifHeaderDirectory.TagPixelAspectRatio, pixelAspectRatio);
				}
			}
			catch (IOException)
			{
				directory.AddError("Unable to read BMP header");
			}
		}
		/// <summary>
		/// Performs the IPTC data extraction, adding found values to the specified instance of
		/// <see cref="Com.Drew.Metadata.Metadata"/>
		/// .
		/// </summary>
		public virtual void Extract(SequentialReader reader, Com.Drew.Metadata.Metadata metadata, long length)
		{
			IptcDirectory directory = metadata.GetOrCreateDirectory<IptcDirectory>();
			int offset = 0;
			// for each tag
			while (offset < length)
			{
				// identifies start of a tag
				short startByte;
				try
				{
					startByte = reader.GetUInt8();
					offset++;
				}
				catch (IOException)
				{
					directory.AddError("Unable to read starting byte of IPTC tag");
					return;
				}
				if (startByte != unchecked((int)(0x1c)))
				{
					directory.AddError("Invalid start to IPTC tag");
					return;
				}
				// we need at least five bytes left to read a tag
				if (offset + 5 >= length)
				{
					directory.AddError("Too few bytes remain for a valid IPTC tag");
					return;
				}
				int directoryType;
				int tagType;
				int tagByteCount;
				try
				{
					directoryType = reader.GetUInt8();
					tagType = reader.GetUInt8();
					tagByteCount = reader.GetUInt16();
					offset += 4;
				}
				catch (IOException)
				{
					directory.AddError("IPTC data segment ended mid-way through tag descriptor");
					return;
				}
				if (offset + tagByteCount > length)
				{
					directory.AddError("Data for tag extends beyond end of IPTC segment");
					return;
				}
				try
				{
					ProcessTag(reader, directory, directoryType, tagType, tagByteCount);
				}
				catch (IOException)
				{
					directory.AddError("Error processing IPTC tag");
					return;
				}
				offset += tagByteCount;
			}
		}
		/// <exception cref="System.IO.IOException"/>
		private void ProcessTag(SequentialReader reader, Com.Drew.Metadata.Directory directory, int directoryType, int tagType, int tagByteCount)
		{
			int tagIdentifier = tagType | (directoryType << 8);
			string @string = null;
			switch (tagIdentifier)
			{
				case IptcDirectory.TagApplicationRecordVersion:
				{
					// short
					int shortValue = reader.GetUInt16();
					reader.Skip(tagByteCount - 2);
					directory.SetInt(tagIdentifier, shortValue);
					return;
				}

				case IptcDirectory.TagUrgency:
				{
					// byte
					directory.SetInt(tagIdentifier, reader.GetUInt8());
					reader.Skip(tagByteCount - 1);
					return;
				}

				case IptcDirectory.TagReleaseDate:
				case IptcDirectory.TagDateCreated:
				{
					// Date object
					if (tagByteCount >= 8)
					{
						@string = reader.GetString(tagByteCount);
						try
						{
							int year = System.Convert.ToInt32(Sharpen.Runtime.Substring(@string, 0, 4));
							int month = System.Convert.ToInt32(Sharpen.Runtime.Substring(@string, 4, 6)) - 1;
							int day = System.Convert.ToInt32(Sharpen.Runtime.Substring(@string, 6, 8));
							DateTime date = new Sharpen.GregorianCalendar(year, month, day).GetTime();
							directory.SetDate(tagIdentifier, date);
							return;
						}
						catch (FormatException)
						{
						}
					}
					else
					{
						// fall through and we'll process the 'string' value below
						reader.Skip(tagByteCount);
					}
					goto case IptcDirectory.TagReleaseTime;
				}

				case IptcDirectory.TagReleaseTime:
				case IptcDirectory.TagTimeCreated:
				default:
				{
					break;
				}
			}
			// time...
			// fall through
			// If we haven't returned yet, treat it as a string
			// NOTE that there's a chance we've already loaded the value as a string above, but failed to parse the value
			if (@string == null)
			{
				@string = reader.GetString(tagByteCount, Runtime.GetProperty("file.encoding"));
			}
			// "ISO-8859-1"
			if (directory.ContainsTag(tagIdentifier))
			{
				// this fancy string[] business avoids using an ArrayList for performance reasons
				string[] oldStrings = directory.GetStringArray(tagIdentifier);
				string[] newStrings;
				if (oldStrings == null)
				{
					newStrings = new string[1];
				}
				else
				{
					newStrings = new string[oldStrings.Length + 1];
					System.Array.Copy(oldStrings, 0, newStrings, 0, oldStrings.Length);
				}
				newStrings[newStrings.Length - 1] = @string;
				directory.SetStringArray(tagIdentifier, newStrings);
			}
			else
			{
				directory.SetString(tagIdentifier, @string);
			}
		}
		public static JpegSegmentData ReadSegments(SequentialReader reader, Iterable<JpegSegmentType> segmentTypes)
		{
			// Must be big-endian
			System.Diagnostics.Debug.Assert((reader.IsMotorolaByteOrder()));
			// first two bytes should be JPEG magic number
			int magicNumber = reader.GetUInt16();
			if (magicNumber != unchecked((int)(0xFFD8)))
			{
				throw new JpegProcessingException("JPEG data is expected to begin with 0xFFD8 (ГїГ�) not 0x" + Sharpen.Extensions.ToHexString(magicNumber));
			}
			ICollection<sbyte> segmentTypeBytes = null;
			if (segmentTypes != null)
			{
				segmentTypeBytes = new HashSet<sbyte>();
				foreach (JpegSegmentType segmentType in segmentTypes)
				{
					segmentTypeBytes.Add(segmentType.byteValue);
				}
			}
			JpegSegmentData segmentData = new JpegSegmentData();
			do
			{
				// next byte is the segment identifier: 0xFF
				short segmentIdentifier = reader.GetUInt8();
				if (segmentIdentifier != unchecked((int)(0xFF)))
				{
					throw new JpegProcessingException("Expected JPEG segment start identifier 0xFF, not 0x" + Sharpen.Extensions.ToHexString(segmentIdentifier));
				}
				// next byte is the segment type
				sbyte segmentType = reader.GetInt8();
				if (segmentType == SegmentSos)
				{
					// The 'Start-Of-Scan' segment's length doesn't include the image data, instead would
					// have to search for the two bytes: 0xFF 0xD9 (EOI).
					// It comes last so simply return at this point
					return segmentData;
				}
				if (segmentType == MarkerEoi)
				{
					// the 'End-Of-Image' segment -- this should never be found in this fashion
					return segmentData;
				}
				// next 2-bytes are <segment-size>: [high-byte] [low-byte]
				int segmentLength = reader.GetUInt16();
				// segment length includes size bytes, so subtract two
				segmentLength -= 2;
				if (segmentLength < 0)
				{
					throw new JpegProcessingException("JPEG segment size would be less than zero");
				}
				// Check whether we are interested in this segment
				if (segmentTypeBytes == null || segmentTypeBytes.Contains(segmentType))
				{
					sbyte[] segmentBytes = reader.GetBytes(segmentLength);
					System.Diagnostics.Debug.Assert((segmentLength == segmentBytes.Length));
					segmentData.AddSegment(segmentType, segmentBytes);
				}
				else
				{
					// Some if the JPEG is truncated, just return what data we've already gathered
					if (!reader.TrySkip(segmentLength))
					{
						return segmentData;
					}
				}
			}
			while (true);
		}