Skip to content

Commit

Permalink
More fixes for #1338: Add a new AtmosphereResource.forceBinaryWrite API
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Oct 30, 2013
1 parent 2f1dbfa commit 4798b6e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,14 @@ enum TRANSPORT {POLLING, LONG_POLLING, STREAMING, WEBSOCKET, JSONP, UNDEFINED, S
* {@link AtmosphereResource}}.
*/
void close() throws IOException;

/**
* Force binary write and never write String value.
*/
AtmosphereResource forceBinaryWrite(boolean force);

/**
* Return true when binary write is forced.
*/
boolean forceBinaryWrite();
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class AtmosphereResourceImpl implements AtmosphereResource {
protected HttpSession session;
private boolean disableSuspendEvent;
private TRANSPORT transport;
private boolean forceBinaryWrite;

/**
* Create an {@link AtmosphereResource}.
Expand Down Expand Up @@ -783,6 +784,17 @@ public void close() throws IOException {
notifyListeners();
}

@Override
public AtmosphereResource forceBinaryWrite(boolean forceBinaryWrite) {
this.forceBinaryWrite = forceBinaryWrite;
return this;
}

@Override
public boolean forceBinaryWrite() {
return forceBinaryWrite;
}

@Override
public HttpSession session() {
return session(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,14 @@ private boolean isUsingStream() {
usingStream.set((Boolean) s);
}
}

// Property always take first.
if (resource() != null) {
boolean force = resource().forceBinaryWrite();
if (!usingStream.get() && force) {
usingStream.set(force);
}
}
return usingStream.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public WebSocket write(AtmosphereResponse r, byte[] b, int offset, int length) t

logger.trace("WebSocket.write()");
boolean transform = filters.size() > 0 && r.getStatus() < 400;
if (binaryWrite) {
if (binaryWrite || r.resource().forceBinaryWrite()) {
if (transform) {
b = transform(b, offset, length);
}
Expand Down

0 comments on commit 4798b6e

Please sign in to comment.