Skip to content

Commit 65c3c44

Browse files
committed
Avoid code duplication
1 parent e314305 commit 65c3c44

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -256,27 +256,21 @@ public void UpdateData()
256256

257257
if (hasValuesInUtf8)
258258
{
259-
length += 5 + CodedCharacterSetUtf8Value.Length; // Additional length for UTF-8 Tag.
259+
// Additional length for UTF-8 Tag.
260+
length += 5 + CodedCharacterSetUtf8Value.Length;
260261
}
261262

262263
this.Data = new byte[length];
263-
int i = 0;
264-
264+
int offset = 0;
265265
if (hasValuesInUtf8)
266266
{
267-
// Envelope Record.
268-
this.Data[i++] = IptcTagMarkerByte;
269-
this.Data[i++] = (byte)IptcRecordNumber.Envelope;
270-
this.Data[i++] = IptcEnvelopeCodedCharacterSet;
271-
this.Data[i++] = (byte)(CodedCharacterSetUtf8Value.Length >> 8);
272-
this.Data[i++] = (byte)CodedCharacterSetUtf8Value.Length;
273-
Buffer.BlockCopy(CodedCharacterSetUtf8Value, 0, this.Data, i, CodedCharacterSetUtf8Value.Length);
274-
i += CodedCharacterSetUtf8Value.Length;
267+
// Write Envelope Record.
268+
offset = this.WriteRecord(offset, CodedCharacterSetUtf8Value, IptcRecordNumber.Envelope, IptcEnvelopeCodedCharacterSet);
275269
}
276270

277271
foreach (IptcValue value in this.Values)
278272
{
279-
// Application Record.
273+
// Write Application Record.
280274
// +-----------+----------------+---------------------------------------------------------------------------------+
281275
// | Octet Pos | Name | Description |
282276
// +==========-+================+=================================================================================+
@@ -293,17 +287,24 @@ public void UpdateData()
293287
// | | Octet Count | the following data field(32767 or fewer octets). Note that the value of bit 7 of|
294288
// | | | octet 4(most significant bit) always will be 0. |
295289
// +-----------+----------------+---------------------------------------------------------------------------------+
296-
this.Data[i++] = IptcTagMarkerByte;
297-
this.Data[i++] = (byte)IptcRecordNumber.Application;
298-
this.Data[i++] = (byte)value.Tag;
299-
this.Data[i++] = (byte)(value.Length >> 8);
300-
this.Data[i++] = (byte)value.Length;
301-
if (value.Length > 0)
302-
{
303-
Buffer.BlockCopy(value.ToByteArray(), 0, this.Data, i, value.Length);
304-
i += value.Length;
305-
}
290+
offset = this.WriteRecord(offset, value.ToByteArray(), IptcRecordNumber.Application, (byte)value.Tag);
291+
}
292+
}
293+
294+
private int WriteRecord(int offset, byte[] recordData, IptcRecordNumber recordNumber, byte recordBinaryRepresentation)
295+
{
296+
this.Data[offset++] = IptcTagMarkerByte;
297+
this.Data[offset++] = (byte)recordNumber;
298+
this.Data[offset++] = recordBinaryRepresentation;
299+
this.Data[offset++] = (byte)(recordData.Length >> 8);
300+
this.Data[offset++] = (byte)recordData.Length;
301+
if (recordData.Length > 0)
302+
{
303+
Buffer.BlockCopy(recordData, 0, this.Data, offset, recordData.Length);
304+
offset += recordData.Length;
306305
}
306+
307+
return offset;
307308
}
308309

309310
private void Initialize()

0 commit comments

Comments
 (0)