001 /*
002 * Java GPX Library (jpx-3.1.0).
003 * Copyright (c) 2016-2023 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.jpx;
021
022 import java.io.Externalizable;
023 import java.io.IOException;
024 import java.io.ObjectInput;
025 import java.io.ObjectOutput;
026 import java.io.Serial;
027 import java.io.StreamCorruptedException;
028
029 /**
030 * The shared serialization delegate for this package.
031 *
032 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
033 * @version 2.0
034 * @since 1.2
035 */
036 final class SerialProxy implements Externalizable {
037
038 @Serial
039 private static final long serialVersionUID = 1L;
040
041 static final byte BOUNDS = 1;
042 static final byte COPYRIGHT = 2;
043 static final byte DEGREES = 3;
044 static final byte DGPS_STATION = 4;
045 static final byte EMAIL = 5;
046 static final byte GPX_TYPE = 6;
047 static final byte LATITUDE = 7;
048 static final byte LENGTH = 8;
049 static final byte LINK = 9;
050 static final byte LONGITUDE = 10;
051 static final byte METADATA = 11;
052 static final byte PERSON = 12;
053 static final byte ROUTE = 13;
054 static final byte SPEED = 14;
055 static final byte TRACK = 15;
056 static final byte TRACK_SEGMENT = 16;
057 static final byte UINT = 17;
058 static final byte WAY_POINT = 18;
059
060 /**
061 * The type being serialized.
062 */
063 private byte _type;
064
065 /**
066 * The object being serialized.
067 */
068 private Object _object;
069
070 /**
071 * Constructor for deserialization.
072 */
073 public SerialProxy() {
074 }
075
076 /**
077 * Creates an instance for serialization.
078 *
079 * @param type the type
080 * @param object the object
081 */
082 SerialProxy(final byte type, final Object object) {
083 _type = type;
084 _object = object;
085 }
086
087 @Override
088 public void writeExternal(final ObjectOutput out) throws IOException {
089 out.writeByte(_type);
090 switch (_type) {
091 case BOUNDS -> ((Bounds)_object).write(out);
092 case COPYRIGHT -> ((Copyright)_object).write(out);
093 case DEGREES -> ((Degrees)_object).write(out);
094 case DGPS_STATION -> ((DGPSStation)_object).write(out);
095 case EMAIL -> ((Email)_object).write(out);
096 case GPX_TYPE -> ((GPX)_object).write(out);
097 case LATITUDE -> ((Latitude)_object).write(out);
098 case LENGTH -> ((Length)_object).write(out);
099 case LINK -> ((Link)_object).write(out);
100 case LONGITUDE -> ((Longitude)_object).write(out);
101 case METADATA -> ((Metadata)_object).write(out);
102 case PERSON -> ((Person)_object).write(out);
103 case ROUTE -> ((Route)_object).write(out);
104 case SPEED -> ((Speed)_object).write(out);
105 case TRACK -> ((Track)_object).write(out);
106 case TRACK_SEGMENT -> ((TrackSegment)_object).write(out);
107 case UINT -> ((UInt)_object).write(out);
108 case WAY_POINT -> ((WayPoint)_object).write(out);
109 default -> throw new StreamCorruptedException(
110 "Unknown serialized type: " + _type
111 );
112 }
113 }
114
115 @Override
116 public void readExternal(final ObjectInput in) throws IOException {
117 _type = in.readByte();
118 switch (_type) {
119 case BOUNDS -> _object = Bounds.read(in);
120 case COPYRIGHT -> _object = Copyright.read(in);
121 case DEGREES -> _object = Degrees.read(in);
122 case DGPS_STATION -> _object = DGPSStation.read(in);
123 case EMAIL -> _object = Email.read(in);
124 case GPX_TYPE -> _object = GPX.read(in);
125 case LATITUDE -> _object = Latitude.read(in);
126 case LENGTH -> _object = Length.read(in);
127 case LINK -> _object = Link.read(in);
128 case LONGITUDE -> _object = Longitude.read(in);
129 case METADATA -> _object = Metadata.read(in);
130 case PERSON -> _object = Person.read(in);
131 case ROUTE -> _object = Route.read(in);
132 case SPEED -> _object = Speed.read(in);
133 case TRACK -> _object = Track.read(in);
134 case TRACK_SEGMENT -> _object = TrackSegment.read(in);
135 case UINT -> _object = UInt.read(in);
136 case WAY_POINT -> _object = WayPoint.read(in);
137 default -> throw new StreamCorruptedException(
138 "Unknown serialized type: " + _type
139 );
140 }
141 }
142
143 @Serial
144 private Object readResolve() {
145 return _object;
146 }
147
148 }
|