@@ -0,0 +1,2163 @@
+package jeranvier.io;%0A%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Arrays;%0A%0Apublic class SlidingWindowInputStream extends InputStream%7B%0A%09%0A%09private static final int INFLATION_FACTOR = 5; // if we have a window of 6, we collect the elements 30 by 30 form the underlying stream%0A%09InputStream in; //underlying stream%0A%09private int frameSize; // the size of the window%0A%09private int frameShift; // the amount by which the window is shifted ( allow overlapping windows)%0A%09private int indexStartFrame; //index at which the next window is starting%0A%09private byte%5B%5D read; // buffer%0A%09private int offset; // amount of element that are still in the read array before reading new elements%0A%09private int inCounter; //how many elements have been last read from the underlying stream%0A%09%0A%09public SlidingWindowInputStream(InputStream in, int frameSize, int frameShift) %7B%0A%09%09this.in = in;%0A%09%09this.frameSize = frameSize;%0A%09%09this.frameShift = frameShift;%0A%09%09indexStartFrame = 0;%0A%09%09inCounter = 0;%0A%09%09read = new byte%5B0%5D;%0A%09%7D%0A%0A%09@Override%0A%09public int read() throws IOException %7B%0A%09%09return 0;%0A%09%7D%0A%0A%09@Override%0A%09public int read(byte%5B%5D b) throws IOException %7B%0A%09%09//if not enough data, read next chunk%0A%09%09if(indexStartFrame + frameSize %3E offset + inCounter)%7B%0A%09%09%09if(read.length != 0)%7B%0A%09%09%09%09read = Arrays.copyOfRange(read, indexStartFrame, indexStartFrame + read.length);%0A%09%09%09%09offset = read.length - indexStartFrame;%0A%09%09%09%7D%0A%09%09%09else%7B%0A%09%09%09%09read = new byte%5BframeSize*INFLATION_FACTOR%5D;%0A%09%09%09%09offset = 0;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09inCounter = in.read(read, offset, read.length - offset);%0A%09%09%09indexStartFrame = 0;%0A%09%09%7D%0A%09%09%0A%09%09//if we cannot write anymore complete frames%0A%09%09if(inCounter + offset %3E= indexStartFrame + frameSize && inCounter == -1)%7B%0A%09%09%09b = new byte%5B0%5D;%0A%09%09%09return -1;%0A%09%09%7D%0A%09%09%0A%09%09//if enough data, write frame, otherwise, we will have exit the method in the lines above%0A%09%09else if(inCounter + offset %3E= indexStartFrame + frameSize)%7B%0A%09%09%09for(int i = indexStartFrame; i %3C indexStartFrame + frameSize; i++)%7B%0A%09%09%09%09b%5Bi-indexStartFrame%5D = read%5Bi%5D;%0A%09%09%09%7D%0A%09%09%09indexStartFrame += frameShift;%0A%09%09%09return frameSize;%0A%09%09%7D%0A%09%09else%7B //to make it complete, but should never be used%0A%09%09%09b = new byte%5B0%5D;%0A%09%09%09return -1;%0A%09%09%7D%0A%09%7D%0A%0A%0A%7D%0A

ADD SlidingWindowInputStream (swis) which take an underlying stream and chunk it in sliding windows ( the windows can overlap)