001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sql.rowset.serial;
019:
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.Reader;
025: import java.math.BigDecimal;
026: import java.net.URL;
027: import java.sql.Array;
028: import java.sql.Blob;
029: import java.sql.Clob;
030: import java.sql.Date;
031: import java.sql.Ref;
032: import java.sql.SQLData;
033: import java.sql.SQLException;
034: import java.sql.SQLOutput;
035: import java.sql.Struct;
036: import java.sql.Time;
037: import java.sql.Timestamp;
038: import java.util.HashMap;
039: import java.util.Map;
040: import java.util.Vector;
041:
042: import org.apache.harmony.luni.util.NotImplementedException;
043: import org.apache.harmony.sql.internal.nls.Messages;
044:
045: public class SQLOutputImpl implements SQLOutput {
046: private Vector attributes;
047:
048: private Map map;
049:
050: /**
051: * Constructs a new SQLOutputImpl object using a list of attributes and a
052: * custom name-type map. JDBC drivers will use this map to identify which
053: * SQLData.writeSQL will be invoked.
054: *
055: * @param attributes -
056: * the list of given attribute objects.
057: * @param map -
058: * the UDT(user defined type) name-type map
059: * @throws SQLException -
060: * if the attributes or the map is null
061: */
062: public SQLOutputImpl(Vector<?> attributes, Map<String, ?> map)
063: throws SQLException {
064: if (null == attributes || null == map) {
065: throw new SQLException(Messages.getString("sql.33")); //$NON-NLS-1$
066: }
067: this .attributes = attributes;
068: this .map = map;
069: }
070:
071: /**
072: * {@inheritDoc}
073: *
074: * @see java.sql.SQLOutput#writeArray(Array)
075: */
076: @SuppressWarnings("unchecked")
077: public void writeArray(Array theArray) throws SQLException {
078: if (theArray != null) {
079: SerialArray serialArray = new SerialArray(theArray, map);
080: attributes.addElement(serialArray);
081: } else {
082: attributes.addElement(theArray);
083: }
084: }
085:
086: /**
087: * {@inheritDoc}
088: *
089: * @see java.sql.SQLOutput#writeAsciiStream(InputStream)
090: */
091: @SuppressWarnings("unchecked")
092: public void writeAsciiStream(InputStream theStream)
093: throws SQLException {
094: BufferedReader br = new BufferedReader(new InputStreamReader(
095: theStream));
096: StringBuffer stringBuffer = new StringBuffer();
097: String line;
098: try {
099: line = br.readLine();
100: while (line != null) {
101: stringBuffer.append(line);
102: line = br.readLine();
103: }
104: attributes.addElement(stringBuffer.toString());
105: } catch (IOException e) {
106: throw new SQLException();
107: }
108: }
109:
110: /**
111: * {@inheritDoc}
112: *
113: * @see java.sql.SQLOutput#writeBigDecimal(BigDecimal)
114: */
115: @SuppressWarnings("unchecked")
116: public void writeBigDecimal(BigDecimal theBigDecimal)
117: throws SQLException {
118: attributes.addElement(theBigDecimal);
119: }
120:
121: /**
122: * {@inheritDoc}
123: *
124: * FIXME So far NO difference has been detected between writeBinaryStream
125: * and writeAsciiStream in RI. Keep their implementation same temporarily
126: * until some bug is found.
127: *
128: * @see java.sql.SQLOutput#writeBinaryStream(InputStream)
129: */
130: @SuppressWarnings("unchecked")
131: public void writeBinaryStream(InputStream theStream)
132: throws SQLException {
133: writeAsciiStream(theStream);
134: }
135:
136: /**
137: * {@inheritDoc}
138: *
139: * @see java.sql.SQLOutput#writeBlob(Blob)
140: */
141: @SuppressWarnings("unchecked")
142: public void writeBlob(Blob theBlob) throws SQLException {
143: if (theBlob != null) {
144: SerialBlob serialBlob = new SerialBlob(theBlob);
145: attributes.addElement(serialBlob);
146: } else {
147: attributes.addElement(theBlob);
148: }
149: }
150:
151: /**
152: * {@inheritDoc}
153: *
154: * @see java.sql.SQLOutput#writeBoolean(boolean)
155: */
156: @SuppressWarnings({"boxing","unchecked"})
157: public void writeBoolean(boolean theFlag) throws SQLException {
158: attributes.addElement(theFlag);
159: }
160:
161: /**
162: * {@inheritDoc}
163: *
164: * @see java.sql.SQLOutput#writeByte(byte)
165: */
166: @SuppressWarnings({"boxing","unchecked"})
167: public void writeByte(byte theByte) throws SQLException {
168: attributes.addElement(theByte);
169: }
170:
171: /**
172: * {@inheritDoc}
173: *
174: * @see java.sql.SQLOutput#writeBytes(byte[])
175: */
176: @SuppressWarnings({"boxing","unchecked"})
177: public void writeBytes(byte[] theBytes) throws SQLException {
178: attributes.addElement(theBytes);
179: }
180:
181: /**
182: * {@inheritDoc}
183: *
184: * @see java.sql.SQLOutput#writeCharacterStream(Reader)
185: */
186: @SuppressWarnings("unchecked")
187: public void writeCharacterStream(Reader theStream)
188: throws SQLException {
189: BufferedReader br = new BufferedReader(theStream);
190: StringBuffer stringBuffer = new StringBuffer();
191: String line;
192: try {
193: line = br.readLine();
194: while (line != null) {
195: stringBuffer.append(line);
196: line = br.readLine();
197: }
198: attributes.addElement(stringBuffer.toString());
199: } catch (IOException e) {
200: throw new SQLException();
201: }
202: }
203:
204: /**
205: * {@inheritDoc}
206: *
207: * @see java.sql.SQLOutput#writeClob(Clob)
208: */
209: @SuppressWarnings("unchecked")
210: public void writeClob(Clob theClob) throws SQLException {
211: if (theClob != null) {
212: SerialClob serialClob = new SerialClob(theClob);
213: attributes.addElement(serialClob);
214: } else {
215: attributes.addElement(theClob);
216: }
217: }
218:
219: /**
220: * {@inheritDoc}
221: *
222: * @see java.sql.SQLOutput#writeDate(Date)
223: */
224: @SuppressWarnings("unchecked")
225: public void writeDate(Date theDate) throws SQLException {
226: attributes.addElement(theDate);
227: }
228:
229: /**
230: * {@inheritDoc}
231: *
232: * @see java.sql.SQLOutput#writeDouble(double)
233: */
234: @SuppressWarnings({"boxing","unchecked"})
235: public void writeDouble(double theDouble) throws SQLException {
236: attributes.addElement(theDouble);
237: }
238:
239: /**
240: * {@inheritDoc}
241: *
242: * @see java.sql.SQLOutput#writeFloat(float)
243: */
244: @SuppressWarnings({"boxing","unchecked"})
245: public void writeFloat(float theFloat) throws SQLException {
246: attributes.addElement(theFloat);
247: }
248:
249: /**
250: * {@inheritDoc}
251: *
252: * @see java.sql.SQLOutput#writeInt(int)
253: */
254: @SuppressWarnings({"boxing","unchecked"})
255: public void writeInt(int theInt) throws SQLException {
256: attributes.addElement(theInt);
257: }
258:
259: /**
260: * {@inheritDoc}
261: *
262: * @see java.sql.SQLOutput#writeLong(long)
263: */
264: @SuppressWarnings({"boxing","unchecked"})
265: public void writeLong(long theLong) throws SQLException {
266: attributes.addElement(theLong);
267: }
268:
269: /**
270: * {@inheritDoc}
271: *
272: * @see java.sql.SQLOutput#writeObject(SQLData)
273: */
274: @SuppressWarnings("unchecked")
275: public void writeObject(SQLData theObject) throws SQLException {
276: if (theObject == null) {
277: attributes.addElement(null);
278: } else {
279: attributes.addElement(new SerialStruct(theObject,
280: new HashMap(map)));
281: }
282: }
283:
284: /**
285: * {@inheritDoc}
286: *
287: * @see java.sql.SQLOutput#writeRef(Ref)
288: */
289: @SuppressWarnings("unchecked")
290: public void writeRef(Ref theRef) throws SQLException {
291: if (theRef != null) {
292: SerialRef serialRef = new SerialRef(theRef);
293: attributes.addElement(serialRef);
294: } else {
295: attributes.addElement(theRef);
296: }
297: }
298:
299: /**
300: * {@inheritDoc}
301: *
302: * @see java.sql.SQLOutput#writeShort(short)
303: */
304: @SuppressWarnings({"boxing","unchecked"})
305: public void writeShort(short theShort) throws SQLException {
306: attributes.addElement(theShort);
307: }
308:
309: /**
310: * {@inheritDoc}
311: *
312: * @see java.sql.SQLOutput#writeString(String)
313: */
314: @SuppressWarnings("unchecked")
315: public void writeString(String theString) throws SQLException {
316: attributes.addElement(theString);
317: }
318:
319: /**
320: * {@inheritDoc}
321: *
322: * @see java.sql.SQLOutput#writeStruct(Struct)
323: */
324: @SuppressWarnings("unchecked")
325: public void writeStruct(Struct theStruct) throws SQLException {
326: if (theStruct != null) {
327: SerialStruct serialStruct = new SerialStruct(theStruct, map);
328: attributes.addElement(serialStruct);
329: } else {
330: attributes.addElement(theStruct);
331: }
332: }
333:
334: /**
335: * {@inheritDoc}
336: *
337: * @see java.sql.SQLOutput#writeTime(Time)
338: */
339: @SuppressWarnings("unchecked")
340: public void writeTime(Time theTime) throws SQLException {
341: attributes.addElement(theTime);
342: }
343:
344: /**
345: * {@inheritDoc}
346: *
347: * @see java.sql.SQLOutput#writeTimestamp(Timestamp)
348: */
349: @SuppressWarnings("unchecked")
350: public void writeTimestamp(Timestamp theTimestamp)
351: throws SQLException {
352: attributes.addElement(theTimestamp);
353: }
354:
355: /**
356: * {@inheritDoc}
357: *
358: * @see java.sql.SQLOutput#writeURL(URL)
359: */
360: @SuppressWarnings("unchecked")
361: public void writeURL(URL theURL) throws SQLException {
362: if (theURL != null) {
363: SerialDatalink serialDatalink = new SerialDatalink(theURL);
364: attributes.addElement(serialDatalink);
365: } else {
366: attributes.addElement(theURL);
367: }
368: }
369: }
|