blob: 6180572b24e14033524accfacf3126d6870ba3f2 [file] [log] [blame]
janakr23e152c2017-09-14 02:58:31 +02001// Copyright 2017 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Googlerab5f2332017-12-11 15:32:29 -080015package com.google.devtools.build.lib.vfs;
janakr23e152c2017-09-14 02:58:31 +020016
tomlua155b532017-11-08 20:12:47 +010017import com.google.common.base.Preconditions;
michajloe45c0552018-02-11 09:23:55 -080018import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
Googlerab5f2332017-12-11 15:32:29 -080019import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
michajloe45c0552018-02-11 09:23:55 -080020import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
Googlerab5f2332017-12-11 15:32:29 -080021import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
tomlua729b9b2018-02-08 15:32:00 -080022import com.google.devtools.build.lib.skyframe.serialization.strings.StringCodecs;
janakr23e152c2017-09-14 02:58:31 +020023import com.google.protobuf.CodedInputStream;
24import com.google.protobuf.CodedOutputStream;
25import java.io.IOException;
26
27/** Custom serialization for {@link Path}s. */
28public class PathCodec implements ObjectCodec<Path> {
29
tomlua729b9b2018-02-08 15:32:00 -080030 private final ObjectCodec<String> stringCodec = StringCodecs.asciiOptimized();
janakr23e152c2017-09-14 02:58:31 +020031 private final FileSystem fileSystem;
janakr23e152c2017-09-14 02:58:31 +020032
33 /** Create an instance for serializing and deserializing {@link Path}s on {@code fileSystem}. */
Googlerab5f2332017-12-11 15:32:29 -080034 public PathCodec(FileSystem fileSystem) {
janakr23e152c2017-09-14 02:58:31 +020035 this.fileSystem = fileSystem;
janakr23e152c2017-09-14 02:58:31 +020036 }
37
38 @Override
39 public Class<Path> getEncodedClass() {
40 return Path.class;
41 }
42
43 @Override
michajloe45c0552018-02-11 09:23:55 -080044 public void serialize(SerializationContext context, Path path, CodedOutputStream codedOut)
michajlob45d5f52017-10-07 19:44:43 +020045 throws IOException, SerializationException {
janakr23e152c2017-09-14 02:58:31 +020046 Preconditions.checkState(
47 path.getFileSystem() == fileSystem,
tomlua729b9b2018-02-08 15:32:00 -080048 "Path's FileSystem (%s) did not match the configured FileSystem (%s) for path (%s)",
janakr23e152c2017-09-14 02:58:31 +020049 path.getFileSystem(),
tomlua729b9b2018-02-08 15:32:00 -080050 fileSystem,
51 path);
michajloe45c0552018-02-11 09:23:55 -080052 stringCodec.serialize(context, path.getPathString(), codedOut);
janakr23e152c2017-09-14 02:58:31 +020053 }
54
55 @Override
michajloe45c0552018-02-11 09:23:55 -080056 public Path deserialize(DeserializationContext context, CodedInputStream codedIn)
57 throws IOException, SerializationException {
58 return fileSystem.getPath(stringCodec.deserialize(context, codedIn));
janakr23e152c2017-09-14 02:58:31 +020059 }
60}