| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package org.apache.commons.io; |
| 19 | |
| 20 | import java.nio.charset.Charset; |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | /** |
| 24 | * Enumerates standard line separators: {@link #CR}, {@link #CRLF}, {@link #LF}. |
| 25 | * |
| 26 | * @since 2.9.0 |
| 27 | */ |
| 28 | public enum StandardLineSeparator { |
| 29 | |
| 30 | /** |
| 31 | * Carriage return. This is the line ending used on macOS 9 and earlier. |
| 32 | */ |
| 33 | CR("\r"), |
| 34 | |
| 35 | /** |
| 36 | * Carriage return followed by line feed. This is the line ending used on Windows. |
| 37 | */ |
| 38 | CRLF("\r\n"), |
| 39 | |
| 40 | /** |
| 41 | * Line feed. This is the line ending used on Linux and macOS X and later. |
| 42 | */ |
| 43 | LF("\n"); |
| 44 | |
| 45 | private final String lineSeparator; |
| 46 | |
| 47 | /** |
| 48 | * Constructs a new instance for a non-null line separator. |
| 49 | * |
| 50 | * @param lineSeparator a non-null line separator. |
| 51 | */ |
| 52 | StandardLineSeparator(final String lineSeparator) { |
| 53 | this.lineSeparator = Objects.requireNonNull(lineSeparator, "lineSeparator"); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Gets the bytes for this instance encoded using the given Charset. |
| 58 | * |
| 59 | * @param charset the encoding Charset. |
| 60 | * @return the bytes for this instance encoded using the given Charset. |
| 61 | */ |
| 62 | public byte[] getBytes(final Charset charset) { |
| 63 | return lineSeparator.getBytes(charset); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Gets the String value of this instance. |
| 68 | * |
| 69 | * @return the String value of this instance. |
| 70 | */ |
| 71 | public String getString() { |
| 72 | return lineSeparator; |
| 73 | } |
| 74 | } |