singrdk/base/Kernel/Singularity.Security/AccessControl/StringReader.sg

132 lines
4.5 KiB
Plaintext
Raw Normal View History

2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
namespace Microsoft.Singularity.Security.AccessControl
{
using System;
internal class StringReader : DataReader
{
private String s;
private int pos;
private int length;
public StringReader(String s)
{
2008-11-17 18:29:00 -05:00
if (s == null) {
2008-03-05 09:52:00 -05:00
throw new ArgumentNullException("s");
}
this.s = s;
this.length = s == null? 0: s.Length;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
s = null;
pos = 0;
length = 0;
}
// Returns the next available character without actually reading it from
// the underlying string. The current position of the StringReader is not
// changed by this operation. The returned value is -1 if no further
// characters are available.
public override int Peek()
{
2008-11-17 18:29:00 -05:00
if (s == null) {
2008-03-05 09:52:00 -05:00
throw new Exception("Reader closed");
}
if (pos == length) return -1;
return s[pos];
}
// Reads the next character from the underlying string. The returned value
// is -1 if no further characters are available.
public override int Read()
{
2008-11-17 18:29:00 -05:00
if (s == null) {
2008-03-05 09:52:00 -05:00
throw new Exception("Reader closed");
}
if (pos == length) return -1;
return s[pos++];
}
// Reads a block of characters. This method will read up to count
// characters from this StringReader into the buffer character
// array starting at position index. Returns the actual number of
// characters read, or zero if the end of the string is reached.
public override int Read(char[] buffer, int index, int count)
{
2008-11-17 18:29:00 -05:00
if (buffer == null)
2008-03-05 09:52:00 -05:00
throw new ArgumentNullException("Argument null buffer");
if (index < 0)
throw new ArgumentOutOfRangeException("Argument out of range");
if (count < 0)
throw new ArgumentOutOfRangeException("Argument out of range");
if (buffer.Length - index < count)
throw new ArgumentException("Invalid offset");
if (s == null)
throw new Exception("Reader closed");
int n = length - pos;
2008-11-17 18:29:00 -05:00
if (n > 0) {
2008-03-05 09:52:00 -05:00
if (n > count) n = count;
s.CopyTo(pos, buffer, index, n);
pos += n;
}
return n;
}
//| <include file='doc\StringReader.uex' path='docs/doc[@for="StringReader.ReadToEnd"]/*' />
public String ReadToEnd()
{
2008-11-17 18:29:00 -05:00
if (s == null) {
2008-03-05 09:52:00 -05:00
throw new Exception("Reader closed");
}
String result;
2008-11-17 18:29:00 -05:00
if (pos == 0)
2008-03-05 09:52:00 -05:00
result = s;
else
result = s.Substring(pos, length - pos);
pos = length;
return result;
}
// Reads a line. A line is defined as a sequence of characters followed by
// a carriage return ('\r'), a line feed ('\n'), or a carriage return
// immediately followed by a line feed. The resulting string does not
// contain the terminating carriage return and/or line feed. The returned
// value is null if the end of the underlying string has been reached.
//
public String ReadLine()
{
2008-11-17 18:29:00 -05:00
if (s == null) {
2008-03-05 09:52:00 -05:00
throw new Exception("Reader closed");
}
int i = pos;
while (i < length) {
char ch = s[i];
if (ch == '\r' || ch == '\n') {
String result = s.Substring(pos, i - pos);
pos = i + 1;
if (ch == '\r' && pos < length && s[pos] == '\n') pos++;
return result;
}
i++;
}
2008-11-17 18:29:00 -05:00
if (i > pos) {
2008-03-05 09:52:00 -05:00
String result = s.Substring(pos, i - pos);
pos = i;
return result;
}
return null;
}
}
}