Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method hasEntity to readable entity #5602

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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