View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the 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 io.netty.handler.codec.http;
17  
18  
19  import io.netty.util.internal.InternalThreadLocalMap;
20  
21  final class CookieEncoderUtil {
22  
23      static StringBuilder stringBuilder() {
24          return InternalThreadLocalMap.get().stringBuilder();
25      }
26  
27      /**
28       * @param buf a buffer where some cookies were maybe encoded
29       * @return the buffer String without the trailing separator, or null if no cookie was appended.
30       */
31      static String stripTrailingSeparatorOrNull(StringBuilder buf) {
32          return buf.length() == 0 ? null : stripTrailingSeparator(buf);
33      }
34  
35      static String stripTrailingSeparator(StringBuilder buf) {
36          if (buf.length() > 0) {
37              buf.setLength(buf.length() - 2);
38          }
39          return buf.toString();
40      }
41  
42      static void addUnquoted(StringBuilder sb, String name, String val) {
43          sb.append(name);
44          sb.append((char) HttpConstants.EQUALS);
45          sb.append(val);
46          sb.append((char) HttpConstants.SEMICOLON);
47          sb.append((char) HttpConstants.SP);
48      }
49  
50      static void add(StringBuilder sb, String name, long val) {
51          sb.append(name);
52          sb.append((char) HttpConstants.EQUALS);
53          sb.append(val);
54          sb.append((char) HttpConstants.SEMICOLON);
55          sb.append((char) HttpConstants.SP);
56      }
57  
58      private CookieEncoderUtil() {
59          // Unused
60      }
61  }