gpt4 book ai didi

java - 使用 kubernetes-client exec() 使用 PipeInputStream 破坏管道

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:46 25 4
gpt4 key购买 nike

我正在使用kubernetes-client尝试从 Pod 复制目录,但我对来自标准输出的输入流做了一些错误。当它尝试 read() 时,我收到 java.io.IOException: Pipebroken 异常。我很确定根本没有数据流。我有点想知道我是否需要在单独的线程或其他线程上读取InputStream?

流的创建方式如下:

public InputStream copyFiles(String containerId,
String folderName) {

ExecWatch exec = client.pods().withName(containerId).redirectingOutput().exec("tar -C " + folderName + " -c");

// We need to wrap the InputStream so that when the stdout is closed, then the underlying ExecWatch is closed
// also. This will cleanup any Websockets connections.
ChainedCloseInputStreamWrapper inputStreamWrapper = new ChainedCloseInputStreamWrapper(exec.getOutput(), exec);

return inputStreamWrapper;
}

并且InputStream是在这个函数中处理的

void copyVideos(final String containerId) {
TarArchiveInputStream tarStream = new TarArchiveInputStream(containerClient.copyFiles(containerId, "/videos/"));
TarArchiveEntry entry;
boolean videoWasCopied = false;
try {
while ((entry = tarStream.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
String fileExtension = entry.getName().substring(entry.getName().lastIndexOf('.'));
testInformation.setFileExtension(fileExtension);
File videoFile = new File(testInformation.getVideoFolderPath(), testInformation.getFileName());
File parent = videoFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
OutputStream outputStream = new FileOutputStream(videoFile);
IOUtils.copy(tarStream, outputStream);
outputStream.close();
videoWasCopied = true;
LOGGER.log(Level.INFO, "{0} Video file copied to: {1}/{2}", new Object[]{getId(),
testInformation.getVideoFolderPath(), testInformation.getFileName()});
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, getId() + " Error while copying the video", e);
ga.trackException(e);
} finally {
if (!videoWasCopied) {
testInformation.setVideoRecorded(false);
}
}

}

InputStream Wrapper 类只是在 InputStream 关​​闭后在最后关闭 ExecWatch ,它看起来像这样:

private static class ChainedCloseInputStreamWrapper extends InputStream {

private InputStream delegate;
private Closeable resourceToClose;

public ChainedCloseInputStreamWrapper(InputStream delegate, Closeable resourceToClose) {
this.delegate = delegate;
this.resourceToClose = resourceToClose;
}

@Override
public int read() throws IOException {
return delegate.read();
}

public int available() throws IOException {
return delegate.available();
}

public void close() throws IOException {
logger.info("Shutdown called!");
delegate.close();

// Close our dependent resource
resourceToClose.close();
}

public boolean equals(Object o) {
return delegate.equals(o);
}

public int hashCode() {
return delegate.hashCode();
}

public int read(byte[] array) throws IOException {
return delegate.read(array);
}

public int read(byte[] array,
int n,
int n2) throws IOException {
return delegate.read(array, n, n2);
}

public long skip(long n) throws IOException {
return delegate.skip(n);
}

public void mark(int n) {
delegate.mark(n);
}

public void reset() throws IOException {
delegate.reset();
}

public boolean markSupported() {
return delegate.markSupported();
}

public String toString() {
return delegate.toString();
}

}

最佳答案

原来我的 tar 命令是错误的,所以它导致了失败,并且标准输出 PipeInputStream 死锁了。我设法找到解决僵局的方法。但失败的主要原因是我忘记告诉 tar 实际做一些事情!我至少需要一个“。”包括当前目录。

关于java - 使用 kubernetes-client exec() 使用 PipeInputStream 破坏管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com