br.com.objectos.tftp.ReadOptions Maven / Gradle / Ivy
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.tftp;
import java.util.ArrayList;
import java.util.List;
/**
* @author [email protected] (Marcio Endo)
*/
class ReadOptions {
final int blockSize;
public ReadOptions(int blockSize) {
this.blockSize = blockSize;
}
@Override
public boolean equals(Object obj) {
ReadOptions that = (ReadOptions) obj;
return blockSize == that.blockSize;
}
@Override
public int hashCode() {
return Integer.hashCode(blockSize);
}
public List toDataMessage(byte[] bytes) {
int parts = (bytes.length / blockSize) + 1;
int fullparts = parts - 1;
List list = new ArrayList<>(parts);
for (int i = 0; i < fullparts; i++) {
byte[] part = new byte[blockSize];
System.arraycopy(bytes, i * blockSize, part, 0, blockSize);
list.add(DataMessage.get(i + 1, part));
}
int lastSize = bytes.length % blockSize;
byte[] part = new byte[lastSize];
System.arraycopy(bytes, fullparts * blockSize, part, 0, lastSize);
list.add(DataMessage.get(parts, part));
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy