1 /*
2 * Copyright 2016 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 * https://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.string;
17
18 import io.netty.buffer.ByteBufUtil;
19 import io.netty.util.CharsetUtil;
20 import io.netty.util.internal.ObjectUtil;
21 import io.netty.util.internal.StringUtil;
22
23 /**
24 * A class to represent line separators in different environments.
25 */
26 public final class LineSeparator {
27
28 /**
29 * The default line separator in the current system.
30 */
31 public static final LineSeparator DEFAULT = new LineSeparator(StringUtil.NEWLINE);
32
33 /**
34 * The Unix line separator(LF)
35 */
36 public static final LineSeparator UNIX = new LineSeparator("\n");
37
38 /**
39 * The Windows line separator(CRLF)
40 */
41 public static final LineSeparator WINDOWS = new LineSeparator("\r\n");
42
43 private final String value;
44
45 /**
46 * Create {@link LineSeparator} with the specified {@code lineSeparator} string.
47 */
48 public LineSeparator(String lineSeparator) {
49 this.value = ObjectUtil.checkNotNull(lineSeparator, "lineSeparator");
50 }
51
52 /**
53 * Return the string value of this line separator.
54 */
55 public String value() {
56 return value;
57 }
58
59 @Override
60 public boolean equals(Object o) {
61 if (this == o) {
62 return true;
63 }
64 if (!(o instanceof LineSeparator)) {
65 return false;
66 }
67 LineSeparator that = (LineSeparator) o;
68 return value != null ? value.equals(that.value) : that.value == null;
69 }
70
71 @Override
72 public int hashCode() {
73 return value != null ? value.hashCode() : 0;
74 }
75
76 /**
77 * Return a <a href="https://en.wikipedia.org/wiki/Hex_dump">hex dump</a> of the line separator in UTF-8 encoding.
78 */
79 @Override
80 public String toString() {
81 return ByteBufUtil.hexDump(value.getBytes(CharsetUtil.UTF_8));
82 }
83 }