001/* 002 * Copyright 2015-2024 the original author or authors 003 * 004 * This software is licensed under the Apache License, Version 2.0, 005 * the GNU Lesser General Public License version 2 or later ("LGPL") 006 * and the WTFPL. 007 * You may choose either license to govern your use of this software only 008 * upon the condition that you accept all of the terms of either 009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL. 010 */ 011package org.minidns.util; 012 013public class SafeCharSequence implements CharSequence { 014 015 @Override 016 public final int length() { 017 return toSafeString().length(); 018 } 019 020 @Override 021 public final char charAt(int index) { 022 return toSafeString().charAt(index); 023 } 024 025 @Override 026 public final CharSequence subSequence(int start, int end) { 027 return toSafeString().subSequence(end, end); 028 } 029 030 public String toSafeString() { 031 // The default implementation assumes that toString() returns a safe 032 // representation. Subclasses may override toSafeString() if this assumption is 033 // not correct. 034 return toString(); 035 } 036}