Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:39:09+00:00 2026-06-08T02:39:09+00:00

I am wrapping Freemarker template loader in Spring MVC as described here to have

  • 0

I am wrapping Freemarker template loader in Spring MVC as described here to have default escaping in html pages.

So, I need to wrap content from java.io.Reader with my strings, not reading it, not converting it to string and not implementing myself. Is there class similar to WrappingReader or com.google.common.io.MultiReader (which is not public) in popular common libraries?

My implementation:

import com.google.common.io.CharStreams;
import com.google.common.io.InputSupplier;

...

private final TemplateLoader delegate;

@Autowired
public HtmlEscapingTemplateLoader(ResourceLoader resourceLoader)
{
    delegate = new SpringTemplateLoader(resourceLoader, "/WEB-INF/templates/");
}

@Override
public Reader getReader(Object templateSource, String encoding) throws IOException
{
    // collecting readers
    Reader prologue = new StringReader("<#escape x as x?html>");
    Reader originalReader = delegate.getReader(templateSource, encoding);
    Reader epilogue = new StringReader("</#escape>");

    // concatenating readers
    return merge(prologue, originalReader, epilogue);
}


protected Reader merge(Reader prologue, Reader originalReader, Reader epilogue) throws IOException
{
    return CharStreams.join(
            Arrays.asList(new ReaderSupplier(prologue), new ReaderSupplier(originalReader), new ReaderSupplier(
                    epilogue))).getInput();
}

private static class ReaderSupplier
        implements InputSupplier<Reader>
{

    private final Reader reader;

    public ReaderSupplier(Reader reader)
    {
        this.reader = reader;
    }

    @Override
    public Reader getInput() throws IOException
    {
        return reader;
    }

}

CharStreams i use is marked as @com.google.common.annotations.Beta. So, could it be rewritten in more solid way without usage of CharStreams?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:39:10+00:00Added an answer on June 8, 2026 at 2:39 am

    Guava contributor here. …Okay.

    @Beta does not mean “not fully tested” or “not widely used.” The only thing it means is that we’re not quite sure we’re ready to freeze the API. That’s only a problem if you’re developing a library, or planning to upgrade versions of Guava later. (Also, honestly, CharStreams is about as stable as @Beta APIs get…)

    You should not have a ReaderSupplier class, which defeats the whole point of the InputSupplier interface. For this particular case, instead of passing around Readers, you should be passing around InputSupplier<Reader>s. In this particular case, CharStreams.newReaderSupplier(String) returns an InputSupplier that produces StringReaders, so that fits this use case perfectly.

    In any event, my complete implementation would be something like:

    static final String prologue = "<#escape x as x?html>";
    static final String epilogue = "</#escape>";
    
    // ideally you shouldn't be passing around Readers at all
    // the point of InputSupplier, etc. is that you should never get direct access
    // to the Reader directly, so you don't have to track whether it's closed or not
    public InputSupplier<Reader> getReaderSupplier(
        final Object templateSource, final String encoding) {
      return CharStreams.join(
        CharStreams.newReaderSupplier(prologue),
        new InputSupplier<Reader>() {
          public Reader getInput() {
            return delegate.getReader(templateSource, encoding);
          }
        },
        CharStreams.newReaderSupplier(epilogue));
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I'm wrapping an HTML tag with another tag '$holder' below. $holder = $('<div
I need wrapping each word with a tag (e. span) in a HTML document,
I'm having problems wrapping my head around this. I have a function void foo(istream&
I'm wrapping up an implementation of Spring Security in Grails. This is my first
I'm wrapping a huge amount of HTML by $() It takes about 250 ms.
I have trouble wrapping my head around a peculiar feature of the JSON data
I have an wrapping object array -in fact a matrix- which includes a checkbox,
I'm having trouble wrapping my head around how to set this up. I have
I need good word-wrapping handling for Java. Not too difficult, except for one wrinkle:
Having a hard time wrapping my head around this one. I have a Page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.