Skip to content

Commit 096387d

Browse files
authored
fix: do not use fixed size buffer (#10)
* fix: do not use fixed size buffer
1 parent 8e96ead commit 096387d

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Diff for: src/main/kotlin/com/vaadin/plugin/copilot/service/CopilotServerServiceImpl.kt

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.vaadin.plugin.copilot.service
22

3+
import com.intellij.openapi.diagnostic.Logger
34
import com.intellij.openapi.project.Project
5+
import com.vaadin.plugin.copilot.CopilotPluginUtil
46
import io.ktor.util.network.*
7+
import java.io.ByteArrayOutputStream
58
import java.io.IOException
69
import java.nio.ByteBuffer
710
import java.nio.channels.SelectionKey
@@ -13,6 +16,8 @@ import java.util.*
1316
// Server implementation based on https://github.com/teocci/NioSocketCodeSample/tree/master
1417
class CopilotServerServiceImpl(private val project: Project): CopilotServerService {
1518

19+
private val LOG: Logger = Logger.getInstance(CopilotPluginUtil::class.java)
20+
1621
private val timeout: Long = 500
1722

1823
private var serverChannel: ServerSocketChannel? = null
@@ -47,7 +52,7 @@ class CopilotServerServiceImpl(private val project: Project): CopilotServerServi
4752
serverChannel!!.register(selector, SelectionKey.OP_ACCEPT)
4853
serverRunning = true
4954
} catch (e: IOException) {
50-
e.printStackTrace()
55+
LOG.error(e)
5156
}
5257
}
5358

@@ -79,7 +84,7 @@ class CopilotServerServiceImpl(private val project: Project): CopilotServerServi
7984
}
8085
}
8186
} catch (e: IOException) {
82-
e.printStackTrace()
87+
LOG.error(e)
8388
} finally {
8489
closeConnection()
8590
}
@@ -92,7 +97,7 @@ class CopilotServerServiceImpl(private val project: Project): CopilotServerServi
9297
serverChannel!!.socket().close()
9398
serverChannel!!.close()
9499
} catch (e: IOException) {
95-
e.printStackTrace()
100+
LOG.error(e)
96101
}
97102
}
98103
}
@@ -109,28 +114,27 @@ class CopilotServerServiceImpl(private val project: Project): CopilotServerServi
109114
private fun read(key: SelectionKey): ByteArray? {
110115
val channel = key.channel() as SocketChannel
111116
val readBuffer = ByteBuffer.allocate(4096)
112-
readBuffer.clear()
113-
114-
val read: Int
117+
val baos = ByteArrayOutputStream()
118+
var read: Int
115119
try {
116-
read = channel.read(readBuffer)
120+
while(true) {
121+
readBuffer.clear()
122+
read = channel.read(readBuffer)
123+
if (read == -1) {
124+
channel.close()
125+
key.cancel()
126+
break
127+
}
128+
baos.write(readBuffer.array(), 0, read)
129+
}
117130
} catch (e: IOException) {
118-
e.printStackTrace()
131+
LOG.error(e)
119132
key.cancel()
120133
channel.close()
121134
return null
122135
}
123136

124-
if (read == -1) {
125-
channel.close()
126-
key.cancel()
127-
return null
128-
}
129-
// IMPORTANT - don't forget the flip() the buffer. It is like a reset without clearing it.
130-
readBuffer.flip()
131-
val data = ByteArray(4096)
132-
readBuffer[data, 0, read]
133-
return data
137+
return baos.toByteArray()
134138
}
135139

136140
}

0 commit comments

Comments
 (0)