| | |
| | | * Dgn7OracleReader |
| | | * User: Ulysses |
| | | * Date: 2007/10/24 |
| | | * Time: ¤U¤È 01:01:08 |
| | | * Time: |
| | | */ |
| | | public class Dgn7OracleReader implements Iterator<Element> |
| | | { |
| | | public class Dgn7OracleReader implements Iterator<Element> { |
| | | private final static Logger logger = Logger.getLogger(Dgn7OracleReader.class); |
| | | |
| | | private String _sql; |
| | |
| | | private static final int FETCHSIZE = 20; |
| | | private Element _element; |
| | | |
| | | public Dgn7OracleReader(String sql, String fieldName, OracleConnection connection) |
| | | { |
| | | public Dgn7OracleReader(String sql, String fieldName, OracleConnection connection) { |
| | | this._sql = sql; |
| | | this._fieldName = fieldName; |
| | | this._connection = connection; |
| | | } |
| | | |
| | | public String getSql() |
| | | { |
| | | public String getSql() { |
| | | return _sql; |
| | | } |
| | | |
| | | public void setSql(String sql) |
| | | { |
| | | public void setSql(String sql) { |
| | | this._sql = sql; |
| | | } |
| | | |
| | | public String getFieldName() |
| | | { |
| | | public String getFieldName() { |
| | | return _fieldName; |
| | | } |
| | | |
| | | public void setFieldName(String fieldName) |
| | | { |
| | | public void setFieldName(String fieldName) { |
| | | this._fieldName = fieldName; |
| | | } |
| | | |
| | | public boolean hasNext() |
| | | { |
| | | if (_resultSet == null) |
| | | { |
| | | try |
| | | { |
| | | public boolean hasNext() { |
| | | if (_resultSet == null) { |
| | | try { |
| | | initializeReader(); |
| | | } catch (SQLException e) |
| | | { |
| | | } catch (SQLException e) { |
| | | throw new RuntimeException("initialize oralce error.", e); |
| | | } catch (Dgn7Exception e) |
| | | { |
| | | } catch (Dgn7Exception e) { |
| | | throw new RuntimeException("initialize oralce error.", e); |
| | | } |
| | | } |
| | | return _element != null; |
| | | } |
| | | |
| | | public Element next() |
| | | { |
| | | public Element next() { |
| | | Element result = _element; |
| | | |
| | | try |
| | | { |
| | | try { |
| | | fetchElement(); |
| | | } catch (SQLException e) |
| | | { |
| | | } catch (SQLException e) { |
| | | throw new RuntimeException("Error:" + e.getMessage(), e); |
| | | } catch (Dgn7Exception e) |
| | | { |
| | | } catch (Dgn7Exception e) { |
| | | throw new RuntimeException("Error:" + e.getMessage(), e); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | public void remove() |
| | | { |
| | | public void remove() { |
| | | throw new RuntimeException("Not Support this method."); |
| | | } |
| | | |
| | | private boolean initializeReader() throws SQLException, Dgn7Exception |
| | | { |
| | | private boolean initializeReader() throws SQLException, Dgn7Exception { |
| | | if (_resultSet != null) return true; |
| | | Statement stmtSrc = _connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); |
| | | |
| | |
| | | return true; |
| | | } |
| | | |
| | | private boolean fetchElement() throws SQLException, Dgn7Exception |
| | | { |
| | | if (_resultSet.next()) |
| | | { |
| | | private boolean fetchElement() throws SQLException, Dgn7Exception { |
| | | if (_resultSet.next()) { |
| | | byte[] raw = null; |
| | | Object value = _resultSet.getObject(this._fieldName); |
| | | |
| | | if (value instanceof BLOB) |
| | | { |
| | | if (value instanceof BLOB) { |
| | | BLOB blob = (BLOB) value; |
| | | |
| | | try |
| | | { |
| | | try { |
| | | raw = getBytesFromBLOB(blob); |
| | | } catch (IOException e) |
| | | { |
| | | } catch (IOException e) { |
| | | throw new Dgn7Exception("IOError", e); |
| | | } |
| | | blob.close(); |
| | | } else if (value instanceof byte[]) |
| | | { |
| | | // blob.close(); |
| | | } else if (value instanceof byte[]) { |
| | | raw = (byte[]) value; |
| | | } |
| | | if (raw == null) |
| | | { |
| | | if (raw == null) { |
| | | _element = null; |
| | | return false; |
| | | } |
| | |
| | | ElementType recordType = ElementType.forID(type); |
| | | IElementHandler handler = recordType.getElementHandler(); |
| | | _element = (Element) handler.read(buffer, signature, elementLength); |
| | | if (recordType.isComplexElement() && (elementLength < raw.length)) |
| | | { |
| | | if (recordType.isComplexElement() && (elementLength < raw.length)) { |
| | | int offset = elementLength; |
| | | while (offset < (raw.length - 4)) |
| | | { |
| | | while (offset < (raw.length - 4)) { |
| | | buffer.position(offset); |
| | | signature = buffer.getShort(); |
| | | type = (byte) ((signature >>> 8) & 0x007f); |
| | | elementLength = (buffer.getShort() * 2) + 4; |
| | | if (raw.length < (offset + elementLength)) |
| | | { |
| | | if (raw.length < (offset + elementLength)) { |
| | | System.out.println("Length not match:" + offset + ":" + buffer.position() + ":" + buffer.limit()); |
| | | break; |
| | | } |
| | | recordType = ElementType.forID(type); |
| | | handler = recordType.getElementHandler(); |
| | | if (handler != null) |
| | | { |
| | | if (handler != null) { |
| | | Element subElement = (Element) handler.read(buffer, signature, elementLength); |
| | | ((ComplexElement) _element).add(subElement); |
| | | offset += elementLength; |
| | | } else |
| | | { |
| | | } else { |
| | | byte[] remain = new byte[buffer.remaining()]; |
| | | System.arraycopy(raw, offset, remain, 0, buffer.remaining()); |
| | | for (int i = 0; i < remain.length; i++) |
| | | { |
| | | if (remain[i] != 0) |
| | | { |
| | | for (int i = 0; i < remain.length; i++) { |
| | | if (remain[i] != 0) { |
| | | logger.info("fetch element has some error. index=" + (offset + i) + ":value=" + remain[i]); |
| | | System.out.println("fetch element has some error. index=" + (offset + i) + ":value=" + remain[i]); |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | } else |
| | | { |
| | | } else { |
| | | _element = null; |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | protected static byte[] getBytesFromBLOB(BLOB blob) throws SQLException, IOException |
| | | { |
| | | protected byte[] getBytesFromBLOB(BLOB blob) throws SQLException, IOException { |
| | | byte[] raw; |
| | | |
| | | // BLOB blob = (BLOB) rs.getBlob(1); |
| | |
| | | ByteBuffer buffer = null; // ByteBuffer.allocate(optimalSize); |
| | | int len; |
| | | |
| | | try |
| | | { |
| | | while ((len = (is.read(chunk))) != -1) |
| | | { |
| | | if (buffer != null) |
| | | { |
| | | try { |
| | | while ((len = (is.read(chunk))) != -1) { |
| | | if (buffer != null) { |
| | | buffer.limit(buffer.limit() + len); |
| | | } else |
| | | { |
| | | } else { |
| | | buffer = ByteBuffer.allocate(len); |
| | | } |
| | | |
| | | buffer.put(chunk); |
| | | } |
| | | |
| | | is.close(); |
| | | assert buffer != null; |
| | | buffer.position(0); |
| | | raw = buffer.array(); |
| | | } catch (IOException e) |
| | | { |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } finally { |
| | | try { |
| | | is.close(); |
| | | } catch (IOException e) { |
| | | logger.warn("InputStream cannot close", e); |
| | | } |
| | | ; |
| | | } |
| | | |
| | | return raw; |