singrdk/base/Services/Iso9660/Iso9660FileStream.cs

170 lines
5.0 KiB
C#
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
using System;
using System.IO;
using Microsoft.Contracts;
using Microsoft.Singularity;
using Microsoft.Singularity.Channels;
namespace Iso9660
{
public class Iso9660FileStream {
private Iso9660FileInfo _info;
private long _pos = 0;
[NotDelayed]
public Iso9660FileStream(Iso9660FileInfo info, FileMode mode,
FileAccess access) {
_info = info;
_pos = 0;
}
~Iso9660FileStream() {}
public bool CanRead {
get {
return true;
}
}
public bool CanWrite {
get {
return false;
}
}
public bool CanSeek {
get {
return true;
}
}
public long Length {
get {
long size = (long)_info.size;
return size;
}
}
public long Position {
get {
return _pos;
}
}
public void Close() {
}
public int Read(ByteContainer buffer, int offset, int count) {
Tracing.Log(Tracing.Debug,"Entered");
long currentLength = (long)_info.size;
// just touch the file
2008-11-17 18:29:00 -05:00
if (count == 0 || _pos >= currentLength) {
2008-03-05 09:52:00 -05:00
return 0;
}
long bytesLeft;
if (currentLength - _pos > (long)count) {
bytesLeft = (long)count;
2008-11-17 18:29:00 -05:00
}
else {
2008-03-05 09:52:00 -05:00
bytesLeft = currentLength - _pos;
}
2008-11-17 18:29:00 -05:00
////
//Console.WriteLine("ReadArgs: count = " + args.count +
// " offset = " + args.offset +
// " bytes to be read " + bytesLeft);
//
2008-03-05 09:52:00 -05:00
if (buffer == null) {
//Console.WriteLine("ReadNFS: user buffer was null!");
throw new ArgumentNullException("Buffer given to Read was null");
2008-11-17 18:29:00 -05:00
}
else if ((long)buffer.Length < bytesLeft) {
2008-03-05 09:52:00 -05:00
throw new ArgumentException("Not enough room in buffer");
}
long uBufOff = (long)offset; // offset into user's buffer;
long curFileOff = _pos;
long blockno = -1;
2008-11-17 18:29:00 -05:00
while (bytesLeft > 0) {
2008-03-05 09:52:00 -05:00
long curBlk = _info.BlockFromByte(curFileOff);
long curBlkSize = Iso9660FileInfo.BlockSize(curBlk);
long curBlkOff = curFileOff % curBlkSize;
2008-11-17 18:29:00 -05:00
////
//Console.WriteLine("ReadArgs: curBlockNum " + curBlk +
// " curBlkSize = " + curBlkSize +
// " cur offset into blk " + curBlkOff);
//
2008-03-05 09:52:00 -05:00
if (blockno < curBlk) {
blockno = curBlk;
//
// 1. fetch data from media's block
//
Iso9660.Stdio.RawDevice.ReadBlock (_info.buf, (ulong)blockno);
}
long dist;
if (curBlkSize - curBlkOff > bytesLeft) {
dist = bytesLeft;
2008-11-17 18:29:00 -05:00
}
else {
2008-03-05 09:52:00 -05:00
dist = curBlkSize - curBlkOff;
}
//
// 2. store data into user's buffer
//
buffer.CopyTo (_info.buf, (int)curBlkOff, (int)uBufOff, (int)dist);
uBufOff += dist;
curFileOff += dist;
bytesLeft -= dist;
}
_pos += uBufOff;
return (int)uBufOff;
Tracing.Log(Tracing.Debug,"Exit");
}
public long Seek(long offset, System.IO.SeekOrigin origin) {
Tracing.Log(Tracing.Debug,"Entered");
2008-11-17 18:29:00 -05:00
switch (origin) {
2008-03-05 09:52:00 -05:00
case System.IO.SeekOrigin.Begin:
if (offset >= 0) {
_pos = offset;
}
break;
case System.IO.SeekOrigin.Current:
if (_pos + offset >= 0) {
_pos += offset;
}
break;
case System.IO.SeekOrigin.End:
long len = this.Length;
if (len + offset >= 0) {
_pos = len + offset;
}
break;
default:
throw new NotSupportedException("Unidentified SeekOrigin argument");
}
Tracing.Log(Tracing.Debug,"Exit");
return (long)_pos;
}
}
}