Skip to content

Commit

Permalink
Simplify OutputStream constructor in Java - See zeroc-ice#2595
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone committed Oct 1, 2024
1 parent b981f90 commit 6d845de
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public BatchRequestQueue(Instance instance, boolean datagram) {
_interceptor = initData.batchRequestInterceptor;
_batchStreamInUse = false;
_batchRequestNum = 0;
_batchStream = new OutputStream(instance, Protocol.currentProtocolEncoding);
_batchStream =
new OutputStream(
Protocol.currentProtocolEncoding,
instance.defaultsAndOverrides().defaultFormat);
_batchStream.writeBlob(Protocol.requestBatchHdr);
_batchMarker = _batchStream.size();
_batchCompress = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ private void dispatchAll(OutputStream os, int requestId, int requestCount) {
} else if (requestCount > 0) {
fillInValue(os, Protocol.headerSize, requestCount);
}
TraceUtil.traceSend(os, _logger, _traceLevels);
TraceUtil.traceSend(os, _reference.getInstance(), _logger, _traceLevels);
}

var is = new InputStream(os.instance(), os.getEncoding(), os.getBuffer(), false);
var is = new InputStream(_reference.getInstance(), os.getEncoding(), os.getBuffer(), false);

if (requestCount > 0) {
is.pos(Protocol.requestBatchHdr.length);
Expand Down Expand Up @@ -248,7 +248,7 @@ private void sendResponse(OutgoingResponse response, int requestId, boolean amd)
// Adopt the OutputStream's buffer.
var inputStream =
new InputStream(
outputStream.instance(),
_reference.getInstance(),
outputStream.getEncoding(),
outputStream.getBuffer(),
true); // adopt: true
Expand Down
24 changes: 8 additions & 16 deletions java/src/Ice/src/main/java/com/zeroc/Ice/ConnectionI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ public synchronized void sendHeartbeat() {
// As a result of this optimization, the only possible heartbeat in _sendStreams is the
// first _sendStreams message.
if (_sendStreams.isEmpty()) {
OutputStream os = new OutputStream(_instance, Protocol.currentProtocolEncoding);
OutputStream os = new OutputStream(Protocol.currentProtocolEncoding);
os.writeBlob(Protocol.magic);
Protocol.currentProtocol.ice_writeMembers(os);
Protocol.currentProtocolEncoding.ice_writeMembers(os);
Expand Down Expand Up @@ -1280,7 +1280,7 @@ public ConnectionI(
_readStream = new InputStream(instance, Protocol.currentProtocolEncoding);
_readHeader = false;
_readStreamPos = -1;
_writeStream = new OutputStream(instance, Protocol.currentProtocolEncoding);
_writeStream = new OutputStream(); // temporary stream
_writeStreamPos = -1;
_upcallCount = 0;
_state = StateNotInitialized;
Expand Down Expand Up @@ -1586,7 +1586,7 @@ private void initiateShutdown() {
//
// Before we shut down, we send a close connection message.
//
OutputStream os = new OutputStream(_instance, Protocol.currentProtocolEncoding);
OutputStream os = new OutputStream(Protocol.currentProtocolEncoding);
os.writeBlob(Protocol.magic);
Protocol.currentProtocol.ice_writeMembers(os);
Protocol.currentProtocolEncoding.ice_writeMembers(os);
Expand Down Expand Up @@ -1670,7 +1670,7 @@ private boolean validate(int operation) {
// (always zero for
// validate connection).
_writeStream.writeInt(Protocol.headerSize); // Message size.
TraceUtil.traceSend(_writeStream, _logger, _traceLevels);
TraceUtil.traceSend(_writeStream, _instance, _logger, _traceLevels);
_writeStream.prepareWrite();
}

Expand Down Expand Up @@ -1861,7 +1861,7 @@ private int sendNextMessage(java.util.List<OutgoingMessage> callbacks) {
message.stream.prepareWrite();
message.prepared = true;

TraceUtil.traceSend(stream, _logger, _traceLevels);
TraceUtil.traceSend(stream, _instance, _logger, _traceLevels);

//
// Send the message.
Expand Down Expand Up @@ -1927,7 +1927,7 @@ private int sendMessage(OutgoingMessage message) {
message.stream.prepareWrite();
message.prepared = true;
int op;
TraceUtil.traceSend(stream, _logger, _traceLevels);
TraceUtil.traceSend(stream, _instance, _logger, _traceLevels);

// Send the message without blocking.
if (_observer != null) {
Expand Down Expand Up @@ -1981,9 +1981,7 @@ private OutputStream doCompress(OutputStream uncompressed, boolean compress) {
BZip2.compress(
uncompressed.getBuffer(), Protocol.headerSize, _compressionLevel);
if (cbuf != null) {
OutputStream cstream =
new OutputStream(
uncompressed.instance(), uncompressed.getEncoding(), cbuf, true);
var cstream = new OutputStream(new Buffer(cbuf, true), uncompressed.getEncoding());

//
// Set compression status.
Expand Down Expand Up @@ -2250,16 +2248,11 @@ private void dispatchAll(
}
} else {
// Received request on a connection without an object adapter.
// TODO: fix #2595
System.err.println(
"*************** Received request on a connection without an object adapter.");
/*
sendResponse(
request.current.createOutgoingResponse(
new com.zeroc.Ice.ObjectNotExistException()),
isTwoWay,
(byte) 0);
*/
}
--requestCount;
}
Expand Down Expand Up @@ -2611,8 +2604,7 @@ public void canceled() {

public void adopt() {
if (adopt) {
OutputStream stream =
new OutputStream(this.stream.instance(), Protocol.currentProtocolEncoding);
var stream = new OutputStream(Protocol.currentProtocolEncoding);
stream.swap(this.stream);
this.stream = stream;
adopt = false;
Expand Down
12 changes: 10 additions & 2 deletions java/src/Ice/src/main/java/com/zeroc/Ice/Current.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ private OutgoingResponse createOutgoingResponseCore(Throwable exc) {
OutputStream ostr;

if (requestId != 0) {
ostr = new OutputStream(adapter.getCommunicator(), Protocol.currentProtocolEncoding);
// The default class format doesn't matter since we always encode user exceptions in
// Sliced format.
ostr = new OutputStream(Protocol.currentProtocolEncoding);
ostr.writeBlob(Protocol.replyHdr);
ostr.writeInt(requestId);
} else {
Expand Down Expand Up @@ -325,7 +327,13 @@ private OutputStream startReplyStream(ReplyStatus replyStatus) {
return new OutputStream();
} else {
var ostr =
new OutputStream(adapter.getCommunicator(), Protocol.currentProtocolEncoding);
new OutputStream(
Protocol.currentProtocolEncoding,
this.adapter
.getCommunicator()
.getInstance()
.defaultsAndOverrides()
.defaultFormat);
ostr.writeBlob(Protocol.replyHdr);
ostr.writeInt(requestId);
ostr.writeByte(replyStatus.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public synchronized EndpointI create(String str, boolean oaEndpoint) {
// and ask the factory to read the endpoint data from that stream to create
// the actual endpoint.
//
var os = new OutputStream(_instance, Protocol.currentProtocolEncoding, false);
var os = new OutputStream(Protocol.currentProtocolEncoding);
os.writeShort(ue.type());
ue.streamWrite(os);
var is =
Expand Down
5 changes: 4 additions & 1 deletion java/src/Ice/src/main/java/com/zeroc/Ice/OutgoingAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public OutgoingAsync(
}
}
if (_os == null) {
_os = new OutputStream(_instance, Protocol.currentProtocolEncoding);
_os =
new OutputStream(
Protocol.currentProtocolEncoding,
_instance.defaultsAndOverrides().defaultFormat);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public T waitForResponse() {

protected OutgoingAsyncBase(Communicator com, Instance instance, String op) {
super(com, instance, op);
_os = new OutputStream(instance, Protocol.currentProtocolEncoding);
_os =
new OutputStream(
Protocol.currentProtocolEncoding,
instance.defaultsAndOverrides().defaultFormat);
}

protected OutgoingAsyncBase(Communicator com, Instance instance, String op, OutputStream os) {
Expand Down
Loading

0 comments on commit 6d845de

Please sign in to comment.