dad824cbba085cc269a30bf3c6af2af4df684bb4
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / ByteStack.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="ByteStack.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">[....]</owner>
6 //------------------------------------------------------------------------------
7
8 using System;
9
10 namespace System.Xml {
11
12 // This stack is designed to minimize object creation for the
13 // objects being stored in the stack by allowing them to be
14 // re-used over time.  It basically pushes the objects creating
15 // a high water mark then as Pop() is called they are not removed
16 // so that next time Push() is called it simply returns the last
17 // object that was already on the stack.
18
19     internal class ByteStack {
20         private byte[] stack;
21         private int growthRate;
22         private int top;
23         private int size;
24
25         public ByteStack(int growthRate) {
26             this.growthRate = growthRate;
27             top = 0;
28             stack = new byte[growthRate];
29             size = growthRate; 
30         }
31
32         public void Push(byte data) {
33             if (size == top) {
34                 byte[] newstack = new byte[size + growthRate];
35                 if (top > 0) {
36                     Buffer.BlockCopy(stack, 0, newstack, 0, top);
37                 }
38                 stack = newstack;
39                 size += growthRate;
40             }
41             stack[top++] = data;
42         }
43
44         public byte Pop() {
45             if (top > 0) {
46                 return stack[--top];
47             } else {
48                 return 0;
49             }
50         }
51
52         public byte Peek() {
53             if (top > 0) {
54                 return stack[top - 1];
55             } else {
56                 return 0;
57             }
58         }
59
60         public int Length {
61             get { 
62                 return top;
63             }
64         }
65     }
66 }