IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
StreamHeader.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2013 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Buffers;
26using System.IO;
27using System.Text;
28
30{
31
32 sealed class StreamHeader
33 {
34
35 internal uint Offset;
36 internal uint Size;
37 internal string Name;
38
39 internal void Read(BinaryReader br)
40 {
41 Offset = br.ReadUInt32();
42 Size = br.ReadUInt32();
43 Name = ReadName(br);
44 }
45
46#if NETFRAMEWORK
47
48 string ReadName(BinaryReader br)
49 {
50 var buf = ArrayPool<byte>.Shared.Rent(32);
51
52 try
53 {
54 byte b;
55 int len = 0;
56 while ((b = br.ReadByte()) != 0)
57 buf[len++] = b;
58
59 int padding = -1 + ((len + 4) & ~3) - len;
60 br.BaseStream.Seek(padding, SeekOrigin.Current);
61 return Encoding.UTF8.GetString(buf, 0, len);
62 }
63 finally
64 {
65 if (buf != null)
66 ArrayPool<byte>.Shared.Return(buf);
67 }
68 }
69
70#else
71
72 unsafe string ReadName(BinaryReader br)
73 {
74 var buf = (Span<byte>)stackalloc byte[32];
75 byte b;
76 int len = 0;
77 while ((b = br.ReadByte()) != 0)
78 buf[len++] = b;
79
80 int padding = -1 + ((len + 4) & ~3) - len;
81 br.BaseStream.Seek(padding, SeekOrigin.Current);
82 return Encoding.UTF8.GetString(buf.Slice(0, len));
83 }
84
85#endif
86
87 }
88
89}
global::java.lang.invoke.LambdaForm.Name Name