/* Basties Note: Implementation using dotnet visible modifier * protected internal and so we do not use reflection. */ /* * Reads all bytes from {@link java.io.ByteArrayInputStream} using its * underlying buffer directly. * * @return an underlying buffer, if a current position is at the buffer * beginning, and an end position is at the buffer end, or a copy of * the underlying buffer part. */ private static byte[] expose(java.io.ByteArrayInputStream bais) { byte[] buffer, buf; int pos; lock (bais) { int available = bais.available(); try { buf = bais.buf; pos = bais.pos; } catch (java.lang.IllegalAccessException iae) { throw new java.lang.InternalError(iae.getLocalizedMessage()); } if (pos == 0 && available == buf.Length) { buffer = buf; } else { buffer = new byte[available]; java.lang.SystemJ.arraycopy(buf, pos, buffer, 0, available); } bais.skip(available); } return(buffer); }
/** * Creates an object using serialization. * * @return the new object */ public Object create() { java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(512); java.io.ByteArrayInputStream bais = null; try { java.io.ObjectOutputStream outJ = new java.io.ObjectOutputStream(baos); outJ.writeObject(iPrototype); bais = new java.io.ByteArrayInputStream(baos.toByteArray()); java.io.ObjectInputStream inJ = new java.io.ObjectInputStream(bais); return(inJ.readObject()); } catch (java.lang.ClassNotFoundException ex) { throw new FunctorException(ex); } catch (java.io.IOException ex) { throw new FunctorException(ex); } finally { try { if (bais != null) { bais.close(); } } catch (java.io.IOException ex) { // ignore } try { if (baos != null) { baos.close(); } } catch (java.io.IOException ex) { // ignore } } }
public static void main(String[] args) { try { java.io.ByteArrayOutputStream outJ = new java.io.ByteArrayOutputStream(); ZOutputStream zOut = new ZOutputStream(outJ, JZlib.Z_BEST_COMPRESSION); String hello = "Hello World!"; //java.io.ObjectOutputStream objOut = new java.io.ObjectOutputStream(zOut); //objOut.writeObject(hello); outJ.write(hello.getBytes()); zOut.close(); java.io.ByteArrayInputStream inJ = new java.io.ByteArrayInputStream(outJ.toByteArray()); ZInputStream zIn = new ZInputStream(inJ); //java.io.ObjectInputStream objIn=new java.io.ObjectInputStream(zIn); byte[] buffer = new byte[hello.length()]; inJ.read(buffer); java.lang.SystemJ.outJ.println(new java.lang.StringJ(buffer).ToString() /*objIn.readObject()*/); } catch (java.lang.Exception e) { e.printStackTrace(); } }
/* * * Read SQL Statements from the SQLStream, and * return a result set for the last SQL Statement * executed. * * @returns the ResultSet or null, if no result set was created * @exception TinySQLException * */ protected virtual TsResultSet sql(Object s) //throws TinySQLException { /* * Build the ResultSet */ TsResultSet rs = null; TinySQLTable jtbl; TinySQLPreparedStatement pstmt = (TinySQLPreparedStatement)null; bool distinct = false; java.util.Vector <Object> actions, columns, columnDefs, values; String actionType, orderType, tableName, statementType, byteString; java.util.Hashtable <Object, Object> h, selectTables; byte[] bStream; java.io.ByteArrayInputStream st; int i; String actionString; groupBreak = false; statementType = s.getClass().getName(); try { /* * Instantiate a new parser object which reads from the SQLStream. This * should probably be changed to a String at some point. Note that * parsing is only done once for a PreparedStatement. */ actions = (java.util.Vector <Object>)null; if (statementType.endsWith("tinySQLPreparedStatement")) { pstmt = (TinySQLPreparedStatement)s; pstmt.updateActions(actions); actions = pstmt.getActions(); byteString = pstmt.getSQLString(); bStream = (byte[])null; if (pstmt.getSQLString() != (String)null) { bStream = pstmt.getSQLString().getBytes(); } } else if (statementType.endsWith("tinySQLStatement")) { bStream = ((TinySQLStatement)s).getSQLString().getBytes(); } else { throw new TinySQLException("Unknown statement type" + statementType); } if (actions == (java.util.Vector <Object>)null) { st = new java.io.ByteArrayInputStream(bStream); SQLStream = (java.io.InputStream)st; TinySQLParser tinyp = new TinySQLParser(SQLStream, this); TinySQLGlobals.writeLongNames(); actions = tinyp.getActions(); if (statementType.endsWith("tinySQLPreparedStatement")) { pstmt.updateActions(actions); } } /* * The actions Vector consists of a list of Hashtables. Each of these * action Hashtables contains elements required for a particular SQL * statement. The following elements are used for various actions; * * Type Name Description * * String tableName The name of the affected table for * CREATE,INSERT,UPDATE,DELETE actions. * * Hashtable selectTables Hashtable of tables in a SELECT action. * * Vector columns A list of column names used by the * the action. * * Vector columnContexts A list of Strings indicating the context * for the elements in the columns Vector. * Values can be SELECT,ORDER,GROUP. * * Vector columnDefs A list of column objects used by the * CREATE TABLE and ALTER TABLE ADD actions. * * Vector values A list of String values used in INSERT * and UPDATE actions. * * String oldColumnName Old column name for the * ALTER TABLE RENAME action. * * String newColumnName New column name for the * ALTER TABLE RENAME action. * * String orderType Type or ORDER BY - ASC or DESC. * * String distinct Distinct rows only - TRUE or NULL * * tinySQLWhere whereClause An object containing the where clause * which can be updated and queried. */ for (i = 0; i < actions.size(); i++) { h = (java.util.Hashtable <Object, Object>)actions.elementAt(i); actionType = (String)h.get("TYPE"); if (TinySQLGlobals.DEBUG) { java.lang.SystemJ.outJ.println("Action: " + actionType); } /* * Many actions have a table specification. If this one, build * a table object to update the where clause if there is one. */ tableName = (String)h.get("TABLE"); wc = (TinySQLWhere)h.get("WHERE"); if (tableName != (String)null & !actionType.equals("DROP_TABLE") & !actionType.equals("CREATE_TABLE") & !actionType.equals("INSERT") & !actionType.startsWith("ALTER")) { jtbl = getTable(tableName); /* * For prepared statements, store any table objects that * are created so that they can be explicitly closed when * the statement processing is complete. */ if (statementType.endsWith("tinySQLPreparedStatement")) { pstmt.addTable(jtbl); } } actionString = UtilString.actionToString(h); if (debug) { java.lang.SystemJ.outJ.println("ACTION: " + actionString); } if (actionType.equals("UPDATE")) { /* * SQL UPDATE */ columns = (java.util.Vector <Object>)h.get("COLUMNS"); values = (java.util.Vector <Object>)h.get("VALUES"); UpdateStatement(tableName, columns, values, wc); } else if (actionType.equals("DELETE")) { /* * SQL DELETE */ DeleteStatement(tableName, wc); } else if (actionType.equals("SELECT")) { /* * SQL SELECT */ selectTables = (java.util.Hashtable <Object, Object>)h.get("TABLES"); columns = (java.util.Vector <Object>)h.get("COLUMNS"); orderType = (String)h.get("ORDER_TYPE"); if ((String)h.get("DISTINCT") != (String)null) { distinct = true; } rs = SelectStatement(selectTables, columns, wc, orderType, distinct, s); } else if (actionType.equals("INSERT")) { /* * SQL INSERT */ columns = (java.util.Vector <Object>)h.get("COLUMNS"); values = (java.util.Vector <Object>)h.get("VALUES"); InsertStatement(statementType, tableName, columns, values); } else if (actionType.equals("CREATE_TABLE")) { /* * SQL CREATE TABLE * * CREATE TABLE User(user_oid NUMBER(8) NOT NULL, * userType VARCHAR(80) DEFAULT '-' NOT NULL, * PRIMARY KEY (user_oid)) * * -> DEFAULT / NOT NULL / PRIMARY KEY is not supported * */ columnDefs = (java.util.Vector <Object>)h.get("COLUMN_DEF"); CreateTable(tableName, columnDefs); } else if (actionType.equals("ALTER_ADD")) { /* * SQL ALTER TABLE ADD */ columnDefs = (java.util.Vector <Object>)h.get("COLUMN_DEF"); AlterTableAddCol(tableName, columnDefs); } else if (actionType.equals("ALTER_DROP")) { /* * SQL ALTER TABLE DROP */ columns = (java.util.Vector <Object>)h.get("COLUMNS"); AlterTableDropCol(tableName, columns); } else if (actionType.equals("ALTER_RENAME")) { /* * SQL ALTER TABLE RENAME */ String oldColumnName = (String)h.get("OLD_COLUMN"); String newColumnName = (String)h.get("NEW_COLUMN"); AlterTableRenameCol(tableName, oldColumnName, newColumnName); } else if (actionType.equals("DROP_TABLE")) { /* * SQL DROP TABLE */ DropTable(tableName); } else { java.lang.SystemJ.outJ.println("Unrecognized action " + actionType); } } } catch (java.lang.Throwable e) { if (TinySQLGlobals.EX_DEBUG) { e.printStackTrace(java.lang.SystemJ.outJ); } throw new TinySQLException(e.getMessage()); } return(rs); }
/** * Caches a stored entry that uses the data descriptor. * * <ul> * <li>Reads a stored entry until the signature of a local file * header, central directory header or data descriptor has been * found.</li> * <li>Stores all entry data in lastStoredEntry.</p> * <li>Rewinds the stream to position at the data * descriptor.</li> * <li>reads the data descriptor</li> * </ul> * * <p>After calling this method the entry should know its size, * the entry's data is cached and the stream is positioned at the * next local file or central directory header.</p> */ private void readStoredEntry() //throws IOException { java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); byte[] LFH = ZipLong.LFH_SIG.getBytes(); byte[] CFH = ZipLong.CFH_SIG.getBytes(); byte[] DD = ZipLong.DD_SIG.getBytes(); int off = 0; bool done = false; while (!done) { int r = inJ.read(buf, off, ZipArchiveOutputStream.BUFFER_SIZE - off); if (r <= 0) { // read the whole archive without ever finding a // central directory throw new java.io.IOException("Truncated ZIP file"); } if (r + off < 4) { // buf is too small to check for a signature, loop off += r; continue; } int readTooMuch = 0; for (int i = 0; !done && i < r - 4; i++) { if (buf[i] == LFH[0] && buf[i + 1] == LFH[1]) { if ((buf[i + 2] == LFH[2] && buf[i + 3] == LFH[3]) || (buf[i] == CFH[2] && buf[i + 3] == CFH[3])) { // found a LFH or CFH: readTooMuch = off + r - i - 12 /* dd without signature */; done = true; } else if (buf[i + 2] == DD[2] && buf[i + 3] == DD[3]) { // found DD: readTooMuch = off + r - i; done = true; } if (done) { // * push back bytes read in excess as well as the data // descriptor // * copy the remaining bytes to cache // * read data descriptor ((java.io.PushbackInputStream)inJ).unread(buf, off + r - readTooMuch, readTooMuch); bos.write(buf, 0, i); readDataDescriptor(); } } } if (!done) { // worst case we've read a data descriptor without a // signature (12 bytes) plus the first three bytes of // a LFH or CFH signature // save the last 15 bytes in the buffer, cache // anything in front of that, read on if (off + r > 15) { bos.write(buf, 0, off + r - 15); java.lang.SystemJ.arraycopy(buf, off + r - 15, buf, 0, 15); off = 15; } else { off += r; } } } byte[] b = bos.toByteArray(); lastStoredEntry = new java.io.ByteArrayInputStream(b); }
/** * Closes the current ZIP archive entry and positions the underlying * stream to the beginning of the next entry. All per-entry variables * and data structures are cleared. * <p> * If the compressed size of this entry is included in the entry header, * then any outstanding bytes are simply skipped from the underlying * stream without uncompressing them. This allows an entry to be safely * closed even if the compression method is unsupported. * <p> * In case we don't know the compressed size of this entry or have * already buffered too much data from the underlying stream to support * uncompression, then the uncompression process is completed and the * end position of the stream is adjusted based on the result of that * process. * * @throws IOException if an error occurs */ private void closeEntry() //throws IOException { if (closed) { throw new java.io.IOException("The stream is closed"); } if (current == null) { return; } // Ensure all entry bytes are read if (bytesReadFromStream <= current.getCompressedSize() && !hasDataDescriptor) { long remaining = current.getCompressedSize() - bytesReadFromStream; while (remaining > 0) { long n = inJ.read(buf, 0, (int)java.lang.Math.min(buf.Length, remaining)); if (n < 0) { throw new java.io.EOFException( "Truncated ZIP entry: " + current.getName()); } else { count(n); remaining -= n; } } } else { skip(java.lang.Long.MAX_VALUE); int inB = 0; if (current.getMethod() == ZipArchiveOutputStream.DEFLATED) { inB = inf.getTotalIn(); } else { inB = readBytesOfEntry; } int diff = 0; // Pushback any required bytes if ((diff = bytesReadFromStream - inB) != 0) { ((java.io.PushbackInputStream)inJ).unread( buf, lengthOfLastRead - diff, diff); pushedBackBytes(diff); } } if (lastStoredEntry == null && hasDataDescriptor) { readDataDescriptor(); } inf.reset(); readBytesOfEntry = offsetInBuffer = bytesReadFromStream = lengthOfLastRead = 0; crc.reset(); current = null; lastStoredEntry = null; }
/** * Creates an object using serialization. * * @return the new object */ public Object create() { java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(512); java.io.ByteArrayInputStream bais = null; try { java.io.ObjectOutputStream outJ = new java.io.ObjectOutputStream(baos); outJ.writeObject(iPrototype); bais = new java.io.ByteArrayInputStream(baos.toByteArray()); java.io.ObjectInputStream inJ = new java.io.ObjectInputStream(bais); return inJ.readObject(); } catch (java.lang.ClassNotFoundException ex) { throw new FunctorException(ex); } catch (java.io.IOException ex) { throw new FunctorException(ex); } finally { try { if (bais != null) { bais.close(); } } catch (java.io.IOException ex) { // ignore } try { if (baos != null) { baos.close(); } } catch (java.io.IOException ex) { // ignore } } }