/*
CLASS {NATIVE_Stream_on_C_Stream|_CSharp}, NATIVE :

IMPORT, ANYWAY: {Stream} .
IMPORT, ANYWAY: {System_functions} .

NATIVE: "///copy NATIVE_Stream_on_C_Stream.cs" .

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


using AL4;

namespace AL4_project
{
    public class NATIVE_Stream_on_C_Stream : Stream
    {
        C_Stream C_S;
        
        public NATIVE_Stream_on_C_Stream(C_Stream cs)
        {
            C_S = cs;
        }
        
        public static Stream Stream_from_C_Stream(C_Stream CS)
        {
            Stream RESULT = new NATIVE_Stream_on_C_Stream(CS);
            return RESULT;
        }
        
        public override long Length
        {
            get
            { 
                C_Long_int.St_long 
                		size = (C_Long_int.St_long)C_S.Do.M_Size().Obj;
                return ((long)(size.f_Lo | (((long)size.f_Hi) << 32)));
            }
        }
        
        public override long Position
        {
            get 
            { 
            	C_Long_int.St_long 
            			pos = (C_Long_int.St_long)C_S.Do.M_Position().Obj;
            	return ((long)(pos.f_Lo | (((long)pos.f_Hi) << 32)));
            }
            set
            {
            	//C_Long_int.St_long new_pos = new C_Long_int.St_long();
            	C_Long_int.St_long new_pos 
            		= (C_Long_int.St_long)C_Long_int.St_long.New().Obj;
            	new_pos.f_Lo = (int)value;
            	new_pos.f_Hi = (int)(value >> 32);
                C_S.Do.M_Set_pos(new_pos.hard_link, 0);
                new_pos.hard_link.Ref_dec_hard();
            }
        }
        
        public override void SetLength(long Value)
        { }
        
        public override bool CanRead
        {
            get { return true; }
        }
        
        public override int Read(byte[] buffer, int offset, int count)
        {
            FixAByte_OnArray dest1 = new FixAByte_OnArray(buffer);
            FixAByte dest2 = new FixAByte(dest1, offset, offset + count - 1);
            return (int)C_S.Do.M_Read_bytes(dest2);
        }
        
        public override bool CanWrite
        {
            get { return false; }
        }
        
        public override void Write(byte[] buffer, int offset, int count)
        {  }
        
        public override void Flush()
        {  }
        
        public override bool CanSeek
        {
            get
            {
                return true;
            }
        }
        
        public override long Seek(long offset, SeekOrigin origin)
        {
        	C_Long_int.St_long off_to = new C_Long_int.St_long();
	        off_to.f_Lo = (int)offset;
	        off_to.f_Hi = (int)(offset >> 32);
            switch (origin)
            {
            case SeekOrigin.Begin:  C_S.Do.M_Set_pos(off_to.hard_link, 0); break;
            case SeekOrigin.Current:C_S.Do.M_Set_pos(off_to.hard_link, 1); break;
            case SeekOrigin.End:    C_S.Do.M_Set_pos(off_to.hard_link, 2); break;
            }
            off_to.hard_link.Ref_dec_hard();
	        C_Long_int.St_long new_pos 
	        		= (C_Long_int.St_long)C_S.Do.M_Position().Obj; 
	        long result = new_pos.f_Lo | (((long)new_pos.f_Hi) << 32);
	        new_pos.hard_link.Ref_dec_hard();
	        return result;
        }
        
        public override void Close()
        {
            C_S.Do.M_Close();
        }
    }
}
/*
END
*/

