1
+ /*
2
+ * Copyright 2017-2020 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
1
17
package io .r2dbc .postgresql .codec ;
2
18
3
19
import io .netty .buffer .ByteBuf ;
@@ -34,12 +50,18 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
34
50
Assert .requireNonNull (type , "type must not be null" );
35
51
Assert .requireNonNull (format , "format must not be null" );
36
52
37
- String decodedAsString = ByteBufUtils .decode (buffer );
38
- String parenRemovedVal = decodedAsString .replaceAll ("[()]" , "" );
39
- String [] coordinatesAsString = parenRemovedVal .split ("," );
40
- double x = Double .parseDouble (coordinatesAsString [0 ]);
41
- double y = Double .parseDouble (coordinatesAsString [1 ]);
42
- return new Point (x , y );
53
+ if (format == FORMAT_BINARY ) {
54
+ double x = buffer .readDouble ();
55
+ double y = buffer .readDouble ();
56
+ return new Point (x , y );
57
+ } else {
58
+ String decodedAsString = ByteBufUtils .decode (buffer );
59
+ String parenRemovedVal = decodedAsString .replaceAll ("[()]" , "" );
60
+ String [] coordinatesAsString = parenRemovedVal .split ("," );
61
+ double x = Double .parseDouble (coordinatesAsString [0 ]);
62
+ double y = Double .parseDouble (coordinatesAsString [1 ]);
63
+ return new Point (x , y );
64
+ }
43
65
}
44
66
45
67
/**
@@ -49,12 +71,16 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
49
71
@ Override
50
72
Parameter doEncode (Point value ) {
51
73
Assert .requireNonNull (value , "value must not be null" );
52
- return create (POINT , FORMAT_BINARY , () -> ByteBufUtils . encode ( this .byteBufAllocator , value .toString ()));
74
+ return create (POINT , FORMAT_BINARY , () -> this .byteBufAllocator . buffer ( lengthInBytes ()). writeDouble ( value .getX ()). writeDouble ( value . getY ()));
53
75
}
54
76
55
77
@ Override
56
78
public Parameter encodeNull () {
57
79
return createNull (POINT , FORMAT_BINARY );
58
80
}
59
81
82
+ public int lengthInBytes () {
83
+ return 16 ;
84
+ }
85
+
60
86
}
0 commit comments