/*
 * Copyright (c) 2017 Mattias Eklöf <mattias.eklof@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package com.github.jomnipod.datatypes;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class ZeroTerminatedString implements DataType<String> {

	private byte[] buffer;

	public ZeroTerminatedString(ByteBuffer byteBuffer, int maxLength) {
		buffer = new byte[maxLength];
		byteBuffer.get(buffer);
	}

	public ZeroTerminatedString(InputStream stream, int maxLength) throws IOException {
		buffer = new byte[maxLength];
		stream.read(buffer);
	}

	@Override
	public String value() {
		int length = buffer.length;
		for (int i = 0; i < buffer.length; i++) {
			if (buffer[i] == 0) {
				length = i;
				break;
			}
		}

		return new String(buffer, 0, length, Charset.forName("ISO-8859-1"));
	}
}

@@ -1383,21 +1383,20 @@
 ate 
-byte%5B%5D buffer
+String value
 ;%0A%0A%09
@@ -1461,24 +1461,31 @@
 Length) %7B%0A%09%09
+byte%5B%5D 
 buffer = new
@@ -1503,365 +1503,218 @@
 ngth
++1
 %5D;%0A%09%09
-byteBuffer.get(buffer);%0A%09%7D%0A%0A%09public ZeroTerminatedString(InputStream stream, int maxLength) throws IOException %7B%0A%09%09buffer = new byte%5BmaxLength%5D;%0A%09%09stream.read(buffer);%0A%09%7D%0A%0A%09@Override%0A%09public String value() %7B%0A%09%09int length = buffer.length;%0A%09%09for (int i = 0; i %3C buffer.length; i++) %7B%0A%09%09%09if (
+int pos = 0;%0A%09%09byte got = byteBuffer.get();%0A%09%09while (pos %3C maxLength && byteBuffer.remaining() != 0 && got != 0) %7B%0A%09%09%09buffer%5Bpos++%5D = got;%0A%09%09%09got = byteBuffer.get();%0A%09%09%7D%0A%09%09
 buffer%5B
-i
+pos
 %5D =
-=
  0
-) %7B%0A%09%09%09%09length = i;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return
+;%0A%09%09this.value =
  new
@@ -1736,14 +1736,11 @@
  0, 
-length
+pos
 , Ch
@@ -1771,11 +1771,67 @@
 1%22));%0A%09%7D
+%0A%0A%09@Override%0A%09public String value() %7B%0A%09%09return value;%0A%09%7D
 %0A%7D%0A

fix ZeroTerminatedString parsing