001/*- 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2025 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.parser; 021 022import jakarta.annotation.Nonnull; 023 024import java.io.IOException; 025import java.io.Reader; 026import java.io.StringWriter; 027 028public class PreserveStringReader extends Reader { 029 030 private final Reader myReader; 031 032 private final StringWriter myWriter; 033 034 public PreserveStringReader(Reader theReader) { 035 super(theReader); 036 myReader = theReader; 037 myWriter = new StringWriter(); 038 } 039 040 @Override 041 public int read(@Nonnull char[] theBuffer, int theOffset, int theLength) throws IOException { 042 int out = myReader.read(theBuffer, theOffset, theLength); 043 if (out >= 0) { 044 myWriter.write(theBuffer, theOffset, out); 045 } 046 047 return out; 048 } 049 050 @Override 051 public void close() throws IOException { 052 myReader.close(); 053 myWriter.close(); 054 } 055 056 public boolean hasString() { 057 return myWriter.getBuffer().length() > 0; 058 } 059 060 public String toString() { 061 return myWriter.toString(); 062 } 063}