title: C# DeflateStream vs Java DeflaterOutputStream
body: <p>In Java, this works as expected:</p>
  
  <pre><code>  public static void testwrite(String filename) throws IOException {
      FileOutputStream fs = new FileOutputStream(new File(filename), false);
      DeflaterOutputStream fs2 = new DeflaterOutputStream(fs, new Deflater(3));
      for (int i = 0; i &lt; 50; i++)
        for (int j = 0; j &lt; 40; j++)
          fs2.write((byte) (i + 0x30));
      fs2.close();
    }
  
    public static void testread(String filename) throws IOException {
      FileInputStream fs = new FileInputStream(new File(filename));
      InflaterInputStream fs2 = new InflaterInputStream(fs);
      int c, n = 0;
      while ((c = fs2.read()) &gt;= 0) {
        System.out.print((char) c);
        if (n++ % 40 == 0)  System.out.println("");
      }
      fs2.close();
    }
  </code></pre>
  
  <p>The first method compresses 2000 chars  in a 106 bytes file, the second reads it ok.</p>
  
  <p>The equivalent in C# would seem to be</p>
  
  <pre><code>private static void testwritecs(String filename) {
      FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
      DeflateStream fs2 = new DeflateStream(fs,CompressionMode.Compress,false);
      for (int i = 0; i &lt; 50; i++)   {
          for(int j = 0; j &lt; 40; j++)
                  fs2.WriteByte((byte)(i+0x30));
      }
      fs2.Flush();
      fs2.Close();
  }
  </code></pre>
  
  <p>But it generates a file of 2636 bytes (larger than the raw data, even though it has low entropy) and is not readable with the Java testread() method above. Any ideas?</p>
  
  <p><em>Edited</em>: The implementation is indeed not standard/portable (this bit of the <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx" rel="nofollow">docs</a>: "an industry standard algorithm" seems a joke), and very crippled. Among other things, its behaviour changes radically if one writes the bytes one at a time or in blocks (which goes against the concept of a "stream"); if I change the above</p>
  
  <pre><code>for(int j = 0; j &lt; 40; j++)
     fs2.WriteByte((byte)(i+0x30));
  </code></pre>
  
  <p>by</p>
  
  <pre><code>byte[] buf = new byte{}[40;
  for(int j = 0; j &lt; 40; j++)
     buf[j]=(byte)(i+0x30));
  fs2.Write(buf,0,buf.Length);
  </code></pre>
  
  <p>the compression gets (slightly) reasonable. Shame.</p>