blob: 383ad8054c1c1c7e327eb22dbd79515bbb01caee [file] [log] [blame]
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.vfs.impl.jar;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.io.BufferExposingByteArrayInputStream;
import com.intellij.openapi.util.io.FileAttributes;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileSystem;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @author yole
*/
public class CoreJarVirtualFile extends VirtualFile {
private final CoreJarHandler myHandler;
private final String myName;
private final FileAttributes myEntry;
private final VirtualFile myParent;
private final List<VirtualFile> myChildren = new ArrayList<VirtualFile>();
public CoreJarVirtualFile(@NotNull CoreJarHandler handler, @NotNull String name, @NotNull FileAttributes entry, @Nullable CoreJarVirtualFile parent) {
myHandler = handler;
myName = name;
myEntry = entry;
myParent = parent;
if (parent != null) {
parent.myChildren.add(this);
}
}
/** @deprecated to be removed in IDEA 15 */
@SuppressWarnings({"deprecation", "UnusedDeclaration"})
public CoreJarVirtualFile(@NotNull CoreJarHandler handler, @NotNull JarHandlerBase.EntryInfo e, @Nullable CoreJarVirtualFile parent) {
this(handler, e.shortName, new FileAttributes(e.isDirectory, false, false, false, e.length, e.timestamp, false), parent);
}
@NotNull
@Override
public String getName() {
return myName;
}
@NotNull
@Override
public VirtualFileSystem getFileSystem() {
return myHandler.getFileSystem();
}
@Override
@NotNull
public String getPath() {
if (myParent == null) return myHandler.getFile().getPath() + "!/";
String parentPath = myParent.getPath();
StringBuilder answer = new StringBuilder(parentPath.length() + 1 + myName.length());
answer.append(parentPath);
if (answer.charAt(answer.length() - 1) != '/') {
answer.append('/');
}
answer.append(myName);
return answer.toString();
}
@Override
public boolean isWritable() {
return false;
}
@Override
public boolean isDirectory() {
return myEntry.isDirectory();
}
@Override
public boolean isValid() {
return true;
}
@Override
public VirtualFile getParent() {
return myParent;
}
@Override
public VirtualFile[] getChildren() {
return myChildren.toArray(new VirtualFile[myChildren.size()]);
}
@NotNull
@Override
public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
throw new UnsupportedOperationException("JarFileSystem is read-only");
}
@NotNull
@Override
public byte[] contentsToByteArray() throws IOException {
Couple<String> pair = ((CoreJarFileSystem)getFileSystem()).splitPath(getPath());
return myHandler.contentsToByteArray(pair.second);
}
@Override
public long getTimeStamp() {
return myEntry.lastModified;
}
@Override
public long getLength() {
return myEntry.length;
}
@Override
public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) { }
@Override
public InputStream getInputStream() throws IOException {
return new BufferExposingByteArrayInputStream(contentsToByteArray());
}
@Override
public long getModificationStamp() {
return 0;
}
}