Filter (seach and replace) array of bytes in an InputStream
2013-05-08 19:23
736 查看
import java.io.*; import java.util.*; class ReplacingInputStream extends FilterInputStream { LinkedList<Integer> inQueue = new LinkedList<Integer>(); LinkedList<Integer> outQueue = new LinkedList<Integer>(); final byte[] search, replacement; protected ReplacingInputStream(InputStream in, byte[] search, byte[] replacement) { super(in); this.search = search; this.replacement = replacement; } private boolean isMatchFound() { Iterator<Integer> inIter = inQueue.iterator(); for (int i = 0; i < search.length; i++) if (!inIter.hasNext() || search[i] != inIter.next()) return false; return true; } private void readAhead() throws IOException { // Work up some look-ahead. while (inQueue.size() < search.length) { int next = super.read(); inQueue.offer(next); if (next == -1) break; } } @Override public int read() throws IOException { // Next byte already determined. if (outQueue.isEmpty()) { readAhead(); if (isMatchFound()) { for (int i = 0; i < search.length; i++) inQueue.remove(); for (byte b : replacement) outQueue.offer((int) b); } else outQueue.add(inQueue.remove()); } return outQueue.remove(); } // TODO: Override the other read methods. } class Test { public static void main(String[] args) throws Exception { byte[] bytes = "hello xyz world.".getBytes("UTF-8"); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); byte[] search = "xyz".getBytes("UTF-8"); byte[] replacement = "abc".getBytes("UTF-8"); InputStream ris = new ReplacingInputStream(bis, search, replacement); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b; while (-1 != (b = ris.read())) bos.write(b); System.out.println(new String(bos.toByteArray())); } }
相关文章推荐
- An instance of getting the maximum and minimum values in an array
- CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters
- Given an array of size N in which every number is between 1 and N, determine if there are any dupli
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- Can we use function on left side of an expression in C and C++?
- Leetcode Maximum XOR of Two Numbers in an Array
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
- how to accepte the array of the checkbox in asp and in php
- Equivalent of CONTAINS and LIKE in an IF statement
- separate odd and even numbers in an array in O(n) time complexity and O(1) space complexity
- Given an array of positive and negative integers find the first subarray with zero sum
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically
- how to remove nil and blank string in an array in Ruby
- Maximum XOR of Two Numbers in an Array
- Multidimensional Array And an Array of Arrays
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Matching Portions of A String to Elements of an Array in Ruby
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)