001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.publickeystore;
028:
029: import java.io.*;
030:
031: /**
032: * Retrieves stored fields from an InputStream.
033: * Since Java Microedition has no serialization, this is a simple substitute.
034: */
035: class InputStorage extends Storage {
036: /** stream to read from */
037: private DataInputStream in;
038:
039: /**
040: * Constructs an InputStorage for an InputStream.
041: * @param input the input storage input stream.
042: * @exception IOException if the storage version cannot be read
043: */
044: InputStorage(InputStream input) throws IOException {
045: in = new DataInputStream(input);
046:
047: // skip past the version number.
048: in.readByte();
049: }
050:
051: /**
052: * Reads a field that was stored as tag, type, value set.
053: * @param tag byte array of one byte to hold the tag of the field that
054: * was read
055: * @return value of field that was stored, or null if there are no more
056: * fields
057: * @exception IOException if the input storage was corrupted
058: */
059: Object readValue(byte[] tag) throws IOException {
060: byte type;
061:
062: try {
063: try {
064: in.readFully(tag, 0, 1);
065: } catch (EOFException eofe) {
066: // this just means there are no more fields in storage
067: return null;
068: }
069:
070: type = in.readByte();
071: if (type == BINARY_TYPE) {
072: int len;
073: byte[] value;
074:
075: /*
076: * must read the length first, because DataOutputStream does
077: * not handle handle byte arrays.
078: */
079: len = in.readUnsignedShort();
080: if (len < 0) {
081: throw new IOException();
082: }
083:
084: value = new byte[len];
085: in.readFully(value);
086: return value;
087: }
088:
089: if (type == STRING_TYPE) {
090: return in.readUTF();
091: }
092:
093: if (type == LONG_TYPE) {
094: return new Long(in.readLong());
095: }
096:
097: if (type == BOOLEAN_TYPE) {
098: return new Boolean(in.readBoolean());
099: }
100:
101: throw new IOException();
102: } catch (IOException e) {
103: throw new IOException("input storage corrupted");
104: }
105: }
106: }
|