File: CompressedStream.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (155 lines) | stat: -rw-r--r-- 3,803 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace ByteFX.Data.MySqlClient
{
	/// <summary>
	/// Summary description for CompressedStream.
	/// </summary>
	internal class CompressedStream : Stream
	{
		private	Stream	srcStream;
		private byte[]	buffer;
		private	int		index;

		public CompressedStream( Stream src )
		{
			srcStream = src;
			index = 0;
			buffer = new byte[0];
		}

		#region Properties
		public override bool CanRead
		{
			get	{ return srcStream.CanRead; }
		}

		public override bool CanWrite
		{
			get	{ return srcStream.CanWrite; }
		}

		public override bool CanSeek
		{
			get	{ return srcStream.CanSeek; }
		}

		public override long Length
		{
			get { return srcStream.Length; }
		}

		public override long Position
		{
			get	{ return srcStream.Position; }
			set	{ srcStream.Position = value; }
		}
		#endregion

		public override void Close()
		{
			srcStream.Close();
			base.Close ();
		}

		public override void Flush()
		{
			srcStream.Flush();
		}

		public override void SetLength(long value)
		{
			srcStream.SetLength( value );
		}

		public override int ReadByte()
		{
			EnsureData(1);

			return (int)buffer[index++];
		}

		public override int Read(byte[] buffer, int offset, int count)
		{
			if (buffer == null)
				throw new ArgumentNullException( "buffer", "Buffer must not be null" );
			if (offset < 0 || offset >= buffer.Length)
				throw new ArgumentOutOfRangeException( "Offset must be a valid position in buffer" );
			if ((offset + count) > buffer.Length)
				throw new ArgumentException( "Buffer is not large enough to complete operation" );

			EnsureData( count );

			for (int i=0; i < count; i++)
				buffer[offset+i] = this.buffer[index++];

			return count;
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
			srcStream.Write( buffer, offset, count );
		}

		public override long Seek(long offset, SeekOrigin origin)
		{
			return srcStream.Seek( offset, origin );
		}

		private void ReadBuffer( Stream s, byte[] buf, int offset, int length )
		{
			while (length > 0)
			{
				int amountRead = s.Read( buf, offset, length );
				if (amountRead == 0)
				throw new MySqlException("Unexpected end of data encountered");
				length -= amountRead;
				offset += amountRead;
			}
		}

		private void ReadNextPacket()
		{
			// read off the uncompressed and compressed lengths
			int compressedLen = srcStream.ReadByte() + (srcStream.ReadByte() << 8) + 
				(srcStream.ReadByte() << 16);
			byte seq = (byte)srcStream.ReadByte();
			int unCompressedLen = srcStream.ReadByte() + (srcStream.ReadByte() << 8) + 
				(srcStream.ReadByte() << 16);

			// if the data is in fact compressed, then uncompress it
			byte[] unCompressedBuffer = null;
			if (unCompressedLen > 0) 
			{
				unCompressedBuffer = new byte[ unCompressedLen ];
				InflaterInputStream iis = new InflaterInputStream( srcStream );
				ReadBuffer( iis, unCompressedBuffer, 0, unCompressedLen );
			}
			else 
			{
				unCompressedBuffer = new byte[ compressedLen ];
				ReadBuffer( srcStream, unCompressedBuffer, 0, compressedLen );
			}

			// now join this buffer to our existing one
			int left = buffer.Length - index;
			byte[] newBuffer = new byte[ left + unCompressedBuffer.Length ];

			int newIndex = 0;
			// first copy in the rest of the original
			for (int i=index; i < buffer.Length; i++)
				newBuffer[newIndex++] = buffer[i];
			unCompressedBuffer.CopyTo( newBuffer, newIndex );
			buffer = newBuffer;
			index = 0;
		}

		private void EnsureData( int size )
		{
			while ((buffer.Length - index) < size)
				ReadNextPacket();
		}
	}
}