ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Static Public Member Functions | List of all members
ImageResizer.ExtensionMethods.StreamExtensions Class Reference

Provides extension methods for copying streams More...

Static Public Member Functions

static MemoryStream CopyToMemoryStream (Stream s)
 Copies the remaining data in the current stream to a new MemoryStream instance. More...
 
static MemoryStream CopyToMemoryStream (Stream s, bool entireStream)
 Copies the current stream into a new MemoryStream instance. More...
 
static MemoryStream CopyToMemoryStream (Stream s, bool entireStream, int chunkSize)
 Copies the current stream into a new MemoryStream instance. More...
 
static byte[] CopyToBytes (Stream s)
 Copies the remaining data in the current stream to a byte[] array of exact size. More...
 
static byte[] CopyToBytes (Stream s, bool entireStream)
 Copies the current stream into a byte[] array of exact size. More...
 
static void CopyToStream (Stream s, Stream other)
 Copies the remaining data from the this stream into the given stream. More...
 
static void CopyToStream (Stream s, Stream other, bool entireStream)
 Copies this stream into the given stream More...
 
static void CopyToStream (Stream src, Stream dest, bool entireStream, int chunkSize)
 Copies this stream into the given stream More...
 
static byte[] CopyToBytes (Stream src, bool entireStream, int chunkSize)
 Copies the current stream into a byte[] array of exact size More...
 
static byte[] CopyOrReturnBuffer (Stream src, out long length, bool entireStream, int chunkSize)
 Attempts to return a byte[] array containing the remaining portion of the stream. Unlike CopyToBytes(), does not return a byte[] array of exact length, and may re-use the actual Stream's byte array, making it unsafe to write to in the future. More...
 

Detailed Description

Provides extension methods for copying streams

Definition at line 12 of file Stream.cs.

Member Function Documentation

static byte [] ImageResizer.ExtensionMethods.StreamExtensions.CopyOrReturnBuffer ( Stream  src,
out long  length,
bool  entireStream,
int  chunkSize 
)
inlinestatic

Attempts to return a byte[] array containing the remaining portion of the stream. Unlike CopyToBytes(), does not return a byte[] array of exact length, and may re-use the actual Stream's byte array, making it unsafe to write to in the future.

Parameters
src
length
chunkSize
Returns

Definition at line 189 of file Stream.cs.

Referenced by ImageResizer.Plugins.WicBuilder.WicBuilderPlugin.BuildJob().

189  {
190  if (src is MemoryStream) {
191  if (entireStream || src.Position == 0) {
192  length = src.Length;
193  //Slice from
194  MemoryStream ms = src as MemoryStream;
195  try {
196  return ms.GetBuffer();
197  } catch (UnauthorizedAccessException) //If we can't slice it, then we read it like a normal stream
198  {
199  return ms.ToArray();
200  }
201  } else {
202  byte[] buf = CopyToBytes(src, entireStream, chunkSize);
203  length = buf.Length;
204  return buf;
205  }
206  }else{
207  MemoryStream ms = CopyToMemoryStream(src, entireStream, chunkSize);
208  return CopyOrReturnBuffer(ms, out length, true, chunkSize);
209  }
210  }
static byte[] CopyOrReturnBuffer(Stream src, out long length, bool entireStream, int chunkSize)
Attempts to return a byte[] array containing the remaining portion of the stream. Unlike CopyToBytes(...
Definition: Stream.cs:189
static MemoryStream CopyToMemoryStream(Stream s)
Copies the remaining data in the current stream to a new MemoryStream instance.
Definition: Stream.cs:19
static byte[] CopyToBytes(Stream s)
Copies the remaining data in the current stream to a byte[] array of exact size.
Definition: Stream.cs:51
static byte [] ImageResizer.ExtensionMethods.StreamExtensions.CopyToBytes ( Stream  s)
inlinestatic

Copies the remaining data in the current stream to a byte[] array of exact size.

Parameters
s
Returns

Definition at line 51 of file Stream.cs.

51  {
52  return CopyToBytes(s, false);
53  }
static byte[] CopyToBytes(Stream s)
Copies the remaining data in the current stream to a byte[] array of exact size.
Definition: Stream.cs:51
static byte [] ImageResizer.ExtensionMethods.StreamExtensions.CopyToBytes ( Stream  s,
bool  entireStream 
)
inlinestatic

Copies the current stream into a byte[] array of exact size.

Parameters
s
entireStreamTrue to copy entire stream if seeakable, false to only copy remaining data
Returns

Definition at line 60 of file Stream.cs.

60  {
61  return CopyToBytes(s, entireStream, 0x1000);
62  }
static byte[] CopyToBytes(Stream s)
Copies the remaining data in the current stream to a byte[] array of exact size.
Definition: Stream.cs:51
static byte [] ImageResizer.ExtensionMethods.StreamExtensions.CopyToBytes ( Stream  src,
bool  entireStream,
int  chunkSize 
)
inlinestatic

Copies the current stream into a byte[] array of exact size

Parameters
src
entireStreamTrue to copy entire stream if seeakable, false to only copy remaining data.
chunkSizeThe buffer size to use (in bytes) if a buffer is required. Default: 4KiB
Returns

Definition at line 134 of file Stream.cs.

134  {
135  byte[] bytes;
136  if (src is MemoryStream) {
137  //Slice from
138  MemoryStream ms = src as MemoryStream;
139  try {
140  byte[] buffer = ms.GetBuffer();
141  long pos = entireStream ? 0 : src.Position;
142  long count = src.Length - pos;
143  bytes = new byte[count];
144  Array.Copy(buffer, pos, bytes, 0, count);
145  return bytes;
146  } catch (UnauthorizedAccessException) //If we can't slice it, then we read it like a normal stream
147  { }
148  if (entireStream || src.Position == 0) return ms.ToArray(); //Uses InternalBlockCopy, quite fast...
149  }
150 
151  if (src.CanSeek) {
152  long pos = entireStream ? 0 : src.Position;
153  if (entireStream) src.Seek(0, SeekOrigin.Begin);
154 
155  // Read the source file into a byte array.
156  int numBytesToRead = (int)(src.Length - pos);
157  bytes = new byte[numBytesToRead];
158  int numBytesRead = 0;
159  while (numBytesToRead > 0) {
160  // Read may return anything from 0 to numBytesToRead.
161  int n = src.Read(bytes, numBytesRead, numBytesToRead);
162 
163  // Break when the end of the file is reached.
164  if (n == 0)
165  break;
166 
167  numBytesRead += n;
168  numBytesToRead -= n;
169  }
170  Debug.Assert(numBytesRead == bytes.Length);
171  return bytes;
172  } else {
173  //No seeking, so we have to buffer to an intermediate memory stream
174  var ms = new MemoryStream();
175  CopyToStream(src, ms, entireStream, chunkSize);
176  return CopyToBytes(ms, true, chunkSize);
177  }
178  }
static void CopyToStream(Stream s, Stream other)
Copies the remaining data from the this stream into the given stream.
Definition: Stream.cs:68
static byte[] CopyToBytes(Stream s)
Copies the remaining data in the current stream to a byte[] array of exact size.
Definition: Stream.cs:51
static MemoryStream ImageResizer.ExtensionMethods.StreamExtensions.CopyToMemoryStream ( Stream  s)
inlinestatic

Copies the remaining data in the current stream to a new MemoryStream instance.

Parameters
s
Returns

Definition at line 19 of file Stream.cs.

19  {
20  return CopyToMemoryStream(s, false);
21  }
static MemoryStream CopyToMemoryStream(Stream s)
Copies the remaining data in the current stream to a new MemoryStream instance.
Definition: Stream.cs:19
static MemoryStream ImageResizer.ExtensionMethods.StreamExtensions.CopyToMemoryStream ( Stream  s,
bool  entireStream 
)
inlinestatic

Copies the current stream into a new MemoryStream instance.

Parameters
s
entireStreamTrue to copy entire stream if seeakable, false to only copy remaining data
Returns

Definition at line 28 of file Stream.cs.

28  {
29  return CopyToMemoryStream(s, entireStream,0x1000);
30  }
static MemoryStream CopyToMemoryStream(Stream s)
Copies the remaining data in the current stream to a new MemoryStream instance.
Definition: Stream.cs:19
static MemoryStream ImageResizer.ExtensionMethods.StreamExtensions.CopyToMemoryStream ( Stream  s,
bool  entireStream,
int  chunkSize 
)
inlinestatic

Copies the current stream into a new MemoryStream instance.

Parameters
s
entireStreamTrue to copy entire stream if seeakable, false to only copy remaining data
chunkSizeThe buffer size to use (in bytes) if a buffer is required. Default: 4KiB
Returns

Definition at line 39 of file Stream.cs.

39  {
40  MemoryStream ms = new MemoryStream(s.CanSeek ? ((int)s.Length + 8 - (entireStream ? 0 : (int)s.Position)) : chunkSize);
41  CopyToStream(s, ms, entireStream, chunkSize);
42  ms.Position = 0;
43  return ms;
44  }
static void CopyToStream(Stream s, Stream other)
Copies the remaining data from the this stream into the given stream.
Definition: Stream.cs:68
static void ImageResizer.ExtensionMethods.StreamExtensions.CopyToStream ( Stream  s,
Stream  other 
)
inlinestatic

Copies the remaining data from the this stream into the given stream.

Parameters
s
otherThe stream to write to

Definition at line 68 of file Stream.cs.

68  {
69  CopyToStream(s, other, false);
70  }
static void CopyToStream(Stream s, Stream other)
Copies the remaining data from the this stream into the given stream.
Definition: Stream.cs:68
static void ImageResizer.ExtensionMethods.StreamExtensions.CopyToStream ( Stream  s,
Stream  other,
bool  entireStream 
)
inlinestatic

Copies this stream into the given stream

Parameters
s
otherThe stream to write to
entireStreamTrue to copy entire stream if seeakable, false to only copy remaining data

Definition at line 77 of file Stream.cs.

77  {
78  CopyToStream(s, other, entireStream, 0x1000);
79  }
static void CopyToStream(Stream s, Stream other)
Copies the remaining data from the this stream into the given stream.
Definition: Stream.cs:68
static void ImageResizer.ExtensionMethods.StreamExtensions.CopyToStream ( Stream  src,
Stream  dest,
bool  entireStream,
int  chunkSize 
)
inlinestatic

Copies this stream into the given stream

Parameters
src
destThe stream to write to
entireStreamTrue to copy entire stream if seeakable, false to only copy remaining data
chunkSizeTrue to copy entire stream if seeakable, false to only copy remaining data

Definition at line 92 of file Stream.cs.

92  {
93  if (entireStream && src.CanSeek) src.Seek(0, SeekOrigin.Begin);
94 
95  if (src is MemoryStream && src.CanSeek) {
96  try {
97  int pos = (int)src.Position;
98  dest.Write(((MemoryStream)src).GetBuffer(), pos, (int)(src.Length - pos));
99  return;
100  } catch (UnauthorizedAccessException) //If we can't slice it, then we read it like a normal stream
101  { }
102  }
103  if (dest is MemoryStream && src.CanSeek) {
104  try {
105  int srcPos = (int)src.Position;
106  int pos = (int)dest.Position;
107  int length = (int)(src.Length - srcPos) + pos;
108  dest.SetLength(length);
109 
110  var data = ((MemoryStream)dest).GetBuffer();
111  while (pos < length) {
112  pos += src.Read(data, pos, length - pos);
113  }
114  return;
115  } catch (UnauthorizedAccessException) //If we can't write directly, fall back
116  { }
117  }
118  int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), chunkSize) : chunkSize;
119  byte[] buffer = new byte[size];
120  int n;
121  do {
122  n = src.Read(buffer, 0, buffer.Length);
123  dest.Write(buffer, 0, n);
124  } while (n != 0);
125  }

The documentation for this class was generated from the following file: