summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/OldStreamProviderAdapter.java
blob: 574823767d9b8e026aeaaa76a6123836117c5827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.intellij.openapi.components.impl.stores;

import com.intellij.openapi.components.RoamingType;
import com.intellij.openapi.options.CurrentUserHolder;
import com.intellij.openapi.util.io.BufferExposingByteArrayInputStream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

final class OldStreamProviderAdapter extends StreamProvider implements CurrentUserHolder {
  final com.intellij.openapi.options.StreamProvider myProvider;
  private final RoamingType myRoamingType;

  public OldStreamProviderAdapter(com.intellij.openapi.options.StreamProvider provider, @NotNull RoamingType roamingType) {
    myProvider = provider;
    myRoamingType = roamingType;
  }

  @Override
  public boolean isEnabled() {
    return myProvider.isEnabled();
  }

  @Override
  public boolean isApplicable(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
    return myRoamingType == roamingType && !(roamingType == RoamingType.PER_USER && StorageUtil.isProjectOrModuleFile(fileSpec));
  }

  @Override
  public boolean saveContent(@NotNull String fileSpec, @NotNull byte[] content, int size, @NotNull RoamingType roamingType, boolean async) throws IOException {
    myProvider.saveContent(fileSpec, new BufferExposingByteArrayInputStream(content, size), size, roamingType, async);
    return false;
  }

  @Nullable
  @Override
  public InputStream loadContent(@NotNull String fileSpec, @NotNull RoamingType roamingType) throws IOException {
    return myRoamingType == roamingType ? myProvider.loadContent(fileSpec, roamingType) : null;
  }

  @NotNull
  @Override
  public List<String> listSubFiles(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
    if (myRoamingType == roamingType) {
      return Arrays.asList(myProvider.listSubFiles(fileSpec, roamingType));
    }
    else {
      return Collections.emptyList();
    }
  }

  @Override
  public void deleteFile(@NotNull String fileSpec, @NotNull RoamingType roamingType) {
    if (myRoamingType == roamingType) {
      myProvider.deleteFile(fileSpec, roamingType);
    }
  }

  @Override
  public String getCurrentUserName() {
    return myProvider instanceof CurrentUserHolder ? ((CurrentUserHolder)myProvider).getCurrentUserName() : null;
  }
}