Wraps a stream, printing to standard out everything that is read from it. : Reader « File Input Output « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » File Input Output » ReaderScreenshots 
Wraps a stream, printing to standard out everything that is read from it.
      
/*
 * LoggingWriter.java Created Sep 7, 2010 by Andrew Butler, PSL
 */
//package prisms.util;

import java.io.IOException;
import java.nio.CharBuffer;

/** Wraps a stream, printing to standard out everything that is read from it. */
public class LoggingReader extends java.io.Reader {
  private java.io.Reader theBase;

  private java.io.Writer theLog;

  /**
   @param base
   *            The reader to wrap
   @param file
   *            The name of the file to log to, or null to log to System.out
   @throws IOException
   *             If an error occurs setting up the log file
   */
  public LoggingReader(java.io.Reader base, String filethrows IOException {
    theBase = base;
    if (file != null)
      theLog = new java.io.BufferedWriter(new java.io.FileWriter(file));
  }

  /**
   @return The reader wrapped by this logging reader
   */
  public java.io.Reader getBase() {
    return theBase;
  }

  @Override
  public int read(CharBuffer targetthrows IOException {
    int read = theBase.read(target);
    if (theLog != null)
      theLog.write(target.array()0, read);
    else
      System.out.print(target.toString().substring(0, read));
    return read;
  }

  @Override
  public int read() throws IOException {
    int read = theBase.read();
    if (read >= 0) {
      if (theLog != null)
        theLog.write(read);
      else
        System.out.print((charread);
    }
    return read;
  }

  @Override
  public int read(char[] cbufthrows IOException {
    int read = theBase.read(cbuf);
    if (theLog != null)
      theLog.write(cbuf, 0, read);
    else
      System.out.print(new String(cbuf, 0, read));
    return read;
  }

  @Override
  public int read(char[] cbuf, int off, int lenthrows IOException {
    int read = theBase.read(cbuf, off, len);
    if (theLog != null)
      theLog.write(cbuf, off, read);
    else
      System.out.print(new String(cbuf, off, read));
    return read;
  }

  @Override
  public long skip(long nthrows IOException {
    return super.skip(n);
  }

  @Override
  public boolean ready() throws IOException {
    return theBase.ready();
  }

  @Override
  public boolean markSupported() {
    return false;
  }

  @Override
  public void close() throws java.io.IOException {
    if (theLog != null)
      theLog.close();
    else
      System.out.flush();
    theBase.close();
  }
}

   
    
    
    
    
    
  
Related examples in the same category
1.Reader to StringBuilder
2.An Iterator over the lines in a Reader
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.