none
CSharp Equivalent of java.io.ByteArrayOutputStream .reset() using visual studio 2008 SP1 RRS feed

  • Question

  • Dear All,

    I am looking for CSharp Equivalent of   java.io.ByteArrayOutputStream .reset()  using Visual Studio 2008 SP1.

    So far, I used stream.Close(), stream.Dispose(),

    byte[] buffer = bos.GetBuffer();

    Array.Clear(buffer, 0, buffer.Length);

    didn't work for me since I need to care about after rest there is stream.Write();

    if not see error message "Can't access a closed stream"

    I hope someone will advise me to get it done.

    Thanks and best regards


                   

    //


                   

    //bos.Position = 0;


                   

    //bos.SetLength(0);

    Friday, May 10, 2013 8:16 AM

Answers

  • Hi,

    It is not and here is my solution

    int len = 0;
    MemoryStream stream = new MemoryStream(len);

    Best regards

     
    • Marked as answer by Mr Ko Ko Friday, May 10, 2013 12:33 PM
    Friday, May 10, 2013 12:33 PM

All replies

  • As far as I can tell from Java documentation the answer is already in your post: use SetLength(0).
    Friday, May 10, 2013 8:26 AM
    Moderator
  • Hi,

    It is not and here is my solution

    int len = 0;
    MemoryStream stream = new MemoryStream(len);

    Best regards

     
    • Marked as answer by Mr Ko Ko Friday, May 10, 2013 12:33 PM
    Friday, May 10, 2013 12:33 PM
  • What's wrong with SetLength(0)? Care to share the difference so others will know too?

    Friday, May 10, 2013 12:36 PM
    Moderator
  • Hi,

    It is not set to size of stream or memorystream to zero.

    Then I couldn't manage to clear the stream for the next container.

    If size of stream is still keep maintaining then I will have the extra info for the next process.

    Best regards

    PS : Actually, I want to do sizeof(Arr) = 0; where Arr is any array may be byte array in C++ using safe but couldn't manage to find the right one for the time being

    The  reason I am using  java.io.ByteArrayOutputStream .reset()   is  to clear the stream in preparation for the next process

    Saturday, May 11, 2013 4:23 AM
  • "If size of stream is still keep maintaining then I will have the extra info for the next process."

    MemoryStream has 2 "size" properties, Length and Capacity. Length gives you the size of the actual data stored in the stream. Capacity gives you the size of the array that the MemoryStream uses to store the data.

    Calling SetLength(0) will set the Length to 0 but keep the Capacity unchanged. This is enough to avoid "the extra info". You can also set the Capacity to 0 after the SetLength(0) call but normaly that's not needed.

    Are you using GetBuffer instead of ToArray to get the data from the stream?

    "Actually, I want to do sizeof(Arr) = 0; where Arr is any array may be byte array in C++ using safe but couldn't manage to find the right one for the time being"

    I'm not sure what you mean by that, arrays are not resizable in either C# or C++. If you're talking about a block of unmanaged memory allocated via something like Marshal.AllocHGlobal then you could use Marshal.ReAllocHGlobal. Not that such a "realloc" may or may not resize the block. If you want to increase the size it's quite possible that you'll get a new block.

    Saturday, May 11, 2013 5:02 AM
    Moderator
  • Hi Mike,

    Thank you lots of explanation and I tried to settle for reset of java in C# as follow

    MemoryStream mem;
    mem.SetLength(0);
    mem = new MemoryStream();

    Seems like ok but I had big issue in DeflateStream not getting same data length in Java as shown

    // in Java
    byte data[] = // Here is 2775 for two images same data 
    reset();  // Here is 2775 for two images same data values in C#
    
    
    int icomplen = 2775;
    len =  icomplen ;
     
    if (len > 1000) {
    
               
    
     Deflater z=new Deflater();
    
     byte data1[] = new byte[icomplen + 50];
     byte data2[] = new byte[10];
    
    z.setInput(data);      // Here is 2775 for two images same data values in C#
    
    z.finish();                           // Here is 2775 for two images same data values in C#
    
    icomplen = z.deflate(data1);   // Here is 2745 for two images after deflate decompression in Java but C# show 4320 where overlapped copies of data
    
    // z.deflate(abRest) == 0    // here is zero
    
     if ((z.deflate(data2) == 0) && (icomplen < len)) {
    
        write(data1, 0, icomplen);
    
         }
    }
    

    May be this question not related to this thread but I hope and I really appreciate you advise to close the case since dead line is over.

    // C#
    
    
    byte[] abReqData;
     
    reset();    
    // using MemoryStream bo = new MemoryStream(); 
    // bo.Setlength(0);
    
    int icomplen = 2775;
    
    int len = icomplen ;
    
    if (len> 1000)
    
    {
    
        byte[] data1= new byte[len+50];
    
        byte[] data2= new byte[10];
    
        MemoryStream compMS = new MemoryStream();
        // I tired to use 
        // MemoryStream compMS = new MemoryStream(data1);
        // Error -> Memory is not expandable but ok in other class
    
        DeflateStream compressedzipStream = new DeflateStream(compMS, CompressionMode.Compress, true);
    
        compressedzipStream.Write(data, 0, data.Length);
    
        reset();
    
        //byte[] compArr = new byte[tmpArr.Length];
       
         byte arr = compMS.ToArray();   // Here I got 4320
         // let say data = 1,2,3,..., 10 
         // arr = 1,2,3,..., 10,1,2,3,..., 10 seems like overlapping and no compression since which is fine in some test programs
    
        icomplen = arr.Length;
        // Above memory stream had extra info then here wrong
    
    
        //compressedzipStream.Close();  
        // I put these close but same issue
    
        //compMS.Close();
    
        int len = 0;
        // is it possible to compressed empty data array in C#?
     
    
        if ((len == 0) && (icomplen < 2775))
    
        {
    
            write(data, 0, icomplen);  // Memory Write
    
    
         }
    }
    

    So far, the compression should have less data count after any compression method but sometimes less data but sometime more data size.

    is it coz of byte value or something else or is there any trick to do for data compression in C#?

    since Java Deflate decompression is straight away to use its API but in C# need to read first two bytes first then can read whole of data since I tried to solve in this program.

    I am looking forward to hearing from you good news.

    Thanks and best regards

     
    Monday, May 13, 2013 2:35 AM
  • "So far, the compression should have less data count after any compression method but sometimes less data but sometime more data size."

    Hmm, I'm not sure if that has anything to do with MemoryStream. I've heard (haven't used it myself) that .NET's DeflateStream sometimes does that. If possible try repeating the test with .NET 4.5, it should behave better.

    Also, it appears that you do not close the deflate stream, you have a Close but it's commented out and it's after ToArray. If you do ToArray without closing the deflate stream it's possible that some of the compressed data will be missing.

    In general, if you want to compress a byte array then the code should look like this:

    private static byte[] Compress(byte[] data)
    {
        using (MemoryStream stream = new MemoryStream()) {
            using (DeflateStream deflate = new DeflateStream(stream, CompressionMode.Compress, true))
                deflate.Write(data, 0, data.Length);
    
            return stream.ToArray();
        }
    }
    

    I'm still not sure what's the story around reset(). It can be useful as a performance optimization in cases where that data you want to compress in memory is large (say, 1 megabyte), reset will allow you to reuse the MemoryStream and avoid repeated large array allocations. Other than this reset is of limited use, it only makes sense to use it if for some reason you cannot recreate the stream.

    "// I tired to use
    // MemoryStream compMS = new MemoryStream(data1);
    // Error -> Memory is not expandable but ok in other class"

    That's normal, initializing from an existing array is normally used only for reading, not for adding new data. After all you cannot resize an array.

    Monday, May 13, 2013 5:29 AM
    Moderator
  • Hi Mike,

    Thank you for your prompt reply and for me I need to tie framework 3.5 coz of dependency constraint.

    I used the close for deflatestream and stream but still extra info as above.

    Yes agree for normal way of compression but typical way of decompression need to add below for real world if not u can't get what u want.

     MemoryStream deCompMS = new MemoryStream(tmpArr);
    
    deCompMS.ReadByte();   // These are necessary n tricks
    deCompMS.ReadByte();
    
    DeflateStream deCompressedzipStream = new DeflateStream(deCompMS, CompressionMode.Decompress, true);

    reset is to clear the stream before next serialization and pls help me for this deflatestream compression.

    Best regards

    Monday, May 13, 2013 6:12 AM
  • I'm not sure what's up with those "tricks". Are you saying that you have data compressed with the Java code and that data cannot be decompressed by .NET without skipping those 2 bytes?

    I'm not familiar with Java's deflate but those 2 bytes might be the header mentioned here: http://blogs.msdn.com/b/bclteam/archive/2007/05/16/system-io-compression-capabilities-kim-hamilton.aspx See the "Working with zlib" paragraph.

    Monday, May 13, 2013 6:19 AM
    Moderator
  • Hi Mike,

    What I mean is I want to use compression in .NET which is same idea as in Java.

    Yes true, 2 bytes header and how about successful deflate or gzip compression for .NET.

    I really appreciate ur help since time is running out.

    Best regards

    Monday, May 13, 2013 7:59 AM
  • "how about successful deflate or gzip compression for .NET."

    Successful in what way? That the compressed data size isn't larger than the uncompressed data?

    In that case you'll have to try to use a different compression library, perhaps SharpZipLib will do better: http://www.icsharpcode.net/OpenSource/SharpZipLib/

    Monday, May 13, 2013 8:25 AM
    Moderator