Skip to content

Commit

Permalink
Added method hasEntity to readable entity (#5602)
Browse files Browse the repository at this point in the history
* Added method hasEntity to readable entity, so we can check if there is any data available.
* Fix multipart entities
  • Loading branch information
tomas-langer committed Dec 6, 2022
1 parent 2b00b7e commit 7c9381e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ default <T> T as(Class<T> type) {
*/
<T> T as(GenericType<T> type);

/**
* Whether an entity actually exists.
*
* @return {@code true} if an entity exists and can be read
*/
boolean hasEntity();

/**
* Whether this entity has been consumed already.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,7 @@
* Base for readable entities.
*/
public abstract class ReadableEntityBase implements ReadableEntity {
private static final ReadableEntity EMPTY = new ReadableEntity() {
@Override
public InputStream inputStream() {
return new ByteArrayInputStream(BufferData.EMPTY_BYTES);
}

@Override
public <T> T as(Class<T> type) {
throw new IllegalStateException("No entity");
}

@Override
public <T> T as(GenericType<T> type) {
throw new IllegalStateException("No entity");
}

@Override
public boolean consumed() {
return true;
}

@Override
public ReadableEntity copy(Runnable entityProcessedRunnable) {
entityProcessedRunnable.run();
return this;
}
};
private static final ReadableEntity EMPTY = new EmptyReadableEntity();

private final AtomicBoolean consumed = new AtomicBoolean();
private final Function<InputStream, InputStream> decoder;
Expand Down Expand Up @@ -150,11 +124,7 @@ public void consume() {
}
}

/**
* Is there an entity.
*
* @return whether the entity has bytes
*/
@Override
public boolean hasEntity() {
return true;
}
Expand Down Expand Up @@ -274,4 +244,37 @@ private void ensureBuffer(int estimate) {
}
}
}

private static final class EmptyReadableEntity implements ReadableEntity {
@Override
public InputStream inputStream() {
return new ByteArrayInputStream(BufferData.EMPTY_BYTES);
}

@Override
public <T> T as(Class<T> type) {
throw new IllegalStateException("No entity");
}

@Override
public <T> T as(GenericType<T> type) {
throw new IllegalStateException("No entity");
}

@Override
public boolean consumed() {
return true;
}

@Override
public ReadableEntity copy(Runnable entityProcessedRunnable) {
entityProcessedRunnable.run();
return this;
}

@Override
public boolean hasEntity() {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public ReadableEntity copy(Runnable entityProcessedRunnable) {
throw new UnsupportedOperationException("Cannot copy a multi-part content.");
}

@Override
public boolean hasEntity() {
return true;
}

protected abstract void finish();

private void contentDisposition() {
Expand Down

0 comments on commit 7c9381e

Please sign in to comment.