Skip to content

Commit

Permalink
Merge pull request resteasy#482 from patriot1burke/master
Browse files Browse the repository at this point in the history
netty fix, finalize guard
  • Loading branch information
patriot1burke committed Apr 2, 2014
2 parents c8bb013 + e33a729 commit bc0ec04
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void close()
}

@Override
protected void finalize() throws Throwable
// This method is synchronized to protect against premature calling of finalize by the GC
protected synchronized void finalize() throws Throwable
{
if (isClosed()) return;
try {
Expand Down Expand Up @@ -144,7 +145,8 @@ protected InputStream getEntityStream()
protected abstract void releaseConnection() throws IOException;


public <T> T readEntity(Class<T> type, Type genericType, Annotation[] anns)
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
public synchronized <T> T readEntity(Class<T> type, Type genericType, Annotation[] anns)
{
abortIfClosed();
if (entity != null)
Expand Down Expand Up @@ -208,7 +210,8 @@ else if (bufferedEntity == null)
return (T) entity;
}

protected <T> Object readFrom(Class<T> type, Type genericType,
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
protected synchronized <T> Object readFrom(Class<T> type, Type genericType,
MediaType media, Annotation[] annotations)
{
Type useGeneric = genericType == null ? type : genericType;
Expand Down Expand Up @@ -312,11 +315,11 @@ public boolean bufferEntity()
}
return true;
}

@Override
public void abortIfClosed()
{
if (bufferedEntity == null) super.abortIfClosed();
}

@Override
public void abortIfClosed()
{
if (bufferedEntity == null) super.abortIfClosed();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ public MediaType getMediaType()
return mediaType == null ? MediaType.WILDCARD_TYPE : MediaType.valueOf(mediaType);
}

protected <T2> Object readFrom(Class<T2> type, Type genericType,
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
protected synchronized <T2> Object readFrom(Class<T2> type, Type genericType,
MediaType media, Annotation[] annotations)
{
Type useGeneric = genericType == null ? type : genericType;
Expand Down Expand Up @@ -584,7 +585,8 @@ public final void releaseConnection()
}

@Override
protected final void finalize() throws Throwable
// this is synchronized to protect against premature finalize called by the GC
protected synchronized final void finalize() throws Throwable
{
releaseConnection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public void write(byte[] b, int off, int len) throws IOException {

@Override
public void flush() throws IOException {
if (!buffer.isWritable()) return;
int readable = buffer.readableBytes();
if (readable == 0) return;
if (!response.isCommitted()) response.prepareChunkStream();
ctx.writeAndFlush(new DefaultHttpContent(buffer.copy()));
buffer.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public void empty() {
public String exception() {
throw new RuntimeException();
}

@GET
@Path("large")
@Produces("text/plain")
public String large() {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 1000; i++) {
buf.append(i);
}
return buf.toString();
}
}

static Client client;
Expand Down Expand Up @@ -94,6 +105,29 @@ public void testEmpty() throws Exception
}
}

@Test
public void testLarge() throws Exception
{
WebTarget target = client.target(generateURL("/large"));
Response response = target.request().get();
try
{
Assert.assertEquals(200, response.getStatus());
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 1000; i++) {
buf.append(i);
}
String expected = buf.toString();
String have = response.readEntity(String.class);
Assert.assertEquals(expected, have);

}
finally
{
response.close();
}
}

@Test
public void testUnhandledException() throws Exception
{
Expand Down

0 comments on commit bc0ec04

Please sign in to comment.