序列化

简介: 本文从源码角度解析Zookeeper的序列化机制,重点分析jute包中的InputArchive和OutputArchive接口及其实现类,涵盖基本类型与记录的序列化过程,揭示其在网络通信与数据存储中的核心作用。

一、前言  在完成了前面的理论学习后,现在可以从源码角度来解析Zookeeper的细节,首先笔者想从序列化入手,因为在网络通信、数据存储中都用到了序列化,下面开始分析。二、序列化  序列化主要在zookeeper.jute包中,其中涉及的主要接口如下    · InputArchive    · OutputArchive    · Index    · Record2.1 InputArchive  其是所有反序列化器都需要实现的接口,其方法如下 InputArchive的类结构如下  1. BinaryInputArchive 2. CsvInputArchive 3. XmlInputArchive2.2 OutputArchive  其是所有序列化器都需要实现此接口,其方法如下。  OutputArchive的类结构如下  1. BinaryOutputArchive 2. CsvOutputArchive 3. XmlOutputArchive

Java

运行代码复制代码

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

/**

// 写boolean类型

public void writeBool(boolean b, String tag) throws IOException {

printBeginEnvelope(tag);

stream.print("<boolean>");

stream.print(b ? "1" : "0");

stream.print("</boolean>");

printEndEnvelope(tag);

}

// 写int类型

public void writeInt(int i, String tag) throws IOException {

printBeginEnvelope(tag);

stream.print("<i4>");

stream.print(Integer.toString(i));

stream.print("</i4>");

printEndEnvelope(tag);

}

// 写long类型

public void writeLong(long l, String tag) throws IOException {

printBeginEnvelope(tag);

stream.print("<ex:i8>");

stream.print(Long.toString(l));

stream.print("</ex:i8>");

printEndEnvelope(tag);

}

// 写float类型

public void writeFloat(float f, String tag) throws IOException {

printBeginEnvelope(tag);

stream.print("<ex:float>");

stream.print(Float.toString(f));

stream.print("</ex:float>");

printEndEnvelope(tag);

}

// 写double类型

public void writeDouble(double d, String tag) throws IOException {

printBeginEnvelope(tag);

stream.print("<double>");

stream.print(Double.toString(d));

stream.print("</double>");

printEndEnvelope(tag);

}

// 写String类型

public void writeString(String s, String tag) throws IOException {

printBeginEnvelope(tag);

stream.print("<string>");

stream.print(Utils.toXMLString(s));

stream.print("</string>");

printEndEnvelope(tag);

}

// 写Buffer类型

public void writeBuffer(byte buf[], String tag)

throws IOException {

printBeginEnvelope(tag);

stream.print("<string>");

stream.print(Utils.toXMLBuffer(buf));

stream.print("</string>");

printEndEnvelope(tag);

}

// 写Record类型

public void writeRecord(Record r, String tag) throws IOException {

r.serialize(this, tag);

}

// 开始写Record类型

public void startRecor

相关文章
|
6天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
18200 12
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
18天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
29726 141
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
7天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
4625 20
|
6天前
|
人工智能 API 开发者
阿里云百炼 Coding Plan 售罄、Lite 停售、Pro 抢不到?最新解决方案
阿里云百炼Coding Plan Lite已停售,Pro版每日9:30限量抢购难度大。本文解析原因,并提供两大方案:①掌握技巧抢购Pro版;②直接使用百炼平台按量付费——新用户赠100万Tokens,支持Qwen3.5-Max等满血模型,灵活低成本。
1468 3
阿里云百炼 Coding Plan 售罄、Lite 停售、Pro 抢不到?最新解决方案