1 21 22 package org.apache.derby.iapi.services.io; 23 24 import java.io.IOException ; 25 import java.io.EOFException ; 26 import java.io.OutputStream ; 27 28 public class ArrayOutputStream extends OutputStream implements Limit { 29 30 private byte[] pageData; 31 32 private int start; 33 private int end; private int position; 35 36 public ArrayOutputStream() { 37 super(); 38 } 39 40 public ArrayOutputStream(byte[] data) { 41 super(); 42 setData(data); 43 } 44 45 public void setData(byte[] data) { 46 pageData = data; 47 start = 0; 48 if (data != null) 49 end = data.length; 50 else 51 end = 0; 52 position = 0; 53 54 } 55 56 59 60 public void write(int b) throws IOException { 61 if (position >= end) 62 throw new EOFException (); 63 64 pageData[position++] = (byte) b; 65 66 } 67 68 public void write(byte b[], int off, int len) throws IOException { 69 70 if ((position + len) > end) 71 throw new EOFException (); 72 73 System.arraycopy(b, off, pageData, position, len); 74 position += len; 75 } 76 77 80 81 public int getPosition() { 82 return position; 83 } 84 85 88 public void setPosition(int newPosition) 89 throws IOException { 90 if ((newPosition < start) || (newPosition > end)) 91 throw new EOFException (); 92 93 position = newPosition; 94 } 95 96 public void setLimit(int length) throws IOException { 97 98 if (length < 0) { 99 throw new EOFException (); 100 } 101 102 if ((position + length) > end) { 103 throw new EOFException (); 104 } 105 106 start = position; 107 end = position + length; 108 109 return; 110 } 111 112 public int clearLimit() { 113 114 int unwritten = end - position; 115 116 end = pageData.length; 117 118 return unwritten; 119 } 120 } 121 | Popular Tags |