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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
// Copyright (C) 2004-2007 MySQL AB
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.IO;
using zlib;
using MySql.Data.MySqlClient.Properties;
using MySql.Data.Common;
namespace MySql.Data.MySqlClient
{
/// <summary>
/// Summary description for CompressedStream.
/// </summary>
internal class CompressedStream : Stream
{
// writing fields
private Stream baseStream;
private MemoryStream cache;
// reading fields
private byte[] localByte;
private byte[] inBuffer;
private byte[] lengthBytes;
private WeakReference inBufferRef;
private int inPos;
private int maxInPos;
private ZInputStream zInStream;
public CompressedStream(Stream baseStream)
{
this.baseStream = baseStream;
localByte = new byte[1];
lengthBytes = new byte[7];
cache = new MemoryStream();
inBufferRef = new WeakReference(inBuffer, false);
}
#region Properties
public override bool CanRead
{
get { return baseStream.CanRead; }
}
public override bool CanWrite
{
get { return baseStream.CanWrite; }
}
public override bool CanSeek
{
get { return baseStream.CanSeek; }
}
public override long Length
{
get { return baseStream.Length; }
}
public override long Position
{
get { return baseStream.Position; }
set { baseStream.Position = value; }
}
#endregion
public override void Close()
{
baseStream.Close();
base.Close();
}
public override void SetLength(long value)
{
throw new NotSupportedException(Resources.CSNoSetLength);
}
public override int ReadByte()
{
try
{
Read(localByte, 0, 1);
return localByte[0];
}
catch (EndOfStreamException)
{
return -1;
}
}
public override bool CanTimeout
{
get
{
return baseStream.CanTimeout;
}
}
public override int ReadTimeout
{
get
{
return baseStream.ReadTimeout;
}
set
{
baseStream.ReadTimeout = value;
}
}
public override int WriteTimeout
{
get
{
return baseStream.WriteTimeout;
}
set
{
baseStream.WriteTimeout = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException("buffer", Resources.BufferCannotBeNull);
if (offset < 0 || offset >= buffer.Length)
throw new ArgumentOutOfRangeException("offset", Resources.OffsetMustBeValid);
if ((offset + count) > buffer.Length)
throw new ArgumentException(Resources.BufferNotLargeEnough, "buffer");
if (inPos == maxInPos)
PrepareNextPacket();
int countToRead = Math.Min(count, maxInPos - inPos);
int countRead;
if (zInStream != null)
countRead = zInStream.read(buffer, offset, countToRead);
else
countRead = baseStream.Read(buffer, offset, countToRead);
inPos += countRead;
// release the weak reference
if (inPos == maxInPos)
{
zInStream = null;
if (!Platform.IsMono())
{
inBufferRef = new WeakReference(inBuffer, false);
inBuffer = null;
}
}
return countRead;
}
private void PrepareNextPacket()
{
MySqlStream.ReadFully(baseStream, lengthBytes, 0, 7);
int compressedLength = lengthBytes[0] + (lengthBytes[1] << 8) + (lengthBytes[2] << 16);
// lengthBytes[3] is seq
int unCompressedLength = lengthBytes[4] + (lengthBytes[5] << 8) +
(lengthBytes[6] << 16);
if (unCompressedLength == 0)
{
unCompressedLength = compressedLength;
zInStream = null;
}
else
{
ReadNextPacket(compressedLength);
MemoryStream ms = new MemoryStream(inBuffer);
zInStream = new ZInputStream(ms);
zInStream.maxInput = compressedLength;
}
inPos = 0;
maxInPos = unCompressedLength;
}
private void ReadNextPacket(int len)
{
if (!Platform.IsMono())
inBuffer = inBufferRef.Target as byte[];
if (inBuffer == null || inBuffer.Length < len)
inBuffer = new byte[len];
MySqlStream.ReadFully(baseStream, inBuffer, 0, len);
}
private MemoryStream CompressCache()
{
// small arrays almost never yeild a benefit from compressing
if (cache.Length < 50)
return null;
byte[] cacheBytes = cache.GetBuffer();
MemoryStream compressedBuffer = new MemoryStream();
ZOutputStream zos = new ZOutputStream(compressedBuffer, zlibConst.Z_DEFAULT_COMPRESSION);
zos.Write(cacheBytes, 0, (int) cache.Length);
zos.finish();
// if the compression hasn't helped, then just return null
if (compressedBuffer.Length >= cache.Length)
return null;
return compressedBuffer;
}
private void CompressAndSendCache()
{
long compressedLength, uncompressedLength;
// we need to save the sequence byte that is written
byte[] cacheBuffer = cache.GetBuffer();
byte seq = cacheBuffer[3];
cacheBuffer[3] = 0;
// first we compress our current cache
MemoryStream compressedBuffer = CompressCache();
// now we set our compressed and uncompressed lengths
// based on if our compression is going to help or not
MemoryStream memStream;
if (compressedBuffer == null)
{
compressedLength = cache.Length;
uncompressedLength = 0;
memStream = cache;
}
else
{
compressedLength = compressedBuffer.Length;
uncompressedLength = cache.Length;
memStream = compressedBuffer;
}
// Make space for length prefix (7 bytes) at the start of output
long dataLength = memStream.Length;
int bytesToWrite = (int)dataLength + 7;
memStream.SetLength(bytesToWrite);
byte[] buffer = memStream.GetBuffer();
Array.Copy(buffer, 0, buffer, 7, (int)dataLength);
// Write length prefix
buffer[0] = (byte) (compressedLength & 0xff);
buffer[1] = (byte) ((compressedLength >> 8) & 0xff);
buffer[2] = (byte) ((compressedLength >> 16) & 0xff);
buffer[3] = seq;
buffer[4] = (byte) (uncompressedLength & 0xff);
buffer[5] = (byte) ((uncompressedLength >> 8) & 0xff);
buffer[6] = (byte) ((uncompressedLength >> 16) & 0xff);
baseStream.Write(buffer, 0, bytesToWrite);
baseStream.Flush();
cache.SetLength(0);
}
public override void Flush()
{
if (!InputDone()) return;
CompressAndSendCache();
}
private bool InputDone()
{
// if we have not done so yet, see if we can calculate how many bytes we are expecting
if (cache.Length < 4) return false;
byte[] buf = cache.GetBuffer();
int expectedLen = buf[0] + (buf[1] << 8) + (buf[2] << 16);
if (cache.Length < (expectedLen + 4)) return false;
return true;
}
public override void WriteByte(byte value)
{
cache.WriteByte(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
cache.Write(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return baseStream.Seek(offset, origin);
}
}
}
|