KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > global > ByteLongObject


1 /**
2  * com.mckoi.database.global.ByteLongObject 24 Sep 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.global;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29
30 /**
31  * A byte array that can be transferred between the client and server. This
32  * is used for transferring BLOB data to/from the database engine.
33  *
34  * @author Tobias Downer
35  */

36
37 public class ByteLongObject implements java.io.Serializable JavaDoc, BlobAccessor {
38
39   static final long serialVersionUID = -6843780673892019530L;
40
41   /**
42    * The binary data.
43    */

44   private byte[] data;
45
46   /**
47    * Constructor.
48    */

49   public ByteLongObject(byte[] from, int offset, int length) {
50     data = new byte[(int) length];
51     System.arraycopy(from, (int) offset, data, 0, (int) length);
52   }
53
54   public ByteLongObject(byte[] from) {
55     this(from, 0, from.length);
56   }
57
58   public ByteLongObject(InputStream JavaDoc in, int length) throws IOException JavaDoc {
59     data = new byte[length];
60     int i = 0;
61     while (i < length) {
62       int read = in.read(data, i, length - i);
63       if (read == -1) {
64         throw new IOException JavaDoc("Premature end of stream.");
65       }
66       i += read;
67     }
68   }
69
70   /**
71    * Returns the size of the data in this object.
72    */

73   public int length() {
74     return data.length;
75   }
76
77   /**
78    * Returns the byte at offset 'n' into the binary object.
79    */

80   public byte getByte(int n) {
81     return data[n];
82   }
83
84   /**
85    * Returns the internal byte[] of this binary object. Care needs to be
86    * taken when handling this object because altering the contents will
87    * change this object.
88    */

89   public byte[] getByteArray() {
90     return data;
91   }
92
93   /**
94    * Returns an InputStream that allows us to read the entire byte long object.
95    */

96   public InputStream JavaDoc getInputStream() {
97     return new BLOBInputStream();
98   }
99   
100   public String JavaDoc toString() {
101     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
102     if (data == null) {
103       buf.append("[ BLOB (NULL) ]");
104     }
105     else {
106       buf.append("[ BLOB size=");
107       buf.append(data.length);
108       buf.append(" ]");
109     }
110     return new String JavaDoc(buf);
111   }
112
113   /**
114    * Inner class that encapsulates the byte long object in an input stream.
115    */

116   private class BLOBInputStream extends InputStream JavaDoc {
117     
118     private int index;
119     
120     public BLOBInputStream() {
121       index = 0;
122     }
123     
124     public int read() throws IOException JavaDoc {
125       if (index >= length()) {
126         return -1;
127       }
128       int b = ((int) getByte(index)) & 0x0FF;
129       ++index;
130       return b;
131     }
132
133     public int read(byte[] buf, int off, int len) throws IOException JavaDoc {
134       // As per the InputStream specification.
135
if (len == 0) {
136         return 0;
137       }
138       
139       int size = length();
140       int to_read = Math.min(len, size - index);
141
142       if (to_read <= 0) {
143         // Nothing can be read
144
return -1;
145       }
146
147       System.arraycopy(data, index, buf, off, to_read);
148       index += to_read;
149
150       return to_read;
151     }
152     
153   }
154   
155 }
156
Popular Tags