1616
1717import com .google .api .core .InternalApi ;
1818import com .google .cloud .ByteArray ;
19+ import com .google .cloud .spanner .ProtobufResultSet ;
1920import com .google .cloud .spanner .ResultSet ;
2021import com .google .cloud .spanner .SpannerExceptionFactory ;
2122import com .google .cloud .spanner .Statement ;
@@ -108,7 +109,7 @@ public static byte[] convertToPG(
108109 DataFormat format ) {
109110 int bufferSize = sessionState .getBinaryConversionBufferSize ();
110111 try {
111- String base64 = resultSet . getValue ( position ). getAsString ( );
112+ String base64 = getBase64 ( resultSet , position );
112113 switch (format ) {
113114 case SPANNER :
114115 case POSTGRESQL_BINARY :
@@ -128,7 +129,7 @@ public static byte[] convertToPG(
128129 }
129130 return null ;
130131 case POSTGRESQL_TEXT :
131- return bytesToHex (resultSet . getBytes ( position ). toByteArray ( ));
132+ return bytesToHex (Base64 . getDecoder (). decode ( base64 ));
132133 default :
133134 throw new IllegalArgumentException ("unknown data format: " + format );
134135 }
@@ -137,6 +138,14 @@ public static byte[] convertToPG(
137138 }
138139 }
139140
141+ static String getBase64 (ResultSet resultSet , int column ) {
142+ if (resultSet instanceof ProtobufResultSet
143+ && ((ProtobufResultSet ) resultSet ).canGetProtobufValue (column )) {
144+ return ((ProtobufResultSet ) resultSet ).getProtobufValue (column ).getStringValue ();
145+ }
146+ return resultSet .getValue (column ).getAsString ();
147+ }
148+
140149 static int copy (int length , InputStream from , DataOutputStream to , int bufferSize )
141150 throws IOException {
142151 byte [] buf = new byte [Math .min (length , bufferSize )];
0 commit comments