PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/jboss/netty/handler/codec/http/AbstractHttpData.java

https://github.com/maerqiang/netty
Java | 108 lines | 68 code | 15 blank | 25 comment | 11 complexity | ef3e8ba09d1ba5e545157178836413b0 MD5 | raw file
  1. /*
  2. * Copyright 2009 Red Hat, Inc.
  3. *
  4. * Red Hat licenses this file to you under the Apache License, version 2.0
  5. * (the "License"); you may not use this file except in compliance with the
  6. * License. You may obtain a copy of the License at:
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package org.jboss.netty.handler.codec.http;
  17. import java.nio.charset.Charset;
  18. /**
  19. * Abstract HttpData implementation
  20. *
  21. * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
  22. * @author Andy Taylor (andy.taylor@jboss.org)
  23. * @author <a href="http://gleamynode.net/">Trustin Lee</a>
  24. * @author <a href="http://openr66.free.fr/">Frederic Bregier</a>
  25. *
  26. */
  27. public abstract class AbstractHttpData implements HttpData {
  28. protected final String name;
  29. protected long definedSize = 0;
  30. protected long size = 0;
  31. protected Charset charset = HttpCodecUtil.DEFAULT_CHARSET;
  32. protected boolean completed = false;
  33. public AbstractHttpData(String name, Charset charset, long size)
  34. throws NullPointerException, IllegalArgumentException {
  35. if (name == null) {
  36. throw new NullPointerException("name");
  37. }
  38. name = name.trim();
  39. if (name.length() == 0) {
  40. throw new IllegalArgumentException("empty name");
  41. }
  42. for (int i = 0; i < name.length(); i ++) {
  43. char c = name.charAt(i);
  44. if (c > 127) {
  45. throw new IllegalArgumentException(
  46. "name contains non-ascii character: " + name);
  47. }
  48. // Check prohibited characters.
  49. switch (c) {
  50. case '=':
  51. case ',':
  52. case ';':
  53. case ' ':
  54. case '\t':
  55. case '\r':
  56. case '\n':
  57. case '\f':
  58. case 0x0b: // Vertical tab
  59. throw new IllegalArgumentException(
  60. "name contains one of the following prohibited characters: " +
  61. "=,; \\t\\r\\n\\v\\f: " + name);
  62. }
  63. }
  64. this.name = name;
  65. if (charset != null) {
  66. setCharset(charset);
  67. }
  68. definedSize = size;
  69. }
  70. @Override
  71. public String getName() {
  72. return name;
  73. }
  74. @Override
  75. public boolean isCompleted() {
  76. return completed;
  77. }
  78. @Override
  79. public Charset getCharset() {
  80. return charset;
  81. }
  82. @Override
  83. public void setCharset(Charset charset) {
  84. if (charset == null) {
  85. throw new NullPointerException("charset");
  86. }
  87. this.charset = charset;
  88. }
  89. @Override
  90. public long length() {
  91. return size;
  92. }
  93. }