]> WPIA git - gigi.git/blobdiff - lib/jetty/org/eclipse/jetty/util/IO.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / util / IO.java
index 51fbed94db4978d02d0187f2d62036d55f7dfc59..6e7b4824be22601a12e1cfbfb36f808681597e17 100644 (file)
@@ -1,6 +1,6 @@
 //
 //  ========================================================================
-//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
+//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
 //  ------------------------------------------------------------------------
 //  All rights reserved. This program and the accompanying materials
 //  are made available under the terms of the Eclipse Public License v1.0
@@ -29,6 +29,9 @@ import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.GatheringByteChannel;
 import java.nio.charset.Charset;
 
 import org.eclipse.jetty.util.log.Log;
@@ -366,7 +369,8 @@ public class IO
         {
             if (writer != null)
                 writer.close();
-        } catch (IOException e)
+        } 
+        catch (IOException e)
         {
             LOG.ignore(e);
         }
@@ -380,6 +384,52 @@ public class IO
         copy(in,bout);
         return bout.toByteArray();
     }
+
+    /* ------------------------------------------------------------ */
+    /**
+     * A gathering write utility wrapper.
+     * <p>This method wraps a gather write with a loop that handles the limitations of some operating systems that
+     * have a limit on the number of buffers written.  The method loops on the write until either all the content
+     * is written or no progress is made.
+     * @param out The GatheringgByteChannel to write to
+     * @param buffers The buffers to write
+     * @param offset The offset into the buffers array
+     * @param length The length in buffers to write
+     * @return The total bytes written
+     * @throws IOException
+     */
+    public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) throws IOException
+    {
+        long total=0;
+        write: while (length>0)
+        {
+            // Write as much as we can
+            long wrote=out.write(buffers,offset,length);
+            
+            // If we can't write any more, give up
+            if (wrote==0)
+                break;
+            
+            // count the total
+            total+=wrote;
+            
+            // Look for unwritten content
+            for (int i=offset;i<buffers.length;i++)
+            {
+                if (buffers[i].hasRemaining())
+                {
+                    // loop with new offset and length;
+                    length=length-(i-offset);
+                    offset=i;
+                    continue write;
+                }
+            }
+            length=0;
+        }
+        
+        return total;
+    }
+    
     
     /* ------------------------------------------------------------ */
     /**
@@ -487,6 +537,7 @@ public class IO
     }
     private static NullWrite __nullWriter = new NullWrite();
     private static PrintWriter __nullPrintWriter = new PrintWriter(__nullWriter);
+
 }