Guest User

RandomAccessStream

a guest
Apr 23rd, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package com.example.zip;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.DataInputStream;
  5.  
  6. /*
  7. Licensed under the Apache License, Version 2.0 (the "License");
  8. you may not use this file except in compliance with the License.
  9. You may obtain a copy of the License at
  10.  
  11.     http://www.apache.org/licenses/LICENSE-2.0
  12.  
  13. Unless required by applicable law or agreed to in writing, software
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18.  */
  19.  
  20. public class RandomAccessStream extends DataInputStream {
  21.     private final ByteArrayInputStream wrappedStream;
  22.     private final int length;
  23.  
  24.     public static RandomAccessStream fromBytes(byte[] bytes) {
  25.         ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
  26.         return new RandomAccessStream(inputStream, bytes.length);
  27.     }
  28.  
  29.     public RandomAccessStream(ByteArrayInputStream wrappedStream, int length) {
  30.         super(wrappedStream);
  31.         this.wrappedStream = wrappedStream;
  32.         this.length = length;
  33.     }
  34.  
  35.     public int length() {
  36.         return length;
  37.     }
  38.  
  39.     public void seek(long pos) {
  40.         wrappedStream.reset();
  41.         wrappedStream.skip(pos);
  42.     }
  43. }
Add Comment
Please, Sign In to add comment