本文整理汇总了C#中System.IO.MemoryStream.SetLength方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.MemoryStream.SetLength方法的具体用法?C# System.IO.MemoryStream.SetLength怎么用?C# System.IO.MemoryStream.SetLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了System.IO.MemoryStream.SetLength方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Expand
/// <summary>
/// Expands the specified info.
/// </summary>
/// <param name="info">optional context and application specific information (can be a zero-length string)</param>
/// <param name="l">length of output keying material in octets (<= 255*HashLen)</param>
/// <returns>OKM (output keying material) of L octets</returns>
public byte[] Expand(byte[] info, int l)
{
if (info == null) info = new byte[0];
hmac.Key = this.prk;
var n = (int) System.Math.Ceiling(l * 1f / hashLength);
var t = new byte[n * hashLength];
using (var ms = new System.IO.MemoryStream())
{
var prev = new byte[0];
for (var i = 1; i <= n; i++)
{
ms.Write(prev, 0, prev.Length);
if (info.Length > 0) ms.Write(info, 0, info.Length);
ms.WriteByte((byte)(0x01 * i));
prev = hmac.ComputeHash(ms.ToArray());
Array.Copy(prev, 0, t, (i - 1) * hashLength, hashLength);
ms.SetLength(0); //reset
}
}
var okm = new byte[l];
Array.Copy(t, okm, okm.Length);
return okm;
}
示例2: GetBytes
public override byte[] GetBytes(int countBytes)
{
using (var okm = new System.IO.MemoryStream())
{
do
{
if (k_unused > 0)
{
var min = Math.Min(k_unused, countBytes);
okm.Write(k, hashLength - k_unused, min);
countBytes -= min;
k_unused -= min;
}
if (countBytes == 0) break;
var n = countBytes / hashLength;
if (countBytes % hashLength > 0) ++n;
using (var hmac_msg = new System.IO.MemoryStream())
{
for (var i = 1; i <= n; ++i)
{
hmac_msg.Write(k, 0, k.Length);
if (context != null)
hmac_msg.Write(context, 0, context.Length);
hmac_msg.WriteByte(counter);
checked { ++counter; };
k = hmac.ComputeHash(hmac_msg.GetBuffer(), 0, (int)hmac_msg.Length);
okm.Write(k, 0, i < n ? hashLength : countBytes);
countBytes -= hashLength;
hmac_msg.SetLength(0);
}
k_unused = -countBytes;
}// using hmac_msg
} while (false);
return okm.ToArray();
}// using okm
}
示例3: GetMessageBody
public override byte[] GetMessageBody()
{
using (System.IO.MemoryStream MS = new System.IO.MemoryStream ()) {
using (System.IO.BinaryWriter BW = new System.IO.BinaryWriter (MS)) {
BW.Write (requestID.ToByteArray ());
BW.Write (tables.Length);
for (int n = 0; n != tables.Length; n++) {
byte[] bytes = tables [n].Serialize ();
BW.Write (bytes.Length);
BW.Write (bytes);
}
if (exception == null) {
MS.WriteByte (0);
} else {
MS.WriteByte (1);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BF = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ();
long p = MS.Position;
try {
BF.Serialize (MS, exception);
} catch {
MS.Position = p;
BF.Serialize (MS, exception.ToString ());
MS.SetLength (MS.Position);
}
}
return MS.ToArray ();
}
}
}
示例4: writeNewCodestream
//.........这里部分代码省略.........
for (t = 0; t < numTiles; t++)
prem[t] = packetHeaders[t].Length;
// Write all PPM markers and information
for (tp = 0; tp < maxtp; tp++)
{
for (t = 0; t < numTiles; t++)
{
if (tileParts[t].Length > tp)
{
totNumPackets = packetHeaders[t].Length;
// Calculate number of packets in this tilepart
numPackets = (tp == tileParts[t].Length - 1)?prem[t]:pptp;
pStart = totNumPackets - prem[t];
pStop = pStart + numPackets;
// If Nppm value wont fit in current PPM marker segment
// write current PPM marker segment and start new
if (ppmLength + 4 > CSJ2K.j2k.codestream.Markers.MAX_LPPM)
{
// Write current PPM marker
temp = ppmMarkerSegment.ToArray();
length = temp.Length - 2;
temp[2] = (byte) (SupportClass.URShift(length, 8));
temp[3] = (byte) length;
fi.write(temp, 0, length + 2);
// Start new PPM marker segment
//UPGRADE_ISSUE: Method 'java.io.ByteArrayOutputStream.reset' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioByteArrayOutputStreamreset'"
//ppmMarkerSegment.reset();
ppmMarkerSegment.SetLength(0);
ppmMarkerSegment.WriteByte((System.Byte) SupportClass.URShift(CSJ2K.j2k.codestream.Markers.PPM, 8));
ppmMarkerSegment.WriteByte((System.Byte) (CSJ2K.j2k.codestream.Markers.PPM & 0x00FF));
ppmMarkerSegment.WriteByte((System.Byte) 0); // Temporary Lppm value
ppmMarkerSegment.WriteByte((System.Byte) 0); // Temporary Lppm value
ppmMarkerSegment.WriteByte((System.Byte) ppmIndex++); // zppm
ppmLength = 3;
}
// Write Nppm value
length = packetHeaderLengths[t][tp];
ppmMarkerSegment.WriteByte((System.Byte) SupportClass.URShift(length, 24));
ppmMarkerSegment.WriteByte((System.Byte) SupportClass.URShift(length, 16));
ppmMarkerSegment.WriteByte((System.Byte) SupportClass.URShift(length, 8));
ppmMarkerSegment.WriteByte((System.Byte) length);
ppmLength += 4;
// Write packet headers
for (p = pStart; p < pStop; p++)
{
length = packetHeaders[t][p].Length;
// If next packet header value wont fit in
// current PPM marker segment write current PPM
// marker segment and start new
if (ppmLength + length > CSJ2K.j2k.codestream.Markers.MAX_LPPM)
{
// Write current PPM marker
temp = ppmMarkerSegment.ToArray();
length = temp.Length - 2;
temp[2] = (byte) (SupportClass.URShift(length, 8));
temp[3] = (byte) length;
fi.write(temp, 0, length + 2);
示例5: createTileParts
//.........这里部分代码省略.........
while (np > 0)
{
phLength = packetHeaders[t][p].Length;
// If the total legth of the packet headers is greater
// than MAX_LPPT, several PPT markers are needed
if (pptLength + phLength > CSJ2K.j2k.codestream.Markers.MAX_LPPT)
{
temp.WriteByte((System.Byte) SupportClass.URShift(CSJ2K.j2k.codestream.Markers.PPT, 8));
temp.WriteByte((System.Byte) (CSJ2K.j2k.codestream.Markers.PPT & 0x00FF));
temp.WriteByte((System.Byte) SupportClass.URShift(pptLength, 8));
temp.WriteByte((System.Byte) pptLength);
temp.WriteByte((System.Byte) pptIndex++);
for (i = pIndex; i < p; i++)
{
temp.Write(packetHeaders[t][i], 0, packetHeaders[t][i].Length);
}
pptLength = 3; // Zppt and Lppt
pIndex = p;
}
pptLength += phLength;
p++;
np--;
}
// Write last PPT marker
temp.WriteByte((System.Byte) SupportClass.URShift(CSJ2K.j2k.codestream.Markers.PPT, 8));
temp.WriteByte((System.Byte) (CSJ2K.j2k.codestream.Markers.PPT & 0x00FF));
temp.WriteByte((System.Byte) SupportClass.URShift(pptLength, 8));
temp.WriteByte((System.Byte) pptLength);
temp.WriteByte((System.Byte) pptIndex);
for (i = pIndex; i < p; i++)
{
temp.Write(packetHeaders[t][i], 0, packetHeaders[t][i].Length);
}
}
pIndex = p;
np = nomnp;
// Write SOD marker
temp.WriteByte((System.Byte) SupportClass.URShift(CSJ2K.j2k.codestream.Markers.SOD, 8));
temp.WriteByte((System.Byte) (CSJ2K.j2k.codestream.Markers.SOD & 0x00FF));
// Write packet data and packet headers if PPT and PPM not used
for (p = tppStart; p < tppStart + np; p++)
{
if (!tempSop)
{
temp.Write(sopMarkSeg[t][p], 0, CSJ2K.j2k.codestream.Markers.SOP_LENGTH);
}
if (!(ppmUsed || pptUsed))
{
temp.Write(packetHeaders[t][p], 0, packetHeaders[t][p].Length);
}
temp.Write(packetData[t][p], 0, packetData[t][p].Length);
}
tppStart += np;
// Edit tile part header
tempByteArr = temp.ToArray();
tileParts[t][tilePart] = tempByteArr;
length = (int)temp.Length;
if (tilePart == 0)
{
// Edit first tile part header
tempByteArr[6] = (byte) (SupportClass.URShift(length, 24)); // Psot
tempByteArr[7] = (byte) (SupportClass.URShift(length, 16));
tempByteArr[8] = (byte) (SupportClass.URShift(length, 8));
tempByteArr[9] = (byte) (length);
tempByteArr[10] = (byte) SupportClass.Identity((0)); // TPsot
tempByteArr[11] = (byte) (numTileParts); // TNsot
}
else
{
// Edit tile part header
tempByteArr[0] = (byte) (SupportClass.URShift(CSJ2K.j2k.codestream.Markers.SOT, 8)); // SOT
tempByteArr[1] = (byte) (CSJ2K.j2k.codestream.Markers.SOT & 0x00FF);
tempByteArr[2] = (byte) SupportClass.Identity((0)); // Lsot
tempByteArr[3] = (byte) SupportClass.Identity((10));
tempByteArr[4] = (byte) (SupportClass.URShift(t, 8)); // Isot
tempByteArr[5] = (byte) (t); //
tempByteArr[6] = (byte) (SupportClass.URShift(length, 24)); // Psot
tempByteArr[7] = (byte) (SupportClass.URShift(length, 16));
tempByteArr[8] = (byte) (SupportClass.URShift(length, 8));
tempByteArr[9] = (byte) (length);
tempByteArr[10] = (byte) (tilePart); //TPsot
tempByteArr[11] = (byte) (numTileParts); // TNsot
}
//UPGRADE_ISSUE: Method 'java.io.ByteArrayOutputStream.reset' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioByteArrayOutputStreamreset'"
//temp.reset();
temp.SetLength(0);
prem -= np;
}
}
temp.Close();
}
示例6: Get
// GET /api/music/5
public HttpResponseMessage Get(Guid id, string play)
{
string musicPath = HttpContext.Current.Server.MapPath("~/Content/Music") + "\\" + id.ToString() + ".mp3";
if (System.IO.File.Exists(musicPath))
{
HttpResponseMessage result = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
System.IO.FileStream stream = new System.IO.FileStream(musicPath, System.IO.FileMode.Open);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.SetLength(stream.Length);
stream.Read(ms.GetBuffer(), 0, (int)stream.Length);
ms.Flush();
stream.Close();
stream.Dispose();
result.Content = new StreamContent(ms);
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("audio/mpeg");
return result;
}
else
{
return new HttpResponseMessage(System.Net.HttpStatusCode.NotFound);
}
}
示例7: SplitSql
private ArrayList SplitSql(string sql)
{
ArrayList commands = new ArrayList();
System.IO.MemoryStream ms = new System.IO.MemoryStream(sql.Length);
// first we tack on a semi-colon, if not already there, to make our
// sql processing code easier. Then we ask our encoder to give us
// the bytes for this sql string
byte[] bytes = connection.Encoding.GetBytes(sql + ";");
byte left_byte = 0;
bool escaped = false;
int parm_start=-1;
for (int x=0; x < bytes.Length; x++)
{
byte b = bytes[x];
// if we see a quote marker, then check to see if we are opening
// or closing a quote
if ((b == '\'' || b == '\"') && ! escaped )
{
if (b == left_byte) left_byte = 0;
else if (left_byte == 0) left_byte = b;
}
else if (b == '\\')
{
escaped = !escaped;
}
// if we see the marker for a parameter, then save its position and
// look for the end
else if (b == '@' && left_byte == 0 && ! escaped && parm_start==-1)
parm_start = x;
// if we see a space and we are tracking a parameter, then end the parameter and have
// that parameter serialize itself to the memory stream
else if (parm_start > -1 && (b != '@') && (b != '$') && (b != '_') && (b != '.') && ! Char.IsLetterOrDigit((char)b))
{
string parm_name = sql.Substring(parm_start, x-parm_start);
if (parameters.Contains( parm_name ))
{
MySqlParameter p = (parameters[parm_name] as MySqlParameter);
p.SerializeToBytes(ms, connection );
}
else
{
// otherwise assume system param. just write it out
byte[] buf = connection.Encoding.GetBytes(parm_name);
ms.Write(buf, 0, buf.Length);
}
parm_start=-1;
}
// if we are not in a string and we are not escaped and we are on a semi-colon,
// then write out what we have as a command
if (left_byte == 0 && ! escaped && b == ';' && ms.Length > 0)
{
bool goodcmd = false;
byte[] byteArray = ms.ToArray();
foreach (byte cmdByte in byteArray)
if (cmdByte != ' ') { goodcmd = true; break; }
if (goodcmd)
commands.Add( byteArray );
ms.SetLength(0);
}
else if (parm_start == -1)
ms.WriteByte(b);
// we want to write out the bytes in all cases except when we are parsing out a parameter
if (escaped && b != '\\') escaped = false;
}
return commands;
}
示例8: GetImage
public static System.Windows.Media.Imaging.BitmapImage GetImage(string fileId)
{
if (string.IsNullOrWhiteSpace(fileId))
{
if (_olaf == null)
{
_olaf = GetImage(Olaf);
}
return _olaf;
}
using (var db = new PetoeterDb(PetoeterDb.FileName))
{
var file = db.FileStorage.FindById(fileId);
if (file == null)
{
if (fileId == Olaf)
{
SaveFile(Olaf, @"olaf.png");
}
return null;
}
using(var reader = file.OpenRead())
{
System.IO.MemoryStream mem = new System.IO.MemoryStream();
mem.SetLength(file.Length);
reader.Read(mem.GetBuffer(), 0, (int)file.Length);
mem.Flush();
System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
image.BeginInit();
image.StreamSource = mem;
image.EndInit();
image.Freeze();
return image;
}
}
}