Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31287 > unrolled thread

How to extend FilterInputStream to convert tabs to spaces?

Started byfangyuncn@gmail.com
First post2015-03-08 06:47 -0700
Last post2015-04-18 21:10 -0400
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to extend FilterInputStream to convert tabs to spaces? fangyuncn@gmail.com - 2015-03-08 06:47 -0700
    Re: How to extend FilterInputStream to convert tabs to spaces? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-03-08 11:33 -0400
      Re: How to extend FilterInputStream to convert tabs to spaces? Fang Yun <fangyuncn@gmail.com> - 2015-03-09 01:08 -0700
        Re: How to extend FilterInputStream to convert tabs to spaces? Arne Vajhøj <arne@vajhoej.dk> - 2015-04-18 21:10 -0400

#31287 — How to extend FilterInputStream to convert tabs to spaces?

Fromfangyuncn@gmail.com
Date2015-03-08 06:47 -0700
SubjectHow to extend FilterInputStream to convert tabs to spaces?
Message-ID<9753acf9-1cb7-48b8-8a9c-4656d5772c93@googlegroups.com>
Hi, I need to write a class Tabs2SpaceInputStream, it extends java.io.FilterInputStream. the code snippet like this:

public class Tabs2SpaceInputStream extends FilterInputStream {

	/**
	 * @param in
	 */
	public Tabs2SpaceInputStream(InputStream in) {
		super(in);
	}

	@Override
	public int read() throws IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public int read(byte[] b) throws IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		// TODO Auto-generated method stub
	}
	
}

This class is used to convert tabs to space when the under inpustream is read. Who give some help about this issue? Thanks a lot!.

Yun

[toc] | [next] | [standalone]


#31288

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-03-08 11:33 -0400
Message-ID<mdhq2f$uru$1@dont-email.me>
In reply to#31287
On 3/8/2015 9:47 AM, fangyuncn@gmail.com wrote:
> Hi, I need to write a class Tabs2SpaceInputStream, it extends java.io.FilterInputStream. the code snippet like this:
>
> public class Tabs2SpaceInputStream extends FilterInputStream {
>
> 	/**
> 	 * @param in
> 	 */
> 	public Tabs2SpaceInputStream(InputStream in) {
> 		super(in);
> 	}
>
> 	@Override
> 	public int read() throws IOException {
> 		// TODO Auto-generated method stub
> 	}
>
> 	@Override
> 	public int read(byte[] b) throws IOException {
> 		// TODO Auto-generated method stub
> 	}
>
> 	@Override
> 	public int read(byte[] b, int off, int len) throws IOException {
> 		// TODO Auto-generated method stub
> 	}
> 	
> }
>
> This class is used to convert tabs to space when the under inpustream is read. Who give some help about this issue? Thanks a lot!.

     Because this has the scent of homework about it I won't offer a
fully-worked-out solution, nor even a code sketch.  But here are a
few observations and hints that might get you started:

     - Are you really sure you want to extend FilterInputStream for
this job?  I suspect FilterReader would be a better choice (which
would, of course, involve a different set of read() methods).

     - To decide how many spaces a tab should expand to, you'll need
to keep track of the current horizontal position.  So: That's one of
the state variables the class requires.

     - Keeping track of the horizontal position requires detecting
the ends of line so you know when to reset the position to zero.

    - You'll also need to know the positions at which the tabs are
set: Every eight spaces, or every four, or at columns 10,15,35,...
So: That's another state variable.

     - For simplicity's sake I'd be inclined to supply the tab positions
(or width) as a constructor parameter; changing the tabs on the fly
could get complicated, and I'd provide for it only if you're pretty sure
you'll need it.  So, this state "variable" might actually be a `final'.

     - Since a read() call might return only part of a tab's expansion
to spaces, you'll need to keep track of how many spaces are "pending"
before consulting the InputStream (or Reader) again. There's a third
state variable.

     - Finally, you should give some thought to the skip() method.

     I hope this helps.  If you run into trouble, post what you've
managed to write and someone's sure to offer advice.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#31293

FromFang Yun <fangyuncn@gmail.com>
Date2015-03-09 01:08 -0700
Message-ID<5645bc64-1847-4b18-88bc-bf34e7ccf608@googlegroups.com>
In reply to#31288
OK, thanks for your advice. In fact, this job is my actually work problem. I will try to solve this problem according to the above suggestions.

[toc] | [prev] | [next] | [standalone]


#31584

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-04-18 21:10 -0400
Message-ID<55330096$0$286$14726298@news.sunsite.dk>
In reply to#31293
On 3/9/2015 4:08 AM, Fang Yun wrote:
> OK, thanks for your advice. In fact, this job is my actually work problem. I will try to solve this problem according to the above suggestions.

It does not sound like a work problem. If this task is a problem
at work, then you will have hundreds/thousands of problems at work.

Anyway if it is home work, then the assignment should be turned in
by now.

So here is some code.

package tabfun;

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.util.LinkedList;
import java.util.Queue;

public class TabConverterReader extends FilterReader {
     private TabConverter cvt;
     private Queue<Character> todobuf;
     public TabConverterReader(Reader in, TabConverter cvt) {
         super(in);
         this.cvt = cvt;
         this.todobuf = new LinkedList<Character>();
     }
     @Override
     public int read() throws IOException {
         if(!todobuf.isEmpty()) {
             return todobuf.remove();
         }
         int c = super.read();
         switch(c) {
             case -1  : // done
                        return c;
             case '\n': cvt.newLine();
                        return c;
             case '\t': for(char sp : cvt.tab().toCharArray()) 
todobuf.add(sp);
                        return todobuf.remove();
             default  : cvt.nonTab();
                        return c;
         }
     }
     @Override
     public int read(char[] buf, int off, int len) throws IOException {
         int n = 0;
         int c;
         while((n < len) && ((c = read()) >= 0)) {
             buf[off + n] = (char)c;
             n++;
         }
         return (n > 0) ? n : -1;
     }
}


package tabfun;

public interface TabConverter {
     public void newLine();
     public void nonTab();
     public String tab();
}


package tabfun;

public abstract class TabConverterBase implements TabConverter {
     protected int linelen;
     public TabConverterBase() {
         linelen = 0;
     }
     @Override
     public void newLine() {
         linelen = 0;
     }
     @Override
     public void nonTab() {
         linelen++;
     }
     @Override
     public abstract String tab();
}

package tabfun;

public class TabConverterEveryN extends TabConverterBase {
     private int n;
     public TabConverterEveryN(int n) {
         this.n = n;
     }
     @Override
     public String tab() {
         StringBuilder sb = new StringBuilder();
         do {
             sb.append(" ");
             linelen++;
         } while((linelen % n) != 0);
         return sb.toString();
     }
}

package tabfun;

import java.util.List;

public class TabConverterList extends TabConverterBase {
     private List<Integer> pos;
     public TabConverterList(List<Integer> pos) {
         this.pos = pos;
     }
     @Override
     public String tab() {
         StringBuilder sb = new StringBuilder();
         do {
             sb.append(" ");
             linelen++;
         } while(!pos.contains(linelen));
         return sb.toString();
     }
}

Arne


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web