-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #4154: adding a callback for stream consumption
- Loading branch information
Showing
22 changed files
with
234 additions
and
369 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/StreamConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* 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 io.fabric8.kubernetes.client; | ||
|
||
import java.io.OutputStream; | ||
import java.io.PipedInputStream; | ||
import java.io.PipedOutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.Channels; | ||
import java.nio.channels.WritableByteChannel; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
public interface StreamConsumer { | ||
|
||
public static StreamConsumer newStreamConsumer(OutputStream os) { | ||
if (os == null) { | ||
return null; | ||
} | ||
checkForPiped(os); | ||
return newBlockingStreamConsumer(Channels.newChannel(os)); | ||
} | ||
|
||
public static StreamConsumer newBlockingStreamConsumer(WritableByteChannel channel) { | ||
if (channel == null) { | ||
return null; | ||
} | ||
return buffer -> { | ||
int remaining = buffer.remaining(); | ||
if (channel.write(buffer) != remaining) { | ||
throw new KubernetesClientException("Unsucessful blocking write"); | ||
} | ||
return null; | ||
}; | ||
} | ||
|
||
public static void checkForPiped(Object object) { | ||
if (object instanceof PipedOutputStream || object instanceof PipedInputStream) { | ||
throw new KubernetesClientException("Piped streams should not be used"); | ||
} | ||
} | ||
|
||
/** | ||
* A callback for consuming a stream as a series of {@link ByteBuffer}s | ||
* | ||
* @param buffer | ||
* @return a {@link CompletionStage} that is completed when the buffer has been fully consumed, | ||
* or null if it was already consumed | ||
* @throws Exception | ||
*/ | ||
CompletionStage<?> consume(ByteBuffer buffer) throws Exception; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.