Merge branch 'master' into javawebsocket-jsr

This commit is contained in:
Joakim Erdfelt 2013-06-05 20:10:14 -07:00
commit a23798321f
4 changed files with 55 additions and 24 deletions

View File

@ -59,12 +59,14 @@ import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@Ignore
public class ReferrerPushStrategyTest extends AbstractHTTPSPDYTest public class ReferrerPushStrategyTest extends AbstractHTTPSPDYTest
{ {
private final int referrerPushPeriod = 1000; private final int referrerPushPeriod = 1000;

View File

@ -39,11 +39,13 @@ import org.eclipse.jetty.util.Fields;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@Ignore
public class WriteThroughAggregateBufferTest extends AbstractHTTPSPDYTest public class WriteThroughAggregateBufferTest extends AbstractHTTPSPDYTest
{ {
private static final Logger LOG = Log.getLogger(WriteThroughAggregateBufferTest.class); private static final Logger LOG = Log.getLogger(WriteThroughAggregateBufferTest.class);

View File

@ -34,29 +34,53 @@ import java.nio.charset.Charset;
/* ------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------- */
/** /**
* Buffer utility methods. * Buffer utility methods.
* * <p>The standard JVM {@link ByteBuffer} can exist in two modes: In fill mode the valid
* These utility methods facilitate the usage of NIO {@link ByteBuffer}'s in a more flexible way. * data is between 0 and pos; In flush mode the valid data is between the pos and the limit.
* The standard {@link ByteBuffer#flip()} assumes that once flipped to flush a buffer, * The various ByteBuffer methods assume a mode and some of them will switch or enforce a mode:
* that it will be completely emptied before being cleared ready to be filled again. * Allocate and clear set fill mode; flip and compact switch modes; read and write assume fill
* The {@link #flipToFill(ByteBuffer)} and {@link #flipToFlush(ByteBuffer, int)} methods provided here * and flush modes. This duality can result in confusing code such as:
* do not assume that the buffer is empty and will preserve content when flipped. * <pre>
* buffer.clear();
* channel.write(buffer);
* </pre>
* Which looks as if it should write no data, but in fact writes the buffer worth of garbage.
* </p>
* <p> * <p>
* ByteBuffers can be considered in one of two modes: Flush mode where valid content is contained between * The BufferUtil class provides a set of utilities that operate on the convention that ByteBuffers
* position and limit which is consumed by advancing the position; and Fill mode where empty space is between * will always be left, passed in an API or returned from a method in the flush mode - ie with
* the position and limit, which is filled by advancing the position. In fill mode, there may be valid data * valid data between the pos and limit. This convention is adopted so as to avoid confusion as to
* in the buffer before the position and the start of this data is given by the return value of {@link #flipToFill(ByteBuffer)} * what state a buffer is in and to avoid excessive copying of data that can result with the usage
* of compress.</p>
* <p> * <p>
* A typical pattern for using the buffers in this style is: * Thus this class provides alternate implementations of {@link #allocate(int)},
* <blockquote><pre> * {@link #allocateDirect(int)} and {@link #clear(ByteBuffer)} that leave the buffer
* ByteBuffer buf = BufferUtil.allocate(4096); * in flush mode. Thus the following tests will pass:<pre>
* while(in.isOpen()) * ByteBuffer buffer = BufferUtil.allocate(1024);
* assert(buffer.remaining()==0);
* BufferUtil.clear(buffer);
* assert(buffer.remaining()==0);
* </pre>
* </p>
* <p>If the BufferUtil methods {@link #fill(ByteBuffer, byte[], int, int)},
* {@link #append(ByteBuffer, byte[], int, int)} or {@link #put(ByteBuffer, ByteBuffer)} are used,
* then the caller does not need to explicitly switch the buffer to fill mode.
* If the caller wishes to use other ByteBuffer bases libraries to fill a buffer,
* then they can use explicit calls of #flipToFill(ByteBuffer) and #flipToFlush(ByteBuffer, int)
* to change modes. Note because this convention attempts to avoid the copies of compact, the position
* is not set to zero on each fill cycle and so its value must be remembered:
* <pre>
* int pos = BufferUtil.flipToFill(buffer);
* try
* { * {
* int pos=BufferUtil.flipToFill(buf); * buffer.put(data);
* if (in.read(buf)<0) * }
* break; * finally
* BufferUtil.flipToFlush(buf,pos); * {
* out.write(buf); * flipToFlush(buffer, pos);
* }</pre></blockquote> * }
* </pre>
* The flipToFill method will effectively clear the buffer if it is emtpy and will compact the buffer if there is no space.
*
*/ */
public class BufferUtil public class BufferUtil
{ {
@ -85,7 +109,7 @@ public class BufferUtil
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** Allocate ByteBuffer in flush mode. /** Allocate ByteBuffer in flush mode.
* The position and limit will both be zero, indicating that the buffer is * The position and limit will both be zero, indicating that the buffer is
* empty and must be flipped before any data is put to it. * empty and in flush mode.
* @param capacity * @param capacity
* @return Buffer * @return Buffer
*/ */
@ -324,7 +348,8 @@ public class BufferUtil
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /** Append bytes to a buffer.
*
*/ */
public static void append(ByteBuffer to, byte[] b, int off, int len) throws BufferOverflowException public static void append(ByteBuffer to, byte[] b, int off, int len) throws BufferOverflowException
{ {
@ -340,7 +365,7 @@ public class BufferUtil
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /** Appends a byte to a buffer
*/ */
public static void append(ByteBuffer to, byte b) public static void append(ByteBuffer to, byte b)
{ {

View File

@ -28,6 +28,7 @@ import java.util.Arrays;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -231,6 +232,7 @@ public class BufferUtilTest
} }
private static final Logger LOG = Log.getLogger(BufferUtilTest.class); private static final Logger LOG = Log.getLogger(BufferUtilTest.class);
@Ignore
@Test @Test
public void testWriteTo() throws IOException public void testWriteTo() throws IOException
{ {