|
28 | 28 | */ |
29 | 29 | publicclassConnectionimplementsjava.sql.Connection |
30 | 30 | { |
31 | | -privatePG_Streampg_stream; |
| 31 | +protectedPG_Streampg_stream; |
32 | 32 |
|
33 | 33 | privateStringPG_HOST; |
34 | 34 | privateintPG_PORT; |
@@ -591,256 +591,18 @@ public String getUserName() throws SQLException |
591 | 591 | { |
592 | 592 | returnPG_USER; |
593 | 593 | } |
594 | | -} |
595 | | - |
596 | | -// *********************************************************************** |
597 | | - |
598 | | -// This class handles all the Streamed I/O for a postgresql connection |
599 | | -classPG_Stream |
600 | | -{ |
601 | | -privateSocketconnection; |
602 | | -privateInputStreampg_input; |
603 | | -privateOutputStreampg_output; |
604 | | - |
605 | | -/** |
606 | | - * Constructor: Connect to the PostgreSQL back end and return |
607 | | - * a stream connection. |
608 | | - * |
609 | | - * @param host the hostname to connect to |
610 | | - * @param port the port number that the postmaster is sitting on |
611 | | - * @exception IOException if an IOException occurs below it. |
612 | | - */ |
613 | | -publicPG_Stream(Stringhost,intport)throwsIOException |
614 | | - { |
615 | | -connection =newSocket(host,port); |
616 | | -pg_input =connection.getInputStream(); |
617 | | -pg_output =connection.getOutputStream(); |
618 | | - } |
619 | | - |
620 | | -/** |
621 | | - * Sends a single character to the back end |
622 | | - * |
623 | | - * @param val the character to be sent |
624 | | - * @exception IOException if an I/O error occurs |
625 | | - */ |
626 | | -publicvoidSendChar(intval)throwsIOException |
627 | | - { |
628 | | -pg_output.write(val); |
629 | | - } |
630 | | - |
631 | | -/** |
632 | | - * Sends an integer to the back end |
633 | | - * |
634 | | - * @param val the integer to be sent |
635 | | - * @param siz the length of the integer in bytes (size of structure) |
636 | | - * @exception IOException if an I/O error occurs |
637 | | - */ |
638 | | -publicvoidSendInteger(intval,intsiz)throwsIOException |
639 | | - { |
640 | | -byte[]buf =newbyte[siz]; |
641 | | - |
642 | | -while (siz-- >0) |
643 | | - { |
644 | | -buf[siz] = (byte)(val &0xff); |
645 | | -val >>=8; |
646 | | - } |
647 | | -Send(buf); |
648 | | - } |
649 | | - |
650 | | -/** |
651 | | - * Send an array of bytes to the backend |
652 | | - * |
653 | | - * @param buf The array of bytes to be sent |
654 | | - * @exception IOException if an I/O error occurs |
655 | | - */ |
656 | | -publicvoidSend(bytebuf[])throwsIOException |
657 | | - { |
658 | | -pg_output.write(buf); |
659 | | - } |
660 | | - |
661 | | -/** |
662 | | - * Send an exact array of bytes to the backend - if the length |
663 | | - * has not been reached, send nulls until it has. |
664 | | - * |
665 | | - * @param buf the array of bytes to be sent |
666 | | - * @param siz the number of bytes to be sent |
667 | | - * @exception IOException if an I/O error occurs |
668 | | - */ |
669 | | -publicvoidSend(bytebuf[],intsiz)throwsIOException |
670 | | - { |
671 | | -inti; |
672 | | - |
673 | | -pg_output.write(buf,0, (buf.length <siz ?buf.length :siz)); |
674 | | -if (buf.length <siz) |
675 | | - { |
676 | | -for (i =buf.length ;i <siz ; ++i) |
677 | | - { |
678 | | -pg_output.write(0); |
679 | | - } |
680 | | - } |
681 | | - } |
682 | | - |
683 | | -/** |
684 | | - * Receives a single character from the backend |
685 | | - * |
686 | | - * @return the character received |
687 | | - * @exception SQLException if an I/O Error returns |
688 | | - */ |
689 | | -publicintReceiveChar()throwsSQLException |
690 | | - { |
691 | | -intc =0; |
692 | | - |
693 | | -try |
694 | | - { |
695 | | -c =pg_input.read(); |
696 | | -if (c <0)thrownewIOException("EOF"); |
697 | | - }catch (IOExceptione) { |
698 | | -thrownewSQLException("Error reading from backend: " +e.toString()); |
699 | | - } |
700 | | -returnc; |
701 | | - } |
702 | | - |
703 | | -/** |
704 | | - * Receives an integer from the backend |
705 | | - * |
706 | | - * @param siz length of the integer in bytes |
707 | | - * @return the integer received from the backend |
708 | | - * @exception SQLException if an I/O error occurs |
709 | | - */ |
710 | | -publicintReceiveInteger(intsiz)throwsSQLException |
711 | | - { |
712 | | -intn =0; |
713 | | - |
714 | | -try |
715 | | - { |
716 | | -for (inti =0 ;i <siz ;i++) |
717 | | - { |
718 | | -intb =pg_input.read(); |
719 | | - |
720 | | -if (b <0) |
721 | | -thrownewIOException("EOF"); |
722 | | -n =n | (b >> (8 *i)) ; |
723 | | - } |
724 | | - }catch (IOExceptione) { |
725 | | -thrownewSQLException("Error reading from backend: " +e.toString()); |
726 | | - } |
727 | | -returnn; |
728 | | - } |
729 | | - |
730 | | -/** |
731 | | - * Receives a null-terminated string from the backend. Maximum of |
732 | | - * maxsiz bytes - if we don't see a null, then we assume something |
733 | | - * has gone wrong. |
734 | | - * |
735 | | - * @param maxsiz maximum length of string |
736 | | - * @return string from back end |
737 | | - * @exception SQLException if an I/O error occurs |
738 | | - */ |
739 | | -publicStringReceiveString(intmaxsiz)throwsSQLException |
740 | | - { |
741 | | -byte[]rst =newbyte[maxsiz]; |
742 | | -ints =0; |
743 | | - |
744 | | -try |
745 | | - { |
746 | | -while (s <maxsiz) |
747 | | - { |
748 | | -intc =pg_input.read(); |
749 | | -if (c <0) |
750 | | -thrownewIOException("EOF"); |
751 | | -elseif (c ==0) |
752 | | -break; |
753 | | -else |
754 | | -rst[s++] = (byte)c; |
755 | | - } |
756 | | -if (s >=maxsiz) |
757 | | -thrownewIOException("Too Much Data"); |
758 | | - }catch (IOExceptione) { |
759 | | -thrownewSQLException("Error reading from backend: " +e.toString()); |
760 | | - } |
761 | | -Stringv =newString(rst,0,s); |
762 | | -returnv; |
763 | | - } |
764 | 594 |
|
765 | 595 | /** |
766 | | - * Read a tuple from the back end. A tuple is a two dimensional |
767 | | - * array of bytes |
768 | | - * |
769 | | - * @param nf the number of fields expected |
770 | | - * @param bin true if the tuple is a binary tuple |
771 | | - * @return null if the current response has no more tuples, otherwise |
772 | | - *an array of strings |
773 | | - * @exception SQLException if a data I/O error occurs |
774 | | - */ |
775 | | -publicbyte[][]ReceiveTuple(intnf,booleanbin)throwsSQLException |
776 | | - { |
777 | | -inti,bim = (nf +7)/8; |
778 | | -byte[]bitmask =Receive(bim); |
779 | | -byte[][]answer =newbyte[nf][0]; |
780 | | - |
781 | | -intwhichbit =0x80; |
782 | | -intwhichbyte =0; |
783 | | - |
784 | | -for (i =0 ;i <nf ; ++i) |
785 | | - { |
786 | | -booleanisNull = ((bitmask[whichbyte] &whichbit) ==0); |
787 | | -whichbit >>=1; |
788 | | -if (whichbit ==0) |
789 | | - { |
790 | | - ++whichbyte; |
791 | | -whichbit =0x80; |
792 | | - } |
793 | | -if (isNull) |
794 | | -answer[i] =null; |
795 | | -else |
796 | | - { |
797 | | -intlen =ReceiveInteger(4); |
798 | | -if (!bin) |
799 | | -len -=4; |
800 | | -if (len <0) |
801 | | -len =0; |
802 | | -answer[i] =Receive(len); |
803 | | - } |
804 | | - } |
805 | | -returnanswer; |
806 | | - } |
807 | | - |
808 | | -/** |
809 | | - * Reads in a given number of bytes from the backend |
810 | | - * |
811 | | - * @param siz number of bytes to read |
812 | | - * @return array of bytes received |
813 | | - * @exception SQLException if a data I/O error occurs |
814 | | - */ |
815 | | -privatebyte[]Receive(intsiz)throwsSQLException |
816 | | - { |
817 | | -byte[]answer =newbyte[siz]; |
818 | | -ints =0; |
819 | | - |
820 | | -try |
821 | | - { |
822 | | -while (s <siz) |
823 | | - { |
824 | | -intw =pg_input.read(answer,s,siz -s); |
825 | | -if (w <0) |
826 | | -thrownewIOException("EOF"); |
827 | | -s +=w; |
828 | | - } |
829 | | - }catch (IOExceptione) { |
830 | | -thrownewSQLException("Error reading from backend: " +e.toString()); |
831 | | - } |
832 | | -returnanswer; |
833 | | - } |
834 | | - |
835 | | -/** |
836 | | - * Closes the connection |
| 596 | + * This method is not part of the Connection interface. Its is an extension |
| 597 | + * that allows access to the PostgreSQL Large Object API |
837 | 598 | * |
838 | | - * @exception IOException if a IO Error occurs |
| 599 | + * @return PGlobj class that implements the API |
839 | 600 | */ |
840 | | -publicvoidclose()throwsIOException |
| 601 | +publicPGlobjgetLargeObjectAPI()throwsSQLException |
841 | 602 | { |
842 | | -pg_output.close(); |
843 | | -pg_input.close(); |
844 | | -connection.close(); |
| 603 | +returnnewPGlobj(this); |
845 | 604 | } |
846 | 605 | } |
| 606 | + |
| 607 | +// *********************************************************************** |
| 608 | + |