/platform/platform-api/src/com/intellij/openapi/vfs/newvfs/FileAttribute.java

https://bitbucket.org/nbargnesi/idea · Java · 123 lines · 86 code · 15 blank · 22 comment · 6 complexity · a3fea059d57f237672bf304213032de9 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  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. * http://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. /*
  17. * @author max
  18. */
  19. package com.intellij.openapi.vfs.newvfs;
  20. import com.intellij.openapi.util.io.FileUtil;
  21. import com.intellij.openapi.vfs.VirtualFile;
  22. import com.intellij.util.io.DataInputOutputUtil;
  23. import org.jetbrains.annotations.NonNls;
  24. import org.jetbrains.annotations.NotNull;
  25. import org.jetbrains.annotations.Nullable;
  26. import java.io.DataInputStream;
  27. import java.io.DataOutputStream;
  28. import java.io.IOException;
  29. import java.util.HashSet;
  30. import java.util.Set;
  31. public class FileAttribute {
  32. private static final Set<String> ourRegisteredIds = new HashSet<String>();
  33. private final String myId;
  34. private final int myVersion;
  35. private final boolean myFixedSize;
  36. /**
  37. * @deprecated
  38. * @see #FileAttribute(String, int, boolean)
  39. */
  40. public FileAttribute(@NonNls final String id, int version) {
  41. this(id, version, false);
  42. }
  43. public FileAttribute(@NonNls final String id, int version, boolean fixedSize) {
  44. myId = id;
  45. myVersion = version;
  46. myFixedSize = fixedSize;
  47. boolean added = ourRegisteredIds.add(id);
  48. assert added : "Attribute id='" + id+ "' is not unique";
  49. }
  50. @Nullable
  51. public DataInputStream readAttribute(VirtualFile file) {
  52. DataInputStream stream = ManagingFS.getInstance().readAttribute(file, this);
  53. if (stream != null) {
  54. try {
  55. int actualVersion = DataInputOutputUtil.readINT(stream);
  56. if (actualVersion != myVersion) {
  57. stream.close();
  58. return null;
  59. }
  60. }
  61. catch (IOException e) {
  62. return null;
  63. }
  64. }
  65. return stream;
  66. }
  67. public DataOutputStream writeAttribute(VirtualFile file) {
  68. final DataOutputStream stream = ManagingFS.getInstance().writeAttribute(file, this);
  69. try {
  70. DataInputOutputUtil.writeINT(stream, myVersion);
  71. }
  72. catch (IOException e) {
  73. throw new RuntimeException(e);
  74. }
  75. return stream;
  76. }
  77. @Nullable
  78. public byte[] readAttributeBytes(VirtualFile file) throws IOException {
  79. final DataInputStream stream = readAttribute(file);
  80. if (stream == null) return null;
  81. try {
  82. int len = stream.readInt();
  83. return FileUtil.loadBytes(stream, len);
  84. }
  85. finally {
  86. stream.close();
  87. }
  88. }
  89. public void writeAttributeBytes(VirtualFile file, @NotNull byte[] bytes) throws IOException {
  90. writeAttributeBytes(file, bytes, 0, bytes.length);
  91. }
  92. public void writeAttributeBytes(VirtualFile file, byte[] bytes, int offset, int len) throws IOException {
  93. final DataOutputStream stream = writeAttribute(file);
  94. try {
  95. stream.writeInt(len);
  96. stream.write(bytes, offset, len);
  97. }
  98. finally {
  99. stream.close();
  100. }
  101. }
  102. public String getId() {
  103. return myId;
  104. }
  105. public boolean isFixedSize() {
  106. return myFixedSize;
  107. }
  108. }