Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[SPARK-47172] Moving ByteBufferWriteableChannel to its own class
  • Loading branch information
sweisdb committed Jun 7, 2024
commit 4ceeee3a3ea58674976d68afaa93e69cfb7694b5
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
import io.netty.channel.*;
import io.netty.util.ReferenceCounted;
import org.apache.spark.network.util.AbstractFileRegion;
import org.apache.spark.network.util.ByteBufferWriteableChannel;

import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.WritableByteChannel;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
Expand Down Expand Up @@ -403,39 +403,4 @@ public void channelRead(ChannelHandlerContext ctx, Object ciphertextMessage)
}
}
}

static class ByteBufferWriteableChannel implements WritableByteChannel {
private final ByteBuffer destination;
private boolean open;

ByteBufferWriteableChannel(ByteBuffer destination) {
this.destination = destination;
this.open = true;
}
@Override
public int write(ByteBuffer src) throws IOException {
if (!isOpen()) {
throw new ClosedChannelException();
}
int bytesToWrite = Math.min(src.remaining(), destination.remaining());
// Destination buffer is full
if (bytesToWrite == 0) {
return 0;
}
ByteBuffer temp = src.slice().limit(bytesToWrite);
destination.put(temp);
src.position(src.position() + bytesToWrite);
return bytesToWrite;
}

@Override
public boolean isOpen() {
return open;
}

@Override
public void close() {
open = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.spark.network.util;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.WritableByteChannel;

public class ByteBufferWriteableChannel implements WritableByteChannel {
private final ByteBuffer destination;
private boolean open;

public ByteBufferWriteableChannel(ByteBuffer destination) {
this.destination = destination;
this.open = true;
}

@Override
public int write(ByteBuffer src) throws IOException {
if (!isOpen()) {
throw new ClosedChannelException();
}
int bytesToWrite = Math.min(src.remaining(), destination.remaining());
// Destination buffer is full
if (bytesToWrite == 0) {
return 0;
}
ByteBuffer temp = src.slice().limit(bytesToWrite);
destination.put(temp);
src.position(src.position() + bytesToWrite);
return bytesToWrite;
}

@Override
public boolean isOpen() {
return open;
}

@Override
public void close() {
open = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.FileRegion;
import org.apache.spark.network.crypto.GcmTransportCipher.ByteBufferWriteableChannel;
import org.apache.spark.network.util.*;

import static org.junit.jupiter.api.Assertions.*;
Expand Down