New transport API skeleton including support for ls-remote
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / PacketLineOut.java
blob0e046edf6b859ab7e5d36a6f9fa06fd9c48b21ae
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.transport;
19 import java.io.IOException;
20 import java.io.OutputStream;
22 import org.spearce.jgit.lib.Constants;
24 class PacketLineOut {
25 private final OutputStream out;
27 private final byte[] lenbuffer;
29 PacketLineOut(final OutputStream i) {
30 out = i;
31 lenbuffer = new byte[4];
34 void writeString(final String s) throws IOException {
35 writePacket(Constants.encodeASCII(s));
38 void writePacket(final byte[] packet) throws IOException {
39 writeLength(packet.length + 4);
40 out.write(packet);
43 void end() throws IOException {
44 writeLength(0);
45 flush();
48 void flush() throws IOException {
49 out.flush();
52 private static final byte[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
53 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
55 private void writeLength(int w) throws IOException {
56 int o = 3;
57 while (o >= 0 && w != 0) {
58 lenbuffer[o--] = hexchar[w & 0xf];
59 w >>>= 4;
61 while (o >= 0)
62 lenbuffer[o--] = '0';
63 out.write(lenbuffer, 0, 4);