Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/DataAccess.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 | 48f993aa2f41 |
children | 4f93bbc73b64 |
comparison
equal
deleted
inserted
replaced
607:66f1cc23b906 | 608:e1b29756f901 |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2010-2012 TMate Software Ltd | 2 * Copyright (c) 2010-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 * |
97 if (length == 0) { | 97 if (length == 0) { |
98 return; | 98 return; |
99 } | 99 } |
100 throw new IOException(String.format("No data, can't read %d bytes", length)); | 100 throw new IOException(String.format("No data, can't read %d bytes", length)); |
101 } | 101 } |
102 // reads bytes into ByteBuffer, up to its limit or total data length, whichever smaller | 102 /** |
103 // TODO post-1.0 perhaps, in DataAccess paradigm (when we read known number of bytes, we shall pass specific byte count to read) | 103 * reads bytes into ByteBuffer, up to its limit or total data length, whichever smaller. |
104 // for 1.0, it's ok as it's our internal class | 104 * XXX perhaps, in DataAccess paradigm (when we read known number of bytes, we shall pass specific byte count to read) |
105 */ | |
105 public void readBytes(ByteBuffer buf) throws IOException { | 106 public void readBytes(ByteBuffer buf) throws IOException { |
106 // int toRead = Math.min(buf.remaining(), (int) length()); | 107 // int toRead = Math.min(buf.remaining(), (int) length()); |
107 // if (buf.hasArray()) { | 108 // if (buf.hasArray()) { |
108 // readBytes(buf.array(), buf.arrayOffset(), toRead); | 109 // readBytes(buf.array(), buf.arrayOffset(), toRead); |
109 // } else { | 110 // } else { |
110 // byte[] bb = new byte[toRead]; | 111 // byte[] bb = new byte[toRead]; |
111 // readBytes(bb, 0, bb.length); | 112 // readBytes(bb, 0, bb.length); |
112 // buf.put(bb); | 113 // buf.put(bb); |
113 // } | 114 // } |
114 // TODO post-1.0 optimize to read as much as possible at once | 115 // TODO [post-1.1] optimize to read as much as possible at once |
115 while (!isEmpty() && buf.hasRemaining()) { | 116 while (!isEmpty() && buf.hasRemaining()) { |
116 buf.put(readByte()); | 117 buf.put(readByte()); |
117 } | 118 } |
118 } | 119 } |
119 public byte readByte() throws IOException { | 120 public byte readByte() throws IOException { |
120 throw new UnsupportedOperationException(); | 121 throw new UnsupportedOperationException(); |
121 } | 122 } |
122 | 123 |
123 // XXX decide whether may or may not change position in the DataAccess | 124 /** |
124 // TODO REVISIT exception handling may not be right, initially just for the sake of quick test | 125 * Content of this DataAccess as byte array. |
126 * Note, likely changes position in the DataAccess. | |
127 * Might provide direct access to underlying data structure in certain cases, do not alter. | |
128 * | |
129 * @return byte array of {@link #length()} size, filled with data | |
130 * @throws IOException | |
131 */ | |
125 public byte[] byteArray() throws IOException { | 132 public byte[] byteArray() throws IOException { |
126 reset(); | 133 reset(); |
127 byte[] rv = new byte[length()]; | 134 byte[] rv = new byte[length()]; |
128 readBytes(rv, 0, rv.length); | 135 readBytes(rv, 0, rv.length); |
129 return rv; | 136 return rv; |