001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io;
019
020import java.nio.charset.Charset;
021import java.util.Objects;
022
023/**
024 * Enumerates standard line separators: {@link #CR}, {@link #CRLF}, {@link #LF}.
025 *
026 * @since 2.9.0
027 */
028public enum StandardLineSeparator {
029
030    /**
031     * Carriage return. This is the line ending used on macOS 9 and earlier.
032     */
033    CR("\r"),
034
035    /**
036     * Carriage return followed by line feed. This is the line ending used on Windows.
037     */
038    CRLF("\r\n"),
039
040    /**
041     * Line feed. This is the line ending used on Linux and macOS X and later.
042     */
043    LF("\n");
044
045    private final String lineSeparator;
046
047    /**
048     * Constructs a new instance for a non-null line separator.
049     *
050     * @param lineSeparator a non-null line separator.
051     */
052    StandardLineSeparator(final String lineSeparator) {
053        this.lineSeparator = Objects.requireNonNull(lineSeparator, "lineSeparator");
054    }
055
056    /**
057     * Gets the bytes for this instance encoded using the given Charset.
058     *
059     * @param charset the encoding Charset.
060     * @return the bytes for this instance encoded using the given Charset.
061     */
062    public byte[] getBytes(final Charset charset) {
063        return lineSeparator.getBytes(charset);
064    }
065
066    /**
067     * Gets the String value of this instance.
068     *
069     * @return the String value of this instance.
070     */
071    public String getString() {
072        return lineSeparator;
073    }
074}