Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/ByteArrayChannel.java @ 608:e1b29756f901
Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 07 May 2013 21:27:51 +0200 |
parents | 1a7a9a20e1f9 |
children |
comparison
equal
deleted
inserted
replaced
607:66f1cc23b906 | 608:e1b29756f901 |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2011 TMate Software Ltd | 2 * Copyright (c) 2011-2013 TMate Software Ltd |
3 * | 3 * |
4 * This program is free software; you can redistribute it and/or modify | 4 * This program is free software; you can redistribute it and/or modify |
5 * it under the terms of the GNU General Public License as published by | 5 * it under the terms of the GNU General Public License as published by |
6 * the Free Software Foundation; version 2 of the License. | 6 * the Free Software Foundation; version 2 of the License. |
7 * | 7 * |
21 import java.util.List; | 21 import java.util.List; |
22 | 22 |
23 import org.tmatesoft.hg.util.ByteChannel; | 23 import org.tmatesoft.hg.util.ByteChannel; |
24 | 24 |
25 /** | 25 /** |
26 * | 26 * {@link ByteChannel} implementation that serializes data into a byte array |
27 * | |
27 * @author Artem Tikhomirov | 28 * @author Artem Tikhomirov |
28 * @author TMate Software Ltd. | 29 * @author TMate Software Ltd. |
29 */ | 30 */ |
30 public class ByteArrayChannel implements ByteChannel { | 31 public class ByteArrayChannel implements ByteChannel { |
31 private final List<ByteBuffer> buffers; | 32 private final List<ByteBuffer> buffers; |
46 buffers = null; | 47 buffers = null; |
47 target = ByteBuffer.allocate(size); | 48 target = ByteBuffer.allocate(size); |
48 } | 49 } |
49 } | 50 } |
50 | 51 |
51 // TODO document what happens on write after toArray() in each case | 52 /* |
53 * {@link #toArray()} calls do not clear data collected so far, subsequent {@link #write(ByteBuffer)} | |
54 * augment collected content. | |
55 */ | |
52 public int write(ByteBuffer buffer) { | 56 public int write(ByteBuffer buffer) { |
53 int rv = buffer.remaining(); | 57 int rv = buffer.remaining(); |
54 if (buffers == null) { | 58 if (buffers == null) { |
55 target.put(buffer); | 59 target.put(buffer); |
56 } else { | 60 } else { |
57 ByteBuffer copy = ByteBuffer.allocate(rv); | 61 ByteBuffer copy = ByteBuffer.allocate(rv); |
58 copy.put(buffer); | 62 copy.put(buffer); |
59 buffers.add(copy); | 63 buffers.add(copy); |
60 } | 64 } |
65 result = null; | |
61 return rv; | 66 return rv; |
62 } | 67 } |
63 | 68 |
69 /** | |
70 * @return content accumulated so far | |
71 */ | |
64 public byte[] toArray() { | 72 public byte[] toArray() { |
65 if (result != null) { | 73 if (result != null) { |
66 return result; | 74 return result; |
67 } | 75 } |
68 if (buffers == null) { | 76 if (buffers == null) { |
82 int off = 0; | 90 int off = 0; |
83 for (ByteBuffer bb : buffers) { | 91 for (ByteBuffer bb : buffers) { |
84 bb.get(result, off, bb.limit()); | 92 bb.get(result, off, bb.limit()); |
85 off += bb.limit(); | 93 off += bb.limit(); |
86 } | 94 } |
87 buffers.clear(); | |
88 return result; | 95 return result; |
89 } | 96 } |
90 } | 97 } |
91 } | 98 } |