17
17
18
18
package org .apache .spark .broadcast
19
19
20
- import java .io .{ ByteArrayInputStream , ObjectInputStream , ObjectOutputStream }
20
+ import java .io ._
21
21
22
22
import scala .reflect .ClassTag
23
23
import scala .util .Random
24
24
25
25
import org .apache .spark .{Logging , SparkConf , SparkEnv , SparkException }
26
26
import org .apache .spark .storage .{BroadcastBlockId , StorageLevel }
27
- import org .apache .spark .util .Utils
28
27
29
28
/**
30
29
* A [[org.apache.spark.broadcast.Broadcast ]] implementation that uses a BitTorrent-like
@@ -228,8 +227,12 @@ private[broadcast] object TorrentBroadcast extends Logging {
228
227
initialized = false
229
228
}
230
229
231
- def blockifyObject [T ](obj : T ): TorrentInfo = {
232
- val byteArray = Utils .serialize[T ](obj)
230
+ def blockifyObject [T : ClassTag ](obj : T ): TorrentInfo = {
231
+ val bos = new ByteArrayOutputStream ()
232
+ val ser = SparkEnv .get.serializer.newInstance()
233
+ val serOut = ser.serializeStream(bos)
234
+ serOut.writeObject[T ](obj).close()
235
+ val byteArray = bos.toByteArray
233
236
val bais = new ByteArrayInputStream (byteArray)
234
237
235
238
var blockNum = byteArray.length / BLOCK_SIZE
@@ -255,7 +258,7 @@ private[broadcast] object TorrentBroadcast extends Logging {
255
258
info
256
259
}
257
260
258
- def unBlockifyObject [T ](
261
+ def unBlockifyObject [T : ClassTag ](
259
262
arrayOfBlocks : Array [TorrentBlock ],
260
263
totalBytes : Int ,
261
264
totalBlocks : Int ): T = {
@@ -264,7 +267,12 @@ private[broadcast] object TorrentBroadcast extends Logging {
264
267
System .arraycopy(arrayOfBlocks(i).byteArray, 0 , retByteArray,
265
268
i * BLOCK_SIZE , arrayOfBlocks(i).byteArray.length)
266
269
}
267
- Utils .deserialize[T ](retByteArray, Thread .currentThread.getContextClassLoader)
270
+ val in = new ByteArrayInputStream (retByteArray)
271
+ val ser = SparkEnv .get.serializer.newInstance()
272
+ val serIn = ser.deserializeStream(in)
273
+ val obj = serIn.readObject[T ]()
274
+ serIn.close()
275
+ obj
268
276
}
269
277
270
278
/**
0 commit comments