本文整理匯總了C#中Sharpen.ByteArrayOutputStream.ToByteArray方法的典型用法代碼示例。如果您正苦於以下問題:C# ByteArrayOutputStream.ToByteArray方法的具體用法?C# ByteArrayOutputStream.ToByteArray怎麽用?C# ByteArrayOutputStream.ToByteArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Sharpen.ByteArrayOutputStream
的用法示例。
在下文中一共展示了ByteArrayOutputStream.ToByteArray方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: CreateTask
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
public static Couchbase.Lite.Document CreateTask(Database database, string title,
Bitmap image, string listId)
{
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
);
Calendar calendar = GregorianCalendar.GetInstance();
string currentTimeString = dateFormatter.Format(calendar.GetTime());
IDictionary<string, object> properties = new Dictionary<string, object>();
properties.Put("type", DocType);
properties.Put("title", title);
properties.Put("checked", false);
properties.Put("created_at", currentTimeString);
properties.Put("list_id", listId);
Couchbase.Lite.Document document = database.CreateDocument();
UnsavedRevision revision = document.CreateRevision();
revision.SetUserProperties(properties);
if (image != null)
{
ByteArrayOutputStream @out = new ByteArrayOutputStream();
image.Compress(Bitmap.CompressFormat.Jpeg, 50, @out);
ByteArrayInputStream @in = new ByteArrayInputStream(@out.ToByteArray());
revision.SetAttachment("image", "image/jpg", @in);
}
revision.Save();
return document;
}
示例2: TestWriteLine1
public virtual void TestWriteLine1()
{
RawText a = new RawText(Constants.EncodeASCII("foo-a\nfoo-b\n"));
ByteArrayOutputStream o = new ByteArrayOutputStream();
a.WriteLine(o, 0);
byte[] r = o.ToByteArray();
NUnit.Framework.Assert.AreEqual("foo-a", RawParseUtils.Decode(r));
}
示例3: DirCacheEntry
/// <exception cref="System.IO.IOException"></exception>
internal DirCacheEntry(byte[] sharedInfo, MutableInteger infoAt, InputStream @in,
MessageDigest md)
{
// private static final int P_CTIME_NSEC = 4;
// private static final int P_MTIME_NSEC = 12;
// private static final int P_DEV = 16;
// private static final int P_INO = 20;
// private static final int P_UID = 28;
// private static final int P_GID = 32;
info = sharedInfo;
infoOffset = infoAt.value;
IOUtil.ReadFully(@in, info, infoOffset, INFO_LEN);
int len;
if (IsExtended)
{
len = INFO_LEN_EXTENDED;
IOUtil.ReadFully(@in, info, infoOffset + INFO_LEN, INFO_LEN_EXTENDED - INFO_LEN);
if ((GetExtendedFlags() & ~EXTENDED_FLAGS) != 0)
{
throw new IOException(MessageFormat.Format(JGitText.Get().DIRCUnrecognizedExtendedFlags
, GetExtendedFlags().ToString()));
}
}
else
{
len = INFO_LEN;
}
infoAt.value += len;
md.Update(info, infoOffset, len);
int pathLen = NB.DecodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
int skipped = 0;
if (pathLen < NAME_MASK)
{
path = new byte[pathLen];
IOUtil.ReadFully(@in, path, 0, pathLen);
md.Update(path, 0, pathLen);
}
else
{
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
{
byte[] buf = new byte[NAME_MASK];
IOUtil.ReadFully(@in, buf, 0, NAME_MASK);
tmp.Write(buf);
}
for (; ; )
{
int c = @in.Read();
if (c < 0)
{
throw new EOFException(JGitText.Get().shortReadOfBlock);
}
if (c == 0)
{
break;
}
tmp.Write(c);
}
path = tmp.ToByteArray();
pathLen = path.Length;
skipped = 1;
// we already skipped 1 '\0' above to break the loop.
md.Update(path, 0, pathLen);
md.Update(unchecked((byte)0));
}
// Index records are padded out to the next 8 byte alignment
// for historical reasons related to how C Git read the files.
//
int actLen = len + pathLen;
int expLen = (actLen + 8) & ~7;
int padLen = expLen - actLen - skipped;
if (padLen > 0)
{
IOUtil.SkipFully(@in, padLen);
md.Update(nullpad, 0, padLen);
}
}
示例4: TestWriteLine3
public virtual void TestWriteLine3()
{
RawText a = new RawText(Constants.EncodeASCII("a\n\nb\n"));
ByteArrayOutputStream o = new ByteArrayOutputStream();
a.WriteLine(o, 1);
byte[] r = o.ToByteArray();
NUnit.Framework.Assert.AreEqual(string.Empty, RawParseUtils.Decode(r));
}
示例5: EncodeObject
//.........這裏部分代碼省略.........
/// The object is not GZip-compressed before being encoded.
/// <p>
/// Example options:<pre>
/// GZIP: gzip-compresses object before encoding it.
/// DO_BREAK_LINES: break lines at 76 characters
/// </pre>
/// <p>
/// Example: <code>encodeObject( myObj, Base64.GZIP )</code> or
/// <p>
/// Example: <code>encodeObject( myObj, Base64.GZIP | Base64.DO_BREAK_LINES )</code>
/// </remarks>
/// <param name="serializableObject">The object to encode</param>
/// <param name="options">Specified options</param>
/// <returns>The Base64-encoded object</returns>
/// <seealso cref="Gzip">Gzip</seealso>
/// <seealso cref="DoBreakLines">DoBreakLines</seealso>
/// <exception cref="System.IO.IOException">if there is an error</exception>
/// <since>2.0</since>
public static string EncodeObject(Serializable serializableObject, int options)
{
if (serializableObject == null)
{
throw new ArgumentNullException("Cannot serialize a null object.");
}
// end if: null
// Streams
ByteArrayOutputStream baos = null;
OutputStream b64os = null;
GZIPOutputStream gzos = null;
ObjectOutputStream oos = null;
try
{
// ObjectOutputStream -> (GZIP) -> Base64 -> ByteArrayOutputStream
baos = new ByteArrayOutputStream();
b64os = new Base64.OutputStream(baos, Encode | options);
if ((options & Gzip) != 0)
{
// Gzip
gzos = new GZIPOutputStream(b64os);
oos = new ObjectOutputStream(gzos);
}
else
{
// Not gzipped
oos = new ObjectOutputStream(b64os);
}
oos.WriteObject(serializableObject);
}
catch (IOException e)
{
// end try
// Catch it and then throw it immediately so that
// the finally{} block is called for cleanup.
throw;
}
finally
{
// end catch
try
{
oos.Close();
}
catch (Exception)
{
}
try
{
gzos.Close();
}
catch (Exception)
{
}
try
{
b64os.Close();
}
catch (Exception)
{
}
try
{
baos.Close();
}
catch (Exception)
{
}
}
// end finally
// Return value according to relevant encoding.
try
{
return Sharpen.Runtime.GetStringForBytes(baos.ToByteArray(), PreferredEncoding);
}
catch (UnsupportedEncodingException)
{
// end try
// Fall back to some Java default
return Sharpen.Runtime.GetStringForBytes(baos.ToByteArray());
}
}
示例6: TestParse_explicit_bad_encoded2
public virtual void TestParse_explicit_bad_encoded2()
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
b.Write(Sharpen.Runtime.GetBytesForString("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
, "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("type tree\n", "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("tag v1.2.3.4.5\n", "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("tagger F\u00f6r fattare <[email protected]> 1218123387 +0700\n"
, "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("encoding ISO-8859-1\n", "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("\n", "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("\u304d\u308c\u3044\n", "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("\n", "UTF-8"));
b.Write(Sharpen.Runtime.GetBytesForString("Hi\n", "UTF-8"));
RevTag c;
c = new RevTag(Id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
c.ParseCanonical(new RevWalk(db), b.ToByteArray());
NUnit.Framework.Assert.AreEqual("F\u00f6r fattare", c.GetTaggerIdent().GetName());
NUnit.Framework.Assert.AreEqual("\u304d\u308c\u3044", c.GetShortMessage());
NUnit.Framework.Assert.AreEqual("\u304d\u308c\u3044\n\nHi\n", c.GetFullMessage());
}
示例7: EncodeBytesToBytes
/// <summary>
/// Similar to
/// <see cref="EncodeBytes(byte[], int, int, int)">EncodeBytes(byte[], int, int, int)
/// </see>
/// but returns
/// a byte array instead of instantiating a String. This is more efficient
/// if you're working with I/O streams and have large data sets to encode.
/// </summary>
/// <param name="source">The data to convert</param>
/// <param name="off">Offset in array where conversion should begin</param>
/// <param name="len">Length of data to convert</param>
/// <param name="options">Specified options</param>
/// <returns>The Base64-encoded data as a String</returns>
/// <seealso cref="Gzip">Gzip</seealso>
/// <seealso cref="DoBreakLines">DoBreakLines</seealso>
/// <exception cref="System.IO.IOException">if there is an error</exception>
/// <exception cref="System.ArgumentNullException">if source array is null</exception>
/// <exception cref="System.ArgumentException">if source array, offset, or length are invalid
/// </exception>
/// <since>2.3.1</since>
public static byte[] EncodeBytesToBytes(byte[] source, int off, int len, int options
)
{
if (source == null)
{
throw new ArgumentNullException("Cannot serialize a null array.");
}
// end if: null
if (off < 0)
{
throw new ArgumentException("Cannot have negative offset: " + off);
}
// end if: off < 0
if (len < 0)
{
throw new ArgumentException("Cannot have length offset: " + len);
}
// end if: len < 0
if (off + len > source.Length)
{
throw new ArgumentException(string.Format("Cannot have offset of %d and length of %d with array of length %d"
, off, len, source.Length));
}
// end if: off < 0
// Compress?
if ((options & Gzip) != 0)
{
ByteArrayOutputStream baos = null;
GZIPOutputStream gzos = null;
Base64.OutputStream b64os = null;
try
{
// GZip -> Base64 -> ByteArray
baos = new ByteArrayOutputStream();
b64os = new Base64.OutputStream(baos, Encode | options);
gzos = new GZIPOutputStream(b64os);
gzos.Write(source, off, len);
gzos.Close();
}
catch (IOException e)
{
// end try
// Catch it and then throw it immediately so that
// the finally{} block is called for cleanup.
throw;
}
finally
{
// end catch
try
{
gzos.Close();
}
catch (Exception)
{
}
try
{
b64os.Close();
}
catch (Exception)
{
}
try
{
baos.Close();
}
catch (Exception)
{
}
}
// end finally
return baos.ToByteArray();
}
else
{
// end if: compress
// Else, don't compress. Better not to use streams at all then.
bool breakLines = (options & DoBreakLines) != 0;
//int len43 = len * 4 / 3;
//.........這裏部分代碼省略.........
示例8: ToByteArray
/// <exception cref="System.IO.IOException"></exception>
private static byte[] ToByteArray(ObjectId id)
{
ByteArrayOutputStream buf = new ByteArrayOutputStream(Constants.OBJECT_ID_LENGTH);
id.CopyRawTo(buf);
return buf.ToByteArray();
}
示例9: Build
/// <summary>Format this builder's state as an annotated tag object.</summary>
/// <remarks>Format this builder's state as an annotated tag object.</remarks>
/// <returns>
/// this object in the canonical annotated tag format, suitable for
/// storage in a repository.
/// </returns>
public virtual byte[] Build()
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET);
try
{
w.Write("object ");
GetObjectId().CopyTo(w);
w.Write('\n');
w.Write("type ");
w.Write(Constants.TypeString(GetObjectType()));
w.Write("\n");
w.Write("tag ");
w.Write(GetTag());
w.Write("\n");
if (GetTagger() != null)
{
w.Write("tagger ");
w.Write(GetTagger().ToExternalString());
w.Write('\n');
}
w.Write('\n');
if (GetMessage() != null)
{
w.Write(GetMessage());
}
w.Close();
}
catch (IOException err)
{
// This should never occur, the only way to get it above is
// for the ByteArrayOutputStream to throw, but it doesn't.
//
throw new RuntimeException(err);
}
return os.ToByteArray();
}
示例10: AssertFileContentsEqual
/// <exception cref="System.IO.IOException"></exception>
private void AssertFileContentsEqual(FilePath actFile, string @string)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FileInputStream fis = null;
byte[] buffer = new byte[100];
try
{
fis = new FileInputStream(actFile);
int read = fis.Read(buffer);
while (read > 0)
{
bos.Write(buffer, 0, read);
read = fis.Read(buffer);
}
string content = Sharpen.Runtime.GetStringForBytes(bos.ToByteArray(), "UTF-8");
NUnit.Framework.Assert.AreEqual(@string, content);
}
finally
{
if (fis != null)
{
fis.Close();
}
}
}
示例11: TestSmallObjectLoader
public virtual void TestSmallObjectLoader()
{
byte[] act = GetRng().NextBytes(512);
ObjectLoader ldr = new ObjectLoader.SmallObject(Constants.OBJ_BLOB, act);
NUnit.Framework.Assert.AreEqual(Constants.OBJ_BLOB, ldr.GetType());
NUnit.Framework.Assert.AreEqual(act.Length, ldr.GetSize());
NUnit.Framework.Assert.IsFalse(ldr.IsLarge(), "not is large");
NUnit.Framework.Assert.AreSame(act, ldr.GetCachedBytes());
NUnit.Framework.Assert.AreSame(act, ldr.GetCachedBytes(1));
NUnit.Framework.Assert.AreSame(act, ldr.GetCachedBytes(int.MaxValue));
byte[] copy = ldr.GetBytes();
NUnit.Framework.Assert.AreNotSame(act, copy);
NUnit.Framework.Assert.IsTrue(Arrays.Equals(act, copy), "same content");
copy = ldr.GetBytes(1);
NUnit.Framework.Assert.AreNotSame(act, copy);
NUnit.Framework.Assert.IsTrue(Arrays.Equals(act, copy), "same content");
copy = ldr.GetBytes(int.MaxValue);
NUnit.Framework.Assert.AreNotSame(act, copy);
NUnit.Framework.Assert.IsTrue(Arrays.Equals(act, copy), "same content");
ObjectStream @in = ldr.OpenStream();
NUnit.Framework.Assert.IsNotNull(@in, "has stream");
NUnit.Framework.Assert.IsTrue(@in is ObjectStream.SmallStream, "is small stream");
NUnit.Framework.Assert.AreEqual(Constants.OBJ_BLOB, @in.GetType());
NUnit.Framework.Assert.AreEqual(act.Length, @in.GetSize());
NUnit.Framework.Assert.AreEqual(act.Length, @in.Available());
NUnit.Framework.Assert.IsTrue(@in.MarkSupported(), "mark supported");
copy = new byte[act.Length];
NUnit.Framework.Assert.AreEqual(act.Length, @in.Read(copy));
NUnit.Framework.Assert.AreEqual(0, @in.Available());
NUnit.Framework.Assert.AreEqual(-1, @in.Read());
NUnit.Framework.Assert.IsTrue(Arrays.Equals(act, copy), "same content");
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
ldr.CopyTo(tmp);
NUnit.Framework.Assert.IsTrue(Arrays.Equals(act, tmp.ToByteArray()), "same content"
);
}
示例12: WriteRef
/// <summary>Overwrite (or create) a loose ref in the remote repository.</summary>
/// <remarks>
/// Overwrite (or create) a loose ref in the remote repository.
/// <p>
/// This method creates any missing parent directories, if necessary.
/// </remarks>
/// <param name="name">
/// name of the ref within the ref space, for example
/// <code>refs/heads/pu</code>.
/// </param>
/// <param name="value">new value to store in this ref. Must not be null.</param>
/// <exception cref="System.IO.IOException">
/// writing is not supported, or attempting to write the file
/// failed, possibly due to permissions or remote disk full, etc.
/// </exception>
internal virtual void WriteRef(string name, ObjectId value)
{
ByteArrayOutputStream b;
b = new ByteArrayOutputStream(Constants.OBJECT_ID_STRING_LENGTH + 1);
value.CopyTo(b);
b.Write('\n');
WriteFile(ROOT_DIR + name, b.ToByteArray());
}
示例13: ToArray
/// <exception cref="System.IO.IOException"></exception>
internal byte[] ToArray()
{
try
{
if (length >= 0)
{
byte[] r = new byte[(int)length];
IOUtil.ReadFully(@in, r, 0, r.Length);
return r;
}
ByteArrayOutputStream r_1 = new ByteArrayOutputStream();
byte[] buf = new byte[2048];
int n;
while ((n = @in.Read(buf)) >= 0)
{
r_1.Write(buf, 0, n);
}
return r_1.ToByteArray();
}
finally
{
@in.Close();
}
}
示例14: Decode
// COPY: Copied from libcore.net.UriCodec
/// <param name="convertPlus">true to convert '+' to ' '.</param>
public static string Decode(string s, bool convertPlus, Encoding charset)
{
if (s.IndexOf('%') == -1 && (!convertPlus || s.IndexOf('+') == -1))
{
return s;
}
StringBuilder result = new StringBuilder(s.Length);
ByteArrayOutputStream @out = new ByteArrayOutputStream();
for (int i = 0; i < s.Length; )
{
char c = s[i];
if (c == '%')
{
do
{
if (i + 2 >= s.Length)
{
throw new ArgumentException("Incomplete % sequence at: " + i);
}
int d1 = HexToInt(s[i + 1]);
int d2 = HexToInt(s[i + 2]);
if (d1 == -1 || d2 == -1)
{
throw new ArgumentException("Invalid % sequence " + Sharpen.Runtime.Substring(s,
i, i + 3) + " at " + i);
}
@out.Write(unchecked((byte)((d1 << 4) + d2)));
i += 3;
}
while (i < s.Length && s[i] == '%');
result.Append(charset.GetString(@out.ToByteArray()));
@out.Reset();
}
else
{
if (convertPlus && c == '+')
{
c = ' ';
}
result.Append(c);
i++;
}
}
return result.ToString();
}
示例15: GetTotalLength
/// <summary>
/// Determines the total length of the multipart content (content length of
/// individual parts plus that of extra elements required to delimit the parts
/// from one another).
/// </summary>
/// <remarks>
/// Determines the total length of the multipart content (content length of
/// individual parts plus that of extra elements required to delimit the parts
/// from one another). If any of the @{link BodyPart}s contained in this object
/// is of a streaming entity of unknown length the total length is also unknown.
/// <p/>
/// This method buffers only a small amount of data in order to determine the
/// total length of the entire entity. The content of individual parts is not
/// buffered.
/// </remarks>
/// <returns>
/// total length of the multipart entity if known, <code>-1</code>
/// otherwise.
/// </returns>
public virtual long GetTotalLength()
{
long contentLen = 0;
foreach (FormBodyPart part in this.parts)
{
ContentBody body = part.GetBody();
long len = body.GetContentLength();
if (len >= 0)
{
contentLen += len;
}
else
{
return -1;
}
}
ByteArrayOutputStream @out = new ByteArrayOutputStream();
try
{
DoWriteTo(this.mode, @out, false);
byte[] extra = @out.ToByteArray();
return contentLen + extra.Length;
}
catch (IOException)
{
// Should never happen
return -1;
}
}