Skip to content

Commit

Permalink
Do not throw an exception when failed to get a header
Browse files Browse the repository at this point in the history
Motivation:

It is often very expensive to instantiate an exception. TextHeader
should not raise an exception when it failed to find a header or when
its header value is not valid.

Modification:

- Change the return type of the getter methods to Integer and Long so
  that null is returned when no header is found or its value is invalid
- Update Javadoc

Result:

- Fixes netty#2758
- No unnecessary instantiation of exceptions
  • Loading branch information
trustin committed Aug 12, 2014
1 parent de72406 commit 6076420
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 101 deletions.
88 changes: 56 additions & 32 deletions codec/src/main/java/io/netty/handler/codec/DefaultTextHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,20 @@ public String get(CharSequence name, String defaultValue) {
}

@Override
public int getInt(CharSequence name) {
public Integer getInt(CharSequence name) {
CharSequence v = getUnconverted(name);
if (v == null) {
throw new NoSuchElementException(String.valueOf(name));
return null;
}

if (v instanceof AsciiString) {
return ((AsciiString) v).parseInt();
} else {
return Integer.parseInt(v.toString());
try {
if (v instanceof AsciiString) {
return ((AsciiString) v).parseInt();
} else {
return Integer.parseInt(v.toString());
}
} catch (NumberFormatException ignored) {
return null;
}
}

Expand All @@ -409,16 +413,20 @@ public int getInt(CharSequence name, int defaultValue) {
}

@Override
public long getLong(CharSequence name) {
public Long getLong(CharSequence name) {
CharSequence v = getUnconverted(name);
if (v == null) {
throw new NoSuchElementException(String.valueOf(name));
return null;
}

if (v instanceof AsciiString) {
return ((AsciiString) v).parseLong();
} else {
return Long.parseLong(v.toString());
try {
if (v instanceof AsciiString) {
return ((AsciiString) v).parseLong();
} else {
return Long.parseLong(v.toString());
}
} catch (NumberFormatException ignored) {
return null;
}
}

Expand All @@ -441,13 +449,17 @@ public long getLong(CharSequence name, long defaultValue) {
}

@Override
public long getTimeMillis(CharSequence name) {
public Long getTimeMillis(CharSequence name) {
CharSequence v = getUnconverted(name);
if (v == null) {
throw new NoSuchElementException(String.valueOf(name));
return null;
}

return HttpHeaderDateFormat.get().parse(v.toString());
try {
return HttpHeaderDateFormat.get().parse(v.toString());
} catch (ParseException ignored) {
return null;
}
}

@Override
Expand Down Expand Up @@ -529,16 +541,20 @@ public String getAndRemove(CharSequence name, String defaultValue) {
}

@Override
public int getIntAndRemove(CharSequence name) {
public Integer getIntAndRemove(CharSequence name) {
CharSequence v = getUnconvertedAndRemove(name);
if (v == null) {
throw new NoSuchElementException(String.valueOf(name));
return null;
}

if (v instanceof AsciiString) {
return ((AsciiString) v).parseInt();
} else {
return Integer.parseInt(v.toString());
try {
if (v instanceof AsciiString) {
return ((AsciiString) v).parseInt();
} else {
return Integer.parseInt(v.toString());
}
} catch (NumberFormatException ignored) {
return null;
}
}

Expand All @@ -561,16 +577,20 @@ public int getIntAndRemove(CharSequence name, int defaultValue) {
}

@Override
public long getLongAndRemove(CharSequence name) {
public Long getLongAndRemove(CharSequence name) {
CharSequence v = getUnconvertedAndRemove(name);
if (v == null) {
throw new NoSuchElementException(String.valueOf(name));
return null;
}

if (v instanceof AsciiString) {
return ((AsciiString) v).parseLong();
} else {
return Long.parseLong(v.toString());
try {
if (v instanceof AsciiString) {
return ((AsciiString) v).parseLong();
} else {
return Long.parseLong(v.toString());
}
} catch (NumberFormatException ignored) {
return null;
}
}

Expand All @@ -593,13 +613,17 @@ public long getLongAndRemove(CharSequence name, long defaultValue) {
}

@Override
public long getTimeMillisAndRemove(CharSequence name) {
public Long getTimeMillisAndRemove(CharSequence name) {
CharSequence v = getUnconvertedAndRemove(name);
if (v == null) {
throw new NoSuchElementException(String.valueOf(name));
return null;
}

return HttpHeaderDateFormat.get().parse(v.toString());
try {
return HttpHeaderDateFormat.get().parse(v.toString());
} catch (ParseException ignored) {
return null;
}
}

@Override
Expand Down Expand Up @@ -1074,7 +1098,7 @@ private HttpHeaderDateFormat() {
dateFormat3.setTimeZone(tz);
}

long parse(String text) {
long parse(String text) throws ParseException {
Date date = dateFormat1.parse(text, parsePos);
if (date == null) {
date = dateFormat2.parse(text, parsePos);
Expand All @@ -1083,7 +1107,7 @@ long parse(String text) {
date = dateFormat3.parse(text, parsePos);
}
if (date == null) {
PlatformDependent.throwException(new ParseException(text, 0));
throw new ParseException(text, 0);
}
return date.getTime();
}
Expand Down
25 changes: 12 additions & 13 deletions codec/src/main/java/io/netty/handler/codec/EmptyTextHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;

public class EmptyTextHeaders implements TextHeaders {
Expand All @@ -38,8 +37,8 @@ public String get(CharSequence name, String defaultValue) {
}

@Override
public int getInt(CharSequence name) {
throw new NoSuchElementException(String.valueOf(name));
public Integer getInt(CharSequence name) {
return null;
}

@Override
Expand All @@ -48,8 +47,8 @@ public int getInt(CharSequence name, int defaultValue) {
}

@Override
public long getLong(CharSequence name) {
throw new NoSuchElementException(String.valueOf(name));
public Long getLong(CharSequence name) {
return null;
}

@Override
Expand All @@ -58,8 +57,8 @@ public long getLong(CharSequence name, long defaultValue) {
}

@Override
public long getTimeMillis(CharSequence name) {
throw new NoSuchElementException(String.valueOf(name));
public Long getTimeMillis(CharSequence name) {
return null;
}

@Override
Expand All @@ -78,8 +77,8 @@ public String getAndRemove(CharSequence name, String defaultValue) {
}

@Override
public int getIntAndRemove(CharSequence name) {
throw new NoSuchElementException(String.valueOf(name));
public Integer getIntAndRemove(CharSequence name) {
return null;
}

@Override
Expand All @@ -88,8 +87,8 @@ public int getIntAndRemove(CharSequence name, int defaultValue) {
}

@Override
public long getLongAndRemove(CharSequence name) {
throw new NoSuchElementException(String.valueOf(name));
public Long getLongAndRemove(CharSequence name) {
return null;
}

@Override
Expand All @@ -98,8 +97,8 @@ public long getLongAndRemove(CharSequence name, long defaultValue) {
}

@Override
public long getTimeMillisAndRemove(CharSequence name) {
throw new NoSuchElementException(String.valueOf(name));
public Long getTimeMillisAndRemove(CharSequence name) {
return null;
}

@Override
Expand Down
Loading

0 comments on commit 6076420

Please sign in to comment.