dude
dude

Reputation: 29

streamreader/buffered stream C#

I have a class Value the output of Value is used as an input to other classes and eventually in Main. In Main a logic is performed and output is produced for first 512 bits. I want my program to return back to value() to start with next 512 bits of file.txt. How can I do that?

public static byte[] Value()
        {
            byte[] numbers = new byte[9999];
            using (FileStream fs = File.Open(@"C:\Users\file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))           
            using (BufferedStream bs = new BufferedStream(fs))
            using (StreamReader sr = new StreamReader(bs))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    for (int i = 0; i < 512; i++)
                    {
                        numbers[i] = Byte.Parse(line[i].ToString());
                    }
                }
            }         
            return numbers;
        }

Upvotes: 0

Views: 1499

Answers (3)

dude
dude

Reputation: 29

@TiGreX

 public static List<byte> ByteValueCaller()
        {
          List<byte> numbers = new List<byte>();

            GetValue(0, numbers);

            return numbers;
        }

        public static void GetValue(int startingByte, List<byte> numbers)
        {
            using (FileStream fs = File.Open(@"C:\Users\file1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (BinaryReader br = new BinaryReader(fs))
            {
                //64bytes == 512bit
                //determines if the last position to use is inside your stream, or if the last position is the end of the stream.
                int bytesToRead = startingByte + 64 > br.BaseStream.Length ? (int)br.BaseStream.Length - startingByte : 64;


                //move your stream to the given possition
                br.BaseStream.Seek(startingByte, SeekOrigin.Begin);

                //populates databuffer with the given bytes
                byte[] dataBuffer = br.ReadBytes(bytesToRead);

                numbers.AddRange(dataBuffer);

                //recursive call to the same 
                if (startingByte + bytesToRead < fs.Length)
                    GetValue(startingByte + bytesToRead, numbers);
            }
        }

Upvotes: 0

TiGreX
TiGreX

Reputation: 1816

It seems like what you are trying to do is use a recursive call on Value() this is based on your comment, but it is not clear, so I am going to do that assumption.

there is a problem I see and it's like in your scenario you're returning a byte[], So I modified your code a little bit to make it as closest as your's.

/// <summary>
/// This method will call your `value` methodand return the bytes and it is the entry point for the loci.
/// </summary>
/// <returns></returns>
public static byte[] ByteValueCaller()
{
    byte[] numbers = new byte[9999];

    Value(0, numbers);

    return numbers;
}

public static void Value(int startingByte, byte[] numbers)
{

    using (FileStream fs = File.Open(@"C:\Users\file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (BinaryReader br = new BinaryReader(fs))
    {
        //64bytes == 512bit
        //determines if the last position to use is inside your stream, or if the last position is the end of the stream.
        int bytesToRead = startingByte + 64 > br.BaseStream.Length ? (int)br.BaseStream.Length - startingByte : 64;


        //move your stream to the given possition
        br.BaseStream.Seek(startingByte, SeekOrigin.Begin);

        //populates databuffer with the given bytes
        byte[] dataBuffer = br.ReadBytes(bytesToRead);

        //This method will migrate from our temporal databuffer to the numbers array.
        TransformBufferArrayToNumbers(startingByte, dataBuffer, numbers);

        //recursive call to the same 
        if (startingByte + bytesToRead < fs.Length)
            Value(startingByte + bytesToRead, numbers);

    }


    static void TransformBufferArrayToNumbers(int startingByte, byte[] dataBuffer, byte[] numbers)
    {
        for (var i = 0; i < dataBuffer.Length; i++)
        {
            numbers[startingByte + i] = dataBuffer[i];
        }
    }
}

Also, be careful with the byte[9999] as you are limiting the characters you can get, if that's a hardcoded limit, I will add also that information on the if that determines the recursive call.

Upvotes: 0

Martin Verjans
Martin Verjans

Reputation: 4806

What can be done is to pass Value() an offset and a length parameter.

But there is a problem with your method, you are actually taking the first bytes for each line in the file, which I don't know is what you want to do. So I corrected this to make sure you return only length bytes.

using System.Linq Skip and Take methods, you may find things easier as well

public static byte[] Value(int startOffset, int length)
{
    byte allBytes = File.ReadAllBytes(@"C:\Users\file.txt");
    return allBytes.Skip(startOffset).Take(length);
}

Upvotes: 1

Related Questions