Skip to content

Commit

Permalink
Merge branch 'experiment/jetty-12.1.x/HttpContent-writeTo' into exper…
Browse files Browse the repository at this point in the history
…iment/jetty-12.1/HttpContent-no-release
  • Loading branch information
lorban committed Jul 24, 2024
2 parents 9afd8bc + a76955b commit 84ab360
Show file tree
Hide file tree
Showing 24 changed files with 1,182 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,52 @@ public class InputStreamRequestContent extends InputStreamContentSource implemen
{
private final String contentType;

/**
* @deprecated use {@link #InputStreamRequestContent(String, InputStream, ByteBufferPool.Sized)} instead.
*/
@Deprecated
public InputStreamRequestContent(InputStream stream)
{
this(stream, 4096);
}

/**
* @deprecated use {@link #InputStreamRequestContent(String, InputStream, ByteBufferPool.Sized)} instead.
*/
@Deprecated
public InputStreamRequestContent(InputStream stream, int bufferSize)
{
this("application/octet-stream", stream, bufferSize);
}

/**
* @deprecated use {@link #InputStreamRequestContent(String, InputStream, ByteBufferPool.Sized)} instead.
*/
@Deprecated
public InputStreamRequestContent(String contentType, InputStream stream, int bufferSize)
{
this(contentType, stream);
setBufferSize(bufferSize);
this(contentType, stream, new ByteBufferPool.Sized(null, false, bufferSize));
}

/**
* @deprecated use {@link #InputStreamRequestContent(String, InputStream, ByteBufferPool.Sized)} instead.
*/
@Deprecated
public InputStreamRequestContent(String contentType, InputStream stream)
{
this(contentType, stream, null);
}

/**
* @deprecated use {@link #InputStreamRequestContent(String, InputStream, ByteBufferPool.Sized)} instead.
*/
@Deprecated
public InputStreamRequestContent(String contentType, InputStream stream, ByteBufferPool bufferPool)
{
this(contentType, stream, new ByteBufferPool.Sized(bufferPool));
}

public InputStreamRequestContent(String contentType, InputStream stream, ByteBufferPool.Sized bufferPool)
{
super(stream, bufferPool);
this.contentType = contentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,35 +267,7 @@ static Content.Source from(ByteBufferPool.Sized byteBufferPool, InputStream inpu
*/
static Content.Source from(ByteBufferPool.Sized byteBufferPool, InputStream inputStream, long offset, long length)
{
if (offset > 0 && length != 0)
{
try
{
inputStream.skip(offset - 1);
if (inputStream.read() == -1)
throw new IllegalArgumentException("Offset out of range");
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
return new InputStreamContentSource(inputStream, byteBufferPool)
{
private long toRead = length;

@Override
protected int fillBufferFromInputStream(InputStream inputStream, byte[] buffer) throws IOException
{
if (toRead == 0)
return -1;
int toReadInt = (int)Math.min(Integer.MAX_VALUE, toRead);
int len = toReadInt > -1 ? Math.min(toReadInt, buffer.length) : buffer.length;
int read = inputStream.read(buffer, 0, len);
toRead -= read;
return read;
}
};
return new InputStreamContentSource(inputStream, byteBufferPool, offset, length);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.io.RetainableByteBuffer;
import org.eclipse.jetty.io.RuntimeIOException;
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.thread.AutoLock;
Expand All @@ -40,64 +41,55 @@ public class InputStreamContentSource implements Content.Source
private final AutoLock lock = new AutoLock();
private final SerializedInvoker invoker = new SerializedInvoker();
private final InputStream inputStream;
private ByteBufferPool.Sized bufferPool;
private final ByteBufferPool.Sized bufferPool;
private Runnable demandCallback;
private Content.Chunk errorChunk;
private long toRead;
private boolean closed;

/**
* @deprecated Use {@link #InputStreamContentSource(InputStream, ByteBufferPool.Sized)} instead.
*/
@Deprecated
public InputStreamContentSource(InputStream inputStream)
{
this(inputStream, null);
}

public InputStreamContentSource(InputStream inputStream, ByteBufferPool bufferPool)
public InputStreamContentSource(InputStream inputStream, ByteBufferPool.Sized bufferPool)
{
this(inputStream, bufferPool instanceof ByteBufferPool.Sized sized ? sized : new ByteBufferPool.Sized(bufferPool));
this(inputStream, bufferPool, 0L, -1L);
}

public InputStreamContentSource(InputStream inputStream, ByteBufferPool.Sized bufferPool)
public InputStreamContentSource(InputStream inputStream, ByteBufferPool.Sized bufferPool, long offset, long length)
{
this.inputStream = Objects.requireNonNull(inputStream);
bufferPool = Objects.requireNonNullElse(bufferPool, ByteBufferPool.SIZED_NON_POOLING);
// Make sure direct is always false as the implementation requires heap buffers to be able to call array().
if (bufferPool.isDirect())
bufferPool = new ByteBufferPool.Sized(bufferPool.getWrapped(), false, bufferPool.getSize());
this.bufferPool = bufferPool;
skipToOffset(inputStream, offset, length);
this.toRead = length;
}

public int getBufferSize()
private static void skipToOffset(InputStream inputStream, long offset, long length)
{
return bufferPool.getSize();
}

/**
* @param bufferSize The size of the buffer
* @deprecated Use {@link InputStreamContentSource#InputStreamContentSource(InputStream, ByteBufferPool.Sized)}
*/
@Deprecated(forRemoval = true)
public void setBufferSize(int bufferSize)
{
try (AutoLock ignored = lock.lock())
if (offset > 0L && length != 0L)
{
if (bufferSize != bufferPool.getSize())
bufferPool = new ByteBufferPool.Sized(bufferPool.getWrapped(), bufferPool.isDirect(), bufferSize);
try
{
inputStream.skip(offset - 1);
if (inputStream.read() == -1)
throw new IllegalArgumentException("Offset out of range");
}
catch (IOException e)
{
throw new RuntimeIOException(e);
}
}
}

public boolean isUseDirectByteBuffers()
{
return bufferPool.isDirect();
}

/**
* @param useDirectByteBuffers this parameter is ignored as this class cannot work with direct byte buffers
* @deprecated Use {@link InputStreamContentSource#InputStreamContentSource(InputStream, ByteBufferPool.Sized)}
*/
@Deprecated(forRemoval = true, since = "12.0.11")
public void setUseDirectByteBuffers(boolean useDirectByteBuffers)
{
}

@Override
public Content.Chunk read()
{
Expand Down Expand Up @@ -135,7 +127,13 @@ public Content.Chunk read()

protected int fillBufferFromInputStream(InputStream inputStream, byte[] buffer) throws IOException
{
return inputStream.read(buffer, 0, buffer.length);
if (toRead == 0L)
return -1;
int toReadInt = toRead >= Integer.MAX_VALUE || toRead < 0L ? -1 : (int)toRead;
int len = toReadInt > -1 ? Math.min(toReadInt, buffer.length) : buffer.length;
int read = inputStream.read(buffer, 0, len);
toRead -= read;
return read;
}

private void close()
Expand Down
Loading

0 comments on commit 84ab360

Please sign in to comment.